"node-red-contrib-play-sound" fails in node-red on miniPC started from systemd service

hi @zenofmud ,
This is cool isn't it. I my self built also something like this. I was using a miniPC. I used mplayer to play mp3 files from nodered ui.
But I have still an unsolved problem regarding the setup. I have very little knowledge of this nodejs/nodered stuff.

My problem is if node-red was started from systemd service, this "node-red-contrib-play-sound" failed to produce sound with this message in the logs:

5 Oct 19:09:19 - [info] [PlaySound:bc151945aaf7784e] Event input: /home/mine/song/003 .mp3
5 Oct 19:09:19 - [info] [PlaySound:bc151945aaf7784e] Play: /home/mine/song/003.mp3
5 Oct 19:09:19 - [info] [PlaySound:bc151945aaf7784e] Event exit:0, null

But if I started node-red with $ node-red from the shell the play-sound node will work fine.

Maybe you could share your thoughts to help me on this?

That first line shows a space between the 003 and the .mp3 where is that coming from?

The fact you’re using a minipc and having a issue based on the way you start NR really doesn’t have anything to do with this project so I’m going to move your question to a new thread.

hi @zenofmud

... that was just my typo when I edited the messages for the post here.

What do you see in the terminal when you start node-red from the shell and it plays correctly?

hi @Colin , here is the screen just from the node-red manual start and to the time when I play the mp3 with mplayer using "node-red-contrib-play-sound".
It's playing for about 7-8 minutes and then the sound stop normally.

09:56:10 mine@mod-server$ node-red
6 Oct 21:56:22 - [info] 

Welcome to Node-RED
===================

6 Oct 21:56:22 - [info] Node-RED version: v3.0.2
6 Oct 21:56:22 - [info] Node.js  version: v14.20.1
6 Oct 21:56:22 - [info] Linux 5.10.0-18-amd64 x64 LE
6 Oct 21:56:23 - [info] Loading palette nodes
6 Oct 21:56:25 - [info] Dashboard version 3.2.0 started at /ui
6 Oct 21:56:25 - [info] Settings file  : /home/mine/.node-red/settings.js
6 Oct 21:56:25 - [info] Context store  : 'memoryOnly' [module=memory]
6 Oct 21:56:25 - [info] Context store  : 'file' [module=localfilesystem]
6 Oct 21:56:25 - [info] User directory : /home/mine/.node-red
6 Oct 21:56:25 - [info] Projects directory: /home/mine/.node-red/projects
6 Oct 21:56:25 - [info] Server now running at http://127.0.0.1:1880/
6 Oct 21:56:25 - [info] Active project : TEST
6 Oct 21:56:25 - [info] Flows file     : /home/mine/.node-red/projects/TEST/flow.json
6 Oct 21:56:25 - [info] Starting flows
6 Oct 21:56:26 - [info] Started flows
6 Oct 21:56:26 - [info] [mqtt-broker:mqtt@localhost:1883] Connected to broker: mod_nodered@mqtt://localhost:1883

6 Oct 21:57:40 - [info] [PlaySound:bc151945aaf7784e] Event input: /home/mine/song/003.mp3
6 Oct 21:57:40 - [info] [PlaySound:bc151945aaf7784e] Play: /home/mine/song/003.mp3
6 Oct 22:05:56 - [info] [PlaySound:bc151945aaf7784e] Event exit:0, null

This below is the screen after I started node-red by systemd and play the file. It immediately stopped (or not even started the mplayer?)

10:16:28 mine@mod-server$ sudo systemctl start nodered.service 
[sudo] password for mine: 
10:16:42 mine@mod-server$ node-red-log 
Started Node-RED graphical event wiring tool.

6 Oct 22:16:43 - [info]
Welcome to Node-RED
===================
6 Oct 22:16:43 - [info] Node-RED version: v3.0.2
6 Oct 22:16:43 - [info] Node.js  version: v14.20.1
6 Oct 22:16:43 - [info] Linux 5.10.0-18-amd64 x64 LE
6 Oct 22:16:43 - [info] Loading palette nodes
6 Oct 22:16:45 - [info] Dashboard version 3.2.0 started at /ui
6 Oct 22:16:45 - [info] Settings file  : /home/mine/.node-red/settings.js
6 Oct 22:16:45 - [info] Context store  : 'memoryOnly' [module=memory]
6 Oct 22:16:45 - [info] Context store  : 'file' [module=localfilesystem]
6 Oct 22:16:45 - [info] User directory : /home/mine/.node-red
6 Oct 22:16:45 - [info] Projects directory: /home/mine/.node-red/projects
6 Oct 22:16:45 - [info] Server now running at http://127.0.0.1:1880/
6 Oct 22:16:45 - [info] Active project : TEST
6 Oct 22:16:45 - [info] Flows file     : /home/mine/.node-red/projects/TEST/flow.json
6 Oct 22:16:45 - [info] Starting flows
6 Oct 22:16:45 - [info] Started flows
6 Oct 22:16:46 - [info] [mqtt-broker:mqtt@localhost:1883] Connected to broker: mod_nodered@mqtt://localhost:1883
6 Oct 22:17:13 - [info] [PlaySound:bc151945aaf7784e] Event input: /home/mine/song/003.mp3
6 Oct 22:17:13 - [info] [PlaySound:bc151945aaf7784e] Play: /home/mine/song/003.mp3
6 Oct 22:17:13 - [info] [PlaySound:bc151945aaf7784e] Event exit:0, null

Run
tail -f /var/log/syslog
and with that open try to run it again. See if anything appears in syslog.

I think the play sound node shells out to a system command - so I suspect the environment when running as a service (from systemd) is different from running direct from command line.

Another thing, this node hasn't been updated in over 4 years and the author hasn't been active on GitHub for over 4 years also.

At this point I would think it is safe to say the node has been abandoned.

hi @dceejay

How do I find the different environments of that specific service?

I did 3 different methods to start NR.

  1. with $ node-red

  2. with $ node-red-pi

  3. with systemd: $ sudo systemctl start nodered.service

With method #1 and #2 I can play the mp3 song with mplayer, but can not play it with method #3.

hi @zenofmud
Yeah, I also think so.
Do you have any other node with same functionality to suggest?

hi @Colin ,

Output from syslog did not help much. This is the screen shot of node-red started by systemd.

04:42:07 mine@mod-server$ sudo tail -f /var/log/syslog
[sudo] password for mine: 
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Context store  : 'memoryOnly' [module=memory]
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Context store  : 'file' [module=localfilesystem]
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] User directory : /home/mine/.node-red
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Projects directory: /home/mine/.node-red/projects
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Server now running at http://127.0.0.1:1880/
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Active project : TEST
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Flows file     : /home/pod/.node-red/projects/TEST/flow.json
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Starting flows
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] Started flows
Oct  7 04:42:11 mod-server Node-RED[2635]: 7 Oct 04:42:11 - [info] [mqtt-broker:mqtt@localhost:1883] Connected to broker: mod_nodered@mqtt://localhost:1883
Oct  7 05:00:51 mod-server Node-RED[2635]: 7 Oct 05:00:51 - [info] [PlaySound:bc151945aaf7784e] Event input: /home/mine/song/003.mp3
Oct  7 05:00:51 mod-server Node-RED[2635]: 7 Oct 05:00:51 - [info] [PlaySound:bc151945aaf7784e] Play: /home/mine/song/003.mp3
Oct  7 05:00:51 mod-server Node-RED[2635]: 7 Oct 05:00:51 - [info] [PlaySound:bc151945aaf7784e] Event exit:0, null

you could take a look at node-red-contrib-play-soundfile and see if that would work for you.

Thanks @zenofmud.
I will check it out.

I think I should go to this direction. Thanks @dceejay.

Here is what is stated in Using environment variables : Node-RED

Does anyone have experience with this?

For those who are following this thread, I solved my original problem with adding this line in the systemd service script /lib/systemd/system/nodered.service :

Environment="DISPLAY=:0"

reloaded daemon config and restart node-red with systemd.

Anyway, this introduced another problem for me which is I can not change the audio volume anymore by changing pulseaudio volume with this error message:

Failed to connect to pulseaudio: Connection refused

I would appreciate if anyone here would give me pointer to any direction for solving this issue.

... and I solved the newly introduced pulseaudio volume problem by uncommenting autospawn = yes in this /etc/pulse/client.conf file and rebooting the miniPC.

Yes. If you look into my alternate installer for node-red, you will find an example setup where the systemd script example file includes an environment file which is a lot easier to manage in case you need to make future changes. The example settings.js also makes better use of optional environment variables as well.

No idea if this will be helpful but I wanted to satisfy my own curiosity.

Install Pico TTS using sudo apt install libttspico-utils then the following tiny flow will speak to you through your server's speakers assuming that at least ALSA is configured.

[{"id":"c360ce2a0183b10e","type":"inject","z":"4a3fcf357a20bf56","name":"Dave","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Look Dave, I can see you're really upset about this.","payloadType":"str","x":210,"y":1280,"wires":[["6f48f521352afcc4"]]},{"id":"6f48f521352afcc4","type":"exec","z":"4a3fcf357a20bf56","command":"pico2wave -l en-GB -w lookdave.wav \"","addpay":"payload","append":"\" && aplay lookdave.wav && rm lookdave.wav","useSpawn":"false","timer":"","winHide":true,"oldrc":false,"name":"","x":450,"y":1280,"wires":[[],[],[]]}]

Similarly, as long as you have ALSA installed and configured, you should be able to play various sound file formats with aplayer:

[{"id":"347583e6184bc379","type":"inject","z":"4a3fcf357a20bf56","name":"Left","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"/usr/share/sounds/alsa/Side_Left.wav","payloadType":"str","x":210,"y":1360,"wires":[["80ecb8a8c9bcb479"]]},{"id":"80ecb8a8c9bcb479","type":"exec","z":"4a3fcf357a20bf56","command":"aplay","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":true,"oldrc":false,"name":"","x":350,"y":1360,"wires":[[],[],[]]}]

Got some ideas on using this with a PIR detector in my office so that the server tells me if something is wrong when I walk in. Might get it to tell me the weather in the morning as well :sunglasses:

I did notice this alternative installer, but I have not tried that yet. Thank you.

As far as I know, I did not install ALSA by intention, I only installed PulseAudio.
I will check this suggestion later. Thanks anyway.