Page 1 of 1

Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Friday 14 November 2025 17:52
by paul402
I have a warning script when the bathroom roof window is open for more than 30 minutes and when the outside temperature is less than 17C it sends a Telegram message. I have an amp and speaker attached to the Raspi but haven't worked out yet how to play an audio file with Dzvents. The file works fine in a terminal and from Thonny.
I couldn't see anything in the Dzvents help files. Did I miss it?
Would appreciate a link or tip to get this working.
Cheers

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Friday 14 November 2025 19:39
by habahabahaba
If I understood you correctly you need

Code: Select all

domoticz.executeShellCommand(YOUR COMMAND )

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Friday 14 November 2025 23:23
by waltervl
And make sure the command has an absolute path so some like /home/pi/scripts/yourbash.sh

You can also test it by running the command with sudo as that is what Domoticz is doing

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Friday 14 November 2025 23:34
by paul402
Thanks for that. It's so easy when you know!

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Saturday 22 November 2025 22:03
by paul402
Well it didn't end up being easy!
This audio file in the /scripts directory
sh01CheckFridgeDoor.mp3

This python file in /scripts with all permissions

Code: Select all

#!/usr/bin/python3
import sys
sys.path.append('/home/pi/domoticz/scripts/python')
# to check path is updated
print (sys.path)
### File name here
af='/home/pi/domoticz/scripts/python/sh01CheckFridgeDoor.mp3'

import pygame
pygame.mixer.init()
pygame.mixer.music.load(af)
pygame.mixer.music.play()
Plays perfect in Thonny.

CMD Box

Code: Select all

sudo/without sudo python/python3 /home/pi/domoticz/scripts/python/01pythonPlayAudioCheckFridgeDoor.py
Runs without error but no sound

Create a Domoticz script to run every minute.
Runs perfect without Domoticz errors but no sound.

I'm obviously doing something wrong but what?
It's driving me crazy.

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Saturday 22 November 2025 23:54
by habahabahaba
And what is your Dzvents script?

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 23 November 2025 15:42
by paul402

Code: Select all

return {
    active = true,
    
    logging = {
            level = domoticz.LOG_DEBUG,
            marker = 'audio output' 
            },
        
    on = {
        timer = {'Every minute'}
    },

    
        execute = function(domoticz)

            local cde = "home/pi/Documents/01pythonPlayAudioCheckFridgeDoor.py"
			domoticz.executeShellCommand({
	      			command = cde,
	      			callback = "myTrigger",
				timeout = 2,
	  		}
  		
  		)
                
     
    end
}
and no errors...

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 23 November 2025 18:35
by habahabahaba
If you are using "callback" so you have to use
"on = {
shellCommandResponses = { 'mycallbackstring' }"

Just try without callback and timeout - simple string.

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 23 November 2025 18:37
by habahabahaba

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 23 November 2025 22:36
by paul402
I guess you mean this format domoticz.executeShellCommand('some shell command')
So I should use
domoticz.executeShellCommand('home/pi/Documents/01pythonPlayAudioCheckFridgeDoor.py')

which executes without error but without sound. I think that it's time for bed. Enough for tonight.

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Monday 24 November 2025 5:54
by habahabahaba
Slash befor the "home..."?

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Monday 24 November 2025 11:26
by paul402
I eventually found a solution on stackexchange....
So you need to add this after the play() command then it works from the cli and Domoticz.

Code: Select all

while pygame.mixer.music.get_busy() == True:
            pass
            
Who knows why!.... but I've wasted a lot of time on this.
I'm falling out of love with Raspis and its OS and dependencies. Designed by sadists for masochists...

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Tuesday 25 November 2025 8:48
by gizmocuz
Raspbian is based on Debian which is a great OS.
Your issue was in a Python script, so there is no need to shout on Debian
Sure there are other ways to play this MP3 file.
Could be that dzVents/Domoticz now has to wait until the MP3 is played... which is not desired, better to play this in the background

You can also use mpg123 and play it in the background

Code: Select all

import subprocess

subprocess.Popen(["mpg123", "music.mp3"])
print("Started in background.")

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Tuesday 25 November 2025 9:56
by waltervl
gizmocuz wrote: Tuesday 25 November 2025 8:48...
Could be that dzVents/Domoticz now has to wait until the MP3 is played... which is not desired, better to play this in the background

You can also use mpg123 and play it in the background

Code: Select all

import subprocess

subprocess.Popen(["mpg123", "music.mp3"])
print("Started in background.")
domoticz.executeShellCommand is running the started proces/python script in async. So why in the Python script a subprocess?

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 30 November 2025 11:14
by paul402
Well blow me down! That worked on both my Domoticz Pi3 and Pi4 installations. Thank you.
I only had to add the Python exit() command to the script.
Thanks

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Sunday 30 November 2025 15:35
by waltervl
paul402 wrote: Sunday 30 November 2025 11:14 Well blow me down! That worked on both my Domoticz Pi3 and Pi4 installations. Thank you.
I only had to add the Python exit() command to the script.
Thanks
What worked? What solution did you end up with?

Re: Easiest way to play a mp3 audio file on Raspi with Dzvents?

Posted: Monday 01 December 2025 8:28
by habahabahaba
Yes, show both full scripts please