Play sound in Domoticz (Linux/Ubuntu)

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Play sound in Domoticz (Linux/Ubuntu)

Post by rudder66 »

Hi all!

I want to play event sounds in Domoticz on a Linux Mint server.
It is important for me to be able to adjust the hardware volume for playing sounds for each event.
Sound card integrated.

I used several utilities:
aplay - only wav plays and there is no way to set the volume.

Code: Select all

#!/bin/bash
aplay /home/rudder/audio/on.mp3 > /dev/null 2>&1 &
exit 0
mpg321 - works through the terminal, but does not work through running a script in Domoticz. I did not understand the reason.

Code: Select all

#!/bin/bash
mpg321 -g 80 /home/rudder/audio/on.mp3 > /dev/null 2>&1 &
exit 0
mplayer - plays most formats, software volume control. After reboot, everything reset.

Code: Select all

#!/bin/bash
mplayer -volume 80 /home/rudder/audio/on.mp3 > /dev/null 2>&1 &
exit 0
The hardware volume control works through the utility:

Code: Select all

pactl set-sink-volume 0 80%
but the script does not work in Domoticz - the volume does not change. :( Via the terminal works fine.

Code: Select all

#!/bin/bash
pactl set-sink-volume 0 80%
mplayer /home/rudder/audio/on.mp3 > /dev/null 2>&1 &
exit 0
Why?
What am I doing wrong?
Thk
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by waaren »

rudder66 wrote: Tuesday 07 January 2020 23:29 but the script does not work in Domoticz - the volume does not change. :( Via the terminal works fine.
Why?
What am I doing wrong?
In many similar issues it turned out that the executing user (the process owner of domoticz) was not the same (or did not use the same environment) as the one that was used to test it on the CLI level.
What helped in those circumstances was to use full qualified pathnames, use sudo or specify what shell or python version need to be used.
(or a combination of them)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by rudder66 »

I use absolute full paths.

Code: Select all

	script:///home/rudder/audio/playmp3.sh
The Domoticz service is started as a daemon with the root user.

Code: Select all

1129 root      20   0 1070232  47860  16900 S   0,7  1,7   0:12.93 domoticz  
All bash scripts have root owner.
All scripts have 777 permission.

Code: Select all

-rwxrwxrwx 1 root   root      161 янв  8 02:12 playmp3.sh

To be more precise, from this script, only one command does not work in Domoticz:

Code: Select all

pactl -- set-sink-volume 0 80%
Command 'mplayer -volume 100 /home/rudder/audio/on.mp3 > /dev/null 2>&1 &' works in the same script!
Even if I leave only one 'pactl -- set-sink-volume 0 80%' command, then it does not work in Domoticz.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by waaren »

rudder66 wrote: Wednesday 08 January 2020 0:38 ITo be more precise, from this script, only one command does not work in Domoticz:

Code: Select all

pactl -- set-sink-volume 0 80%
Did you try

Code: Select all

/usr/bin/pactl -- set-sink-volume 0 80%
?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by rudder66 »

No, it does not work, I tried.
I do not know why but pactl does not work in Domoticz.
As a solution: I created a dummy volume selector and use separate volume control. It works!

Example for 70%

Code: Select all

#!/bin/bash
amixer set Master 100% > /dev/null 2>&1 &
amixer set PCM 70% > /dev/null 2>&1 &
amixer set Speaker 100% > /dev/null 2>&1 &
exit 0
Next, I use one of the utilities without the volume key:
for MP3

Code: Select all

#!/bin/bash
mplayer /home/rudder/audio/on.mp3 > /dev/null 2>&1 &
exit 0

for WAV

Code: Select all

#!/bin/bash
aplay /home/rudder/audio/police.wav > /dev/null 2>&1 &
exit 0
So I got what I wanted.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by waaren »

rudder66 wrote: Wednesday 08 January 2020 14:44 ...So I got what I wanted.
Good to read !
Just for my curiosity; can you please try to replace the line

Code: Select all

pactl -- set-sink-volume 0 80%
to

Code: Select all

/bin/bash -c 'pactl -- set-sink-volume 0 80%' 
or

Code: Select all

/bin/bash -c '/usr/bin/pactl -- set-sink-volume 0 80%'  # If that is the path to pactl (check with which pactl) 
and test ?
Thx!
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rudder66
Posts: 12
Joined: Saturday 07 December 2019 20:21
Target OS: Linux
Domoticz version: 4.11553
Location: Minsk, Belarus
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by rudder66 »

It does not work in both cases. I checked all the paths to the utilities - they are correct.
User avatar
Thuis
Posts: 273
Joined: Tuesday 11 September 2018 11:36
Target OS: Linux
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Play sound in Domoticz (Linux/Ubuntu)

Post by Thuis »

Is domoticz user in the audio group?
https://www.freedesktop.org/wiki/Softwa ... fectSetup/

I have a slider switch thingy, and a script that goes like this:
Spoiler: show
commandArray = {}

for deviceName,deviceValue in pairs(devicechanged) do
if (deviceName=='BTVolume') then
local a = (tostring(deviceValue))
if a == "On" or a == 'Off' then
a = 0
end

local percentage = (tonumber(string.match(a,'%d[%d]*')))
cmd = 'pactl set-sink-volume bluez_sink.00_58_xx_xx_xx_xx.a2dp_sink '..percentage..'%' -- send to SPEAKER
os.execute(cmd)
end
end

return commandArray
And this works. I had the same problems, only I use a bluetooth speaker to play music in the bathroom. My only solution was to set pulse as system wide.
I now can change the volume with a button in domoticz. Maybe for a soundcard it is different? My domoticz user is not root.

Systemwide:
https://www.freedesktop.org/wiki/Softwa ... ystemWide/

Also i have a selector switch with mp3 and radio stations to choose from.
Here is an example of radio rijnmond script:
Spoiler: show
1 #!/bin/sh
2 echo quit > /home/userx/domoticz/scripts/mplayer.pipe > /dev/null 2>&1 &
3 killall mplayer > /dev/null 2>&1 &
4 sleep 1
5 Audio1="http://d2e9xgjjdd9cr5.cloudfront.net/au ... ylist.m3u8"
6 /usr/bin/mplayer -ao pulse -noconsolecontrols -slave -volume 80 -input file=/home/userx/domoticz/scripts/mplayer.pipe -playlist $Audio1 > /dev/null 2>&1 &
The volume goes to 80 but i can change it with the slider switch from 0 to 100.

Maybe it is of help to you, just for information.
I Love Domoticz ! And the community around it :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest