-
Notifications
You must be signed in to change notification settings - Fork 58
state trigger problem. #231
-
I would like to use this in a state trigger:
@state_trigger("(light.spot1=='unavailable' or light.spot1.old=='unavailable') or (light.spot2=='unavailable' or light.spot2.old=='unavailable') ")
def test_spot(trigger_type=None, var_name=None, value=None, old_value=None):
notify.....
At first sight,this should work. If spot1 or spot2 becomes "unavailable", or was "unavailable" and becomes 'on' or 'off',
the trigger should go off.
Test;
Initial state:
spot2 is 'unavailable'
spot1 is 'on'
Test:
=> I turn spot1 'off
Result:
trigger is triggered. Probably, because light.spot1 is "watched" by the trigger and something happened with the state.
Then the condition is evaluated. Since spot2 (!) is 'unavailable' , the trigger is triggered....
var_name='light.spot1'
value='off'
old_value='on'
So, the result is not what i want.
What I need, is that I can use 'var_name' in the trigger condition. (or something similar)
Then I could write
@state_trigger("((light.spot1=='unavailable' or light.spot1.old=='unavailable') and var_name=='light.spot1') or ((light.spot2=='unavailable' or light.spot2.old=='unavailable') and var_name=='light.spot2') ")
def test_spot(trigger_type=None, var_name=None, value=None, old_value=None):
notify.....
this should solve my problem....
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 1 reply
-
Other possibility is using closures:
light_unavailable_triggers = {}
def light_unavailable(light_id):
@state_trigger(f"{light_id} == 'unavailable'", f"{light_id}.old == 'unavailable'")
def light_unavailable_trigger(var_name=None, value=None, old_value=None):
....
....
light_unavailable_triggers[light_id] = light_unavailable_trigger
light_unavailable('light.spot1')
light_unavailable('light.spot2')
Beta Was this translation helpful? Give feedback.
All reactions
-
yes, I can do it like that. But, I wanted to make 1 conditionstring. I could create closures (I did it already), then I would have 20 triggers for 20 lights, while I wanted 1 trigger with one (long) conditionstring.
I now found a solution. In stead of using a state_trigger, I used an event_trigger:
@event_trigger(EVENT_STATE_CHANGED,trigger_condition)
def test_lights_available(entity_id=None, new_state=None, old_state=None):
In the trigger condition you can used "entity_id", so my problem is solved like that....
Beta Was this translation helpful? Give feedback.
All reactions
-
@state_active
may also be useful to you.
Also, it is incredibly easy (and equally as performant) to write multiple triggers that all call some helper function.
def doStuff(): log.info('stuff') @state_trigger("input_boolean.test_1 == 'unavailable' or input_boolean.test_1.old == 'unavailable'") def one(): doStuff() @state_trigger("input_boolean.test_2 == 'unavailable' or input_boolean.test_2.old == 'unavailable'") def one(): doStuff()
Also, your enclosures in your trigger condition don't really add anything here, so I'm not if that's working as you expect.
True or True or True or True # same thing as (True or True) or (True or True)
Beta Was this translation helpful? Give feedback.