Simplify my rule

Hi Guys

This rule works very well, but im sure I can simplify it.


rule "Mute the music if a Call is received"
when
        Item Mute_Music received command ON
then
   //Mute Music if Playing
    if (Main_Zone_Power == ON && spotify_current_playing == ON || Main_Zone_Power == ON && myKodi_control == PLAY ) {
        Zone_1_Mute.sendCommand("ON")
        logInfo(scene, "Music Muted due to Incoming Call")
}
end


rule "Un Mute the music when the call is Idle"
when
        Item Mute_Music received command OFF
then
   //Un Mute Music if Playing
    if (Main_Zone_Power == ON && spotify_current_playing == ON || Main_Zone_Power == ON && myKodi_control == PLAY ) {
        Zone_1_Mute.sendCommand("OFF")
        logInfo(scene, "Music Un Muted due to Call Hang Up")
}
end

Can I use a variant using ‘ELSE’ somewhere or do I need two rules because I have an item on and off state?

Like so? I guess this wouldnt work, because the Mute needs to be off, only if the Mute_Music item is OFF


rule "Mute the music if a Call is received"
when
        Item Mute_Music received command ON
then
   //Mute Music if Playing
    if (Main_Zone_Power == ON && spotify_current_playing == ON || Main_Zone_Power == ON && myKodi_control == PLAY ) {
        Zone_1_Mute.sendCommand("ON")
        logInfo(scene, "Music Muted due to Incoming Call")
     else
        Zone_1_Mute.sendCommand("OFF")
}
end

I think this should work:

rule "Mute the music if a Call is received"
when
        Item Mute_Music received command
then
	if (Main_Zone_Power == ON && spotify_current_playing == ON || Main_Zone_Power == ON && myKodi_control == PLAY ) {
		if (receivedCommand == ON) { 
			Zone_1_Mute.sendCommand("ON")
			logInfo(scene, "Music Muted due to Incoming Call")
		} else if (receivedCommand == OFF) {
			Zone_1_Mute.sendCommand("ON")
	        logInfo(scene, "Music Un Muted due to Call Hang Up")
		}
	}

You could shorten it more if you skip the logInfo.

rule "Mute the music if a Call is received"
when
        Item Mute_Music received command
then
	if (Main_Zone_Power == ON && spotify_current_playing == ON || Main_Zone_Power == ON && myKodi_control == PLAY ) {
			Zone_1_Mute.sendCommand(receivedCommand.toString)
	}
end
1 Like

Interesting! Ill give this a go… thank you! :slight_smile:

I’m pretty sure that your rule won’t work, as you didn’t use the state.
The rule should be more like:

rule "Mute and unmute the music in case of a Call"
when
    Item Mute_Music received command
then
    if (Main_Zone_Power.state == ON && (spotify_current_playing.state == ON || myKodi_control.state == PLAY )) {
        Zone_1_Mute.sendCommand(receivedCommand)
        logInfo("mute",if(receivedCommand == ON) "Music muted due to Incoming Call" else "Music unmuted due to Call Hang Up")
	}
end

In question of logging… this:

        logInfo("mute",if(receivedCommand == ON) "Music muted due to Incoming Call" else "Music unmuted 

is the same as

        if(receivedCommand == ON)
            logInfo("mute","Music muted due to Incoming Call")
        else 
            logInfo("mute","Music unmuted due to Call Hang Up")

but it’s shorter :wink:

Wow, I get blown away each time I see how powerful rules can be. Thanks Udo & Alpoy :sunglasses: