I am trying to write a rule to turn on/off my home cinema, when one of the two devices that uses it turns ON (e.g. Chromecast or HTPC). Additionally the rule should switch the AVR to the corresponding input - even if one of the devices is already turned on.
Conversely, another rule shuts down the cinema only when, none of these devices is turned on anymore.
I have come up with a setup, where I have the two devices in an OR-group (see below). So if one of them turns on, the group will turn on and trigger the rule to turn on the cinema (and vice-versa for turning it off).
I am now struggling with the part of setting the input of the AVR. For this I somehow need to find out from inside the rule, which device of the group caused the group to change and hence trigger the rule.
I do not know how to achieve this. I have googled a bit, but was unable to come up with a solution.
I found this post that mentions triggeringItem.name
, which seems like what I need and should at some point be introduced for groups too. However, I am unable to figure out if this is already possible for groups and how to use it (seems to me this only works with DSL rules?!).
Additionally, I have a question regarding the rule condition for switching inputs, when the group is already ON:
Right now I am using this:
@when("Item Home_Cinema changed from OFF to ON")
I am guessing I will have to change this to something like:
@when("Item Home_Cinema updated")
This should trigger the rule, when the group is already on and I turn on the second device, so that the rule switches to this device. The would be that the group gets updated to the same value (ON) in this case.
Is this possible?
It seems to me that all of this logic needs to go into a single rule, because of the grouping and because the switching of inputs will only work once the AVR is turned on. But perhaps there is a much cleaner/easier way to achieve this?! If so, I am open for suggestions.
Items:
Group:Switch:OR(ON,OFF) Home_Cinema "Devices using the home cinema."
Switch Chromecast_Livingroom_Idle "Chromecast Livingroom Idle" <network> (gAssistent) { channel="chromecast:chromecast:livingroom:idling" }
Switch pChromecast_Livingroom_Active "Chromecast Livingroom Active" <network> (Home_Cinema) // THIS IS PROXY SWITCH THAT INVERTS Chromecast_Livingroom_Idle
Switch HTPC (Home_Cinema) { channel="network:pingdevice:htpc:online" }
Rule for turning the cinema on:
@rule("Turn on home cinema", description="Turn ON the home cinema", tags=["home_cinema"])
@when("Item Home_Cinema changed from OFF to ON")
def turn_on(event):
turn_on.log.info("Turned ON home cinema.")
turn_on.log.info("Triggering item:" + event.triggeringItem.toString)
sendCommandCheckFirst("projector_screen", "ON")
sendCommandCheckFirst("denon_x3500h_power","ON")
if(event.itemName == "pChromecast_Livingroom_Active"):
sendCommandCheckFirst("denon_x3500h_input","MPLAY")
if(event.itemName == "HTPC"):
sendCommandCheckFirst("denon_x3500h_input","GAME")
Rule for turning the cinema off:
@rule("Turn off home cinema", description="Turn OFF the home cinema", tags=["home_cinema"])
@when("Item Home_Cinema changed from ON to OFF")
def turn_off(event):
turn_off.log.info("Turned OFF home cinema.")
turn_on.log.info("Triggering item:" + event.triggeringItem.toString)
sendCommandCheckFirst("projector_screen", "OFF")
avr_state = getItemValue("denon_x3500h_input","ERROR")
if(avr_state == "ERROR"):
turn_off.log.error("denon_x3500h_input: could not get item value.")
if(avr_state == "MPLAY" or avr_state == "GAME"):
sendCommandCheckFirst("denon_x3500h_power","OFF")
pass
else:
turn_off.log.info("getItemValue('denon_x3500h_input') != 'MPLAY': Not turning off 'denon_x3500h_power'")