Play Sound on Rule in OpenHAB 2

I want to play the “doorbell” sound by a self-defined rule (created with the Eclipse Smart Home IDE). I want to play the sound when an Item receives a command.

When I use the Rules Engine via the Paper UI everything works fine, see the screenshots of my rule:

Everytime I send a MQTT message to the specified topic, the sound is played in my browser tab with the OpenHAB UI opened.

However, I tried to “code” this rule also by the Eclipse Smart Home IDE, I used the following code:

rule "TestA"
when Item TestStringItem received command then
	playSound("doorbell.mp3")
end

This seems not to work, the “playSound” command is not known - I get the following error on the code: The method playSound(String) is undefined

How can I use the Audio functions in my rules code?

1 Like

Hi Daniel

I am having the same problem. Did you find a solution?
I am trying to get playStream to work but I get the same error when I run playStream or playSound from a rule file.

Kind Regards
Craig

Post your code, please

Hi

Sorry, after staring at it for a while longer I figured it out. I copied the code from an example online and got the wrong type of quotes.

Incorrect command which doesn’t work:
playSound(“doorbell.mp3”)
Correct command which works:
playSound(“doorbell.mp3”)

Here my working rule:

rule "windowOpenCommand"
when Item Heizung_Bad_Fensterkontakt_FensterZustand changed to OPEN then

	isWindowOpen = true;
	
	while(isWindowOpen == true) {
		Thread::sleep(1000);
		secsWindowOpen = secsWindowOpen + 1;
		
		if(secsWindowOpen >= 600) {
			if(secsWindowOpen % 300 == 0) {
				playSound("RedAlert.mp3");
			}
		}
	}
end

This plays a sound every 10 minutes, if the window is open…
Sound file has to be added to the sounds directory.

I can’t remember if I had to change something else… Let me know if it worked, if not, I will have a detailed look

1 Like

Great to see that it worked also for you! :wink:

While this may be working, this rule bloks a thread for 10 minutes or more if the window stays opened. You only have 5 threads to play with in openhab. If you have 5 of the same rules and you windows are opened, openHAB will stop executing any other rules.

There is a diffrent approach with the expire binding.

  1. install the expire binding
  2. new item:
Switch windowTimer { expire="10m, command=OFF" }
  1. rules
rule "windowOpenCommand"
when
    Item Heizung_Bad_Fensterkontakt_FensterZustand changed to OPEN
then
    windowTimer.sendCommand(ON) // Start timer if window open
end

rule "windowClosedCommand"
when
    Item Heizung_Bad_Fensterkontakt_FensterZustand changed to OPEN
then
    windowTimer.sendCommand(OFF) // Stop timer if window closed
end


rule "window timer"
when
    Item windowTimer received command OFF // Timer ended
then
    if (Heizung_Bad_Fensterkontakt_FensterZustand.state.toString == "OPEN") {
        playSound("RedAlert.mp3")
        windowTimer.sendCommand(ON) // reset timer
    }
end

Good luck

1 Like

gelöscht