Pausing Multimedia when Phone rings with saving previous Player state

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:

Define a variable on top of your rule…

var wasPlaying = true

Which will be your flag. Turn this true/false when needed.

Seems like an easy solution. I am having trouple transferring that to my rule.

var wasPlaying = true

how do I get that connected to the player state?

here is an example:

// 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

what’s wrong here? thanks guys. Feeling quite stupid

needs to read

if (wasPlaying==true)

you have a dangling “or” here, would suggest to delete.

you may need to specify the type

var boolean wasPlaying

Sorry missed that first time, another dangling “or” just delete if you have no other condition

again apologies for the piecemeal, but these two rules trigger on different items, is this intentional?

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

@soesas this is not the most intuitive thing in OH2; “PLAY” refers to a string and PLAY is an item state
see here for more discussion on state: https://docs.openhab.org/concepts/items.html
So if you compare item states if (item==PLAY) you refer to an item state.
Your command `sendCommand(“item”,“command”) needs two strings as argument, hence the quotation marks. This is explained in more detail here: https://docs.openhab.org/configuration/rules-dsl.html#manipulating-item-states

I surely need to catch up on my reading :slight_smile: