[Help!] exec node + pulseaudio

I am running Node-RED on a Raspberry Pi Zero W.
In the Pi terminal, I can run the following without error:
pactl -- set-sink-volume 0 15%

If I run the same code via an exec node, it doesn't work, and I get the following error:
Connection failure: Connection refused
pa_context_connect() failed: Connection refused

I can use the exec node to run pactl info and it returns the info.

My problem is that I have absolutely no understanding of the Linux stack, so trawling through other people's solutions have just led to random copy and paste sessions, and none of the changes have helped.

I would really appreciate it if someone could give me a high level explanation of the problem to focus my research further.

Thanks
Brendon

What version of NR and node.js are you running? (see startup log)
What OS/version are you running on the Pi?

Is NR running under the same user as you are running in the terminal?

Thanks for responding.

Raspbian GNU Linux 11 (bullseye)
All updates are installed

Node RED was installed and then refreshed using the instructions here: Running on Raspberry Pi : Node-RED

Node-RED 2.2.2
Node.js 14.18.1
node red service is running under user pi
terminal is running under pi@console1 (console1 is the host name I assigned to the device)

Thanks
Brendon

can you provide an export of your flow which is showing the issue.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Just to let you know, I just tried it on one of my pi's and have no issue at all.

How have you got the exec node configured?

Here is my code

[
    {
        "id": "1b0e2ed279b9c88d",
        "type": "exec",
        "z": "89944eaef1a0d6e4",
        "command": "pactl -- set-sink-volume 0 70%",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Set volume to 70%",
        "x": 460,
        "y": 220,
        "wires": [
            [
                "9b5d469038e63be2"
            ],
            [],
            []
        ]
    }
] 

I don't know if it is worth mentioning that I am not using https
I also tried loading it in the chrome browser on the Pi itself. Same error.

Just tried your exec node on a pi4 and it works fine.

Which version of Bullseye are you using - desktop? desktop with suggested software, lite?

I'll flash a SD card tomrrow and stick it in one of my pi Zerow's but I want to make sure what version of Bullseye you have.

Interesting, I just installed the 2022-04-04 Bullseye on a pi zero w. Pulseaudio is not installed by default so I installed it
sudo apt-get install pulseaudio
and started getting the error from the command line

pi@paulpi:~ $ pactl -- set-sink-volume 0 15%
Connection failure: Connection refused
pa_context_connect() failed: Connection refused

but once I started it up:

pi@paulpi:~ $ pulseaudio -D

It works fine in the CLI

pi@paulpi : ~ $ pactl -- set-sink-volume 0 15%
pi@paulpi : ~ $

FYI: I did a fresh install of the core items (Raspbian and Node-RED) on a Raspberry Pi 3B+.
Same issue.

Same issue as you originally had or same as I have?
And please reply to my question of what version of raspbian you installed

I get the same error as I got previously.
To summarise:

Commands work in terminal.
They fail via exec.
"Simple" commands (e.g. delete a file) work via both terminal and exec.
Both the following fail:
pactl -- set-sink-volume 0 15%
amixer set Master 70%

Today I installed Raspbian 32 bit with the Raspberry Pi Desktop using Raspberry Pi imager.

Thanks
Brendon

So I just refreshed my SD card using 2022-04-04-raspios-bullseye-armhf.img and put it in a Raspberry Pi 3 Model B. Then I did:

sudo apt update
sudo apt full-upgrade

I then installed NR using the script and created the following flows

[{"id":"27d6cae76dfbbdb1","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"1b0e2ed279b9c88d","type":"exec","z":"27d6cae76dfbbdb1","command":"pactl -- set-sink-volume 0 20%","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":590,"y":180,"wires":[["a5c3cde7f06df862"],["c4bcd1215151f359"],["067fd8c0894758ca"]]},{"id":"8e948c825a7ab298","type":"inject","z":"27d6cae76dfbbdb1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":180,"wires":[["1b0e2ed279b9c88d"]]},{"id":"c4bcd1215151f359","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":180,"wires":[]},{"id":"a5c3cde7f06df862","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":140,"wires":[]},{"id":"067fd8c0894758ca","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":220,"wires":[]},{"id":"036d47456cf6d7d4","type":"exec","z":"27d6cae76dfbbdb1","command":"amixer set Master 70%","addpay":"","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":570,"y":380,"wires":[["fbb12fd84db39755"],["c609af3bea6d77db"],["4cd2c4281e32f5be"]]},{"id":"ec1811a29e1a2f54","type":"inject","z":"27d6cae76dfbbdb1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":380,"wires":[["036d47456cf6d7d4"]]},{"id":"c609af3bea6d77db","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":380,"wires":[]},{"id":"fbb12fd84db39755","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":340,"wires":[]},{"id":"4cd2c4281e32f5be","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":420,"wires":[]},{"id":"cf53559376404d84","type":"catch","z":"27d6cae76dfbbdb1","name":"","scope":null,"uncaught":false,"x":420,"y":60,"wires":[["ac9e9794d0c0c914"]]},{"id":"ac9e9794d0c0c914","type":"debug","z":"27d6cae76dfbbdb1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":60,"wires":[]}]

I now have the RPI3 dashboard showing on Monitor 1 and I have a Mac with another monitor accessing NR from the RPI3. When I press the two injects, I see the volume slider on the Pi Desktop move up and down. So it is working fine for me. Try the flow I provided.

If it doesn't work there is something different between how I am doing things and how you are.
Are you accessing NR using the monitor/keyboard connected on the Pi or a different computer?

Do you have more than one user defined on the Pi?

Thanks for your efforts to resolve this.
I just tried your flow on a fresh build - re-installed Raspbian and NR.
Same errors as previously reported.

I have Google Chrome running on my laptop.
It is displaying the Node-RED interface on the Raspberry Pi at http://192.168.1.xxx:1880

lslogins -u reveals a root user, and the user that I am logged in as.

I then pasted the commands into the Pi terminal via VNC.
pactl -- set-sink-volume 0 20% failed with Failed to get sink information: No such entity
pactl -- set-sink-volume 1 20% worked

amixer set Master 70% in terminal also works

I updated Node-RED with the changed command, and it still failed with the same result
In the terminal, sudo pactl -- set-sink-volume 1 20% resulted in:
Connection failure: Connection refused
pa_context_connect() failed: Connection refused
This suggests to me that the command is running correctly as the user and doesn't need elevated permissions.

Thanks
Brendon

1 - did you use the same RPI build that I used?
2 - are you doing this on the Pi Zero W? Is it a Pi Zero 2 W or older PiSero W v1.1?
3 - did you do the 'update' and 'full-upgrade'? (just making sure)
4 - why are you using VNC and not SSHing into the Pi? (curiosity question)
5 - when you run Node-red and VNC into the Pi, are you using the user 'Pi' or have you added another user?
6 - If you SSH into the Pi, you can start node-red and see the log. Please copy and paste the startup log from when you enter node-red thru the lines

17 May 09:34:18 - [info] Starting flows
17 May 09:34:18 - [info] Started flows

Meanwhile I'll flash a SD card and run it on a Pi Zero W (v1.1) and see what I see.

also if the user is not 'Pi' please run a groups NN where 'nn' is the id of the user you are running, like this:

**pi@pulsepi** : **~ $** groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users input render netdev spi i2c gpio lpadmin

You may need to add your user to some of those groups.

1 - did you use the same RPI build that I used?

I have no way to tell. I used the Raspberry Pi imager, which just fetches the image without declaring what it is.
cat /etc/os-release gives:
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)"

2 - are you doing this on the Pi Zero W? Is it a Pi Zero 2 W or older PiSero W v1.1?

cat /sys/firmware/devicetree/base/model gives:
Raspberry Pi Zero W Rev 1.1

3 - did you do the 'update' and 'full-upgrade'? (just making sure)

Yes

4 - why are you using VNC and not SSHing into the Pi? (curiosity question)

I do both. I use VNC to access VLC player, Chromium etc when necesssary for my explorations. I use terminal via VNC for executing commands, unless I happen to have ssh open.

5 - when you run Node-red and VNC into the Pi, are you using the user 'Pi' or have you added another user?

The new Raspbian setup does away with the default pi user. You have to create a user as part of the setup.

6 - If you SSH into the Pi, you can start node-red and see the log. Please copy and paste the startup log from when you enter node-red thru the lines

21 May 17:06:49 - [info]

Welcome to Node-RED

21 May 17:06:49 - [info] Node-RED version: v2.2.2
21 May 17:06:49 - [info] Node.js version: v14.19.1
21 May 17:06:49 - [info] Linux 5.15.32+ arm LE
21 May 17:06:56 - [info] Loading palette nodes
21 May 17:07:29 - [info] Dashboard version 3.1.6 started at /ui
21 May 17:07:39 - [warn] ------------------------------------------------------
21 May 17:07:39 - [warn] [tuya-led-node/tuya-led] ReferenceError: TuyaLedNode is not defined (line:139)
21 May 17:07:39 - [warn] ------------------------------------------------------
21 May 17:07:39 - [info] Settings file : /home/console1/.node-red/settings.js
21 May 17:07:39 - [info] Context store : 'default' [module=memory]
21 May 17:07:39 - [info] User directory : /home/console1/.node-red
21 May 17:07:39 - [warn] Projects disabled : editorTheme.projects.enabled=false
21 May 17:07:39 - [info] Flows file : /home/console1/.node-red/flows.json
21 May 17:07:40 - [info] Server now running at http://127.0.0.1:1880/
21 May 17:07:40 - [info] Starting flows
21 May 17:07:41 - [info] Started flows

console1 is the name of the user and the host name. The Tuya error is not related because the exec errors started before I installed the Tuya node.

Console1 user has access to all the groups you listed.

Thanks
Brendon

@brendonhatcher I don't know what to say but it works for me. I just reflashed my SD card (a 16GB) card using the Raspberry Pi Imager and named the pi console1 and named the user console

Here is the history of the commands I ran (I installed netatalk so I can access the files from my mac - I also have to edit netatalk/afp.conf in order for it to work)

    1  sudo apt install netatalk
    2  cat /etc/os-release
    3  groups console1
    4  sudo apt update
    5  sudo apt full-upgrade
    6  sudo nano /etc/netatalk/afp.conf
    7  bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
    8  node-red admin init
    9  sudo reboot
   10  pactl -- set-sink-volume 0 15%
   11  sudo raspi-config

I then used sudo raspi-config to allow the use of VNC. I then used VNC to access the pi, open a terminal window and ran:

console1@console1:~ $ pactl -- set-sink-volume 0 20%
console1@console1:~ $ 

as you can see I get no error.

The only difference I can think of is I'm using macOS Monterey on a Mac mini (M1, 2020).

At this point it doesn't look like there is anything else I can do for you. For me it works.

@Steve-Mcl Steve, if you have a moment (hahaha) and have a Pi Zero W v1.1 could you take a look at this ar make any suggestions?

Unfortunately I don't. Been out of stock every time I've went to order

@zenofmud did you try the commands as follows:

  1. Web browser on your mac
  2. Nodered running via http://xxx.xxx.xxx.xxx:1880

I also don't get errors when using VNC --> terminal or via the nodered interface running on chromium on the Pi. I only get the errors via web browser remotely.

Thanks again for your efforts.

A couple posts ago you posted this:

Now you say you don’t get errors so I’m confused.

Sitting at my Mac and opening the node-red instance running on the pi with the test flow, it works fine. And if I have a VNC connection to the pi to watch the screen, I can see the volume change on the pi.

So what do you mean by I only get the errors via web browser remotely