# Pausing Multimedia when Phone rings with saving previous Player state

**URL:** <https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081>\
**Category:** Beginners\
**Created:** [January 21, 2018, 6:27pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081 "2018-01-21T18:27:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [January 21, 2018, 6:27pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/1 "2018-01-21T18:27:27Z")

</div>

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:

```php
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.

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [January 21, 2018, 6:33pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/2 "2018-01-21T18:33:21Z")

</div>

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:

> [@Design Pattern: Proxy Item](https://community.openhab.org/t/design-pattern-proxy-item/15991):
>
> Edit: Updates for OH 4 Please see [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for information on what a DP is and how to use them. Problem Statement Often one faces one or more of the following situations: perform additional logic before sending a command to a device (e.g. make sure the device can safely receive the command) send a command to multiple devices based on a single event and Groups, the follow profile, or linking multiple Channels to one Item is not feasible t…

---

<div class="post-metadata">

**Author:** ![luckymallari](https://community.openhab.org/user_avatar/community.openhab.org/luckymallari/32/15259_2.png) [@luckymallari](https://community.openhab.org/u/luckymallari)\
**Post date:** [January 21, 2018, 6:42pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/3 "2018-01-21T18:42:35Z")

</div>

Define a variable on top of your rule..

```auto
var wasPlaying = true

```

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

---

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [January 21, 2018, 6:51pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/4 "2018-01-21T18:51:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![miker](https://community.openhab.org/user_avatar/community.openhab.org/miker/32/12615_2.png) [@miker](https://community.openhab.org/u/miker)\
**Post date:** [January 21, 2018, 6:59pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/5 "2018-01-21T18:59:28Z")

</div>

here is an example:

```auto
// 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
}

```

---

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [January 21, 2018, 7:19pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/6 "2018-01-21T19:19:09Z")

</div>

I tried it using variables (which I have never done before) but absolutely no luck.

```php
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

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [January 21, 2018, 8:52pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/7 "2018-01-21T20:52:58Z")

</div>

> [@soesas](#):
>
> if (wasPlaying=true)

needs to read

```auto
if (wasPlaying==true)

```

> [@soesas](#):
>
> Item fboxRinging\_Out changed to OFF or

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

> [@soesas](#):
>
> var wasPlaying

you may need to specify the type

```auto
var boolean wasPlaying

```

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [January 21, 2018, 8:54pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/8 "2018-01-21T20:54:50Z")

</div>

> [@soesas](#):
>
> Item fboxRinging changed to ON or

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

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [January 21, 2018, 8:56pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/9 "2018-01-21T20:56:49Z")

</div>

> [@soesas](#):
>
> rule “pause when phone rings”  
> when  
> Item fboxRinging changed to ON or  
> …  
> rule “play after hanging up phone”  
> when  
> Item fboxRinging\_Out changed to OFF or  
> then

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

---

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [January 21, 2018, 10:15pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/10 "2018-01-21T22:15:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [January 22, 2018, 8:38am UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/11 "2018-01-22T08:38:12Z")

</div>

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.

```php
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

```

---

<div class="post-metadata">

**Author:** ![lipp\_markus](https://community.openhab.org/user_avatar/community.openhab.org/lipp_markus/32/56218_2.png) [@lipp\_markus](https://community.openhab.org/u/lipp_markus)\
**Post date:** [January 22, 2018, 5:45pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/12 "2018-01-22T17:45:37Z")

</div>

> [@soesas](#):
>
> I am just confused about the different formatting in the rule. “PLAY” and ==PLAY.

@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](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](https://docs.openhab.org/configuration/rules-dsl.html#manipulating-item-states)

---

<div class="post-metadata">

**Author:** ![soesas](https://community.openhab.org/user_avatar/community.openhab.org/soesas/32/16911_2.png) [@soesas](https://community.openhab.org/u/soesas)\
**Post date:** [February 2, 2018, 10:26pm UTC](https://community.openhab.org/t/pausing-multimedia-when-phone-rings-with-saving-previous-player-state/39081/13 "2018-02-02T22:26:54Z")

</div>

I surely need to catch up on my reading 🙂
