Hello there. Another problem that keeps me up at nights.
I created a rule which turns my Music to Pause whenever the Phone rings (used hardware is volumio and a Fritzphone - both included with the appropriate bindings).
Here is my code:
rule "Turn off Volumio when Phone is ringing"
when
Item fboxRinging changed to ON or
Item fboxRinging_Out changed to ON
then
sendCommand(volumio2_player_volumio_player, "PAUSE")
end
rule "play after hanging up phone"
when
Item fboxRinging_Out changed to OFF or
Item fboxRinging changed to OFF or
then
sendCommand(volumio2_player_volumio_player, "PLAY")
end
Here’s the thing: I only want openhab to sendCommand “PLAY” after Phonecall ended if the audio was in state PLAY before the phone rang. How do I achieve that? Thank you guys.
You could define a proxy item that saves the state of your player (and is updated) and work with the state of this proxy, here is a fantastic write-up for proxy items:
// if light level < less than 175 lumens, do something...
// using AEON Light sensor value... ie. <ItemName>.state to get value
if (AEONMultiSensor_SensorLuminance.state < 175) {
variable = 1
}
I tried it using variables (which I have never done before) but absolutely no luck.
var wasPlaying
rule "pause when phone rings"
when
Item fboxRinging changed to ON
then
if (volumio2_player_volumio_player.state == "PLAY"){
wasPlaying=true
}
else {wasPlaying=false}
sendCommand(volumio2_player_volumio_player, "PAUSE")
end
rule "play after hanging up phone"
when
Item fboxRinging changed to OFF
then
if (wasPlaying=true)
{
sendCommand(volumio2_player_volumio_player, "PLAY")
}
end
Hey Sorry, my mistake - I tried to shorten the code. It has multiple triggers (incoming, outgoing call and turning on the TV). I changed the code in my thread.
Cool. @lipp_markus appreciate your help. It now works beautifully. I am just confused about the different formatting in the rule. “PLAY” and ==PLAY. Try and Error helped me out.
Here is the finished code in case anybody else wonders.
var boolean wasPlaying
rule "pause when phone rings"
when
Item (...) changed from OFF to ON
then
if (*player.state==PLAY){
wasPlaying=true
logInfo("File", "Player WAS playing")
}
else {wasPlaying=false
logInfo("File","Player was not playing")
}
sendCommand(*player, "PAUSE")
end
rule "play after hanging up phone"
when
Item (...) changed from ON to OFF
then
if (wasPlaying==true)
{
sendCommand(*player,"PLAY")
logInfo("File", "Phone hung up. Continue Play")
}
end