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

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

If I understood you correctly you need

Code: Select all

domoticz.executeShellCommand(YOUR COMMAND )
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

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

Post 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
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by paul402 »

Thanks for that. It's so easy when you know!
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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.
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

And what is your Dzvents script?
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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...
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

If you are using "callback" so you have to use
"on = {
shellCommandResponses = { 'mycallbackstring' }"

Just try without callback and timeout - simple string.
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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.
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

Slash befor the "home..."?
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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...
User avatar
gizmocuz
Posts: 2707
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

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

Post 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.")
Quality outlives Quantity!
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

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

Post 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?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
paul402
Posts: 105
Joined: Monday 23 May 2016 23:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
User avatar
waltervl
Posts: 6677
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

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

Post 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?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
habahabahaba
Posts: 266
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

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

Post by habahabahaba »

Yes, show both full scripts please
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest