state trigger going wrong ... and it was my fault! #227
Unanswered
stefanuytterhoeven
asked this question in
Ideas
Replies: 1 comment
-
Multiple trigger closures can be returned from a single function if they are in a |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
problem: I wanted to check mulitple entities (lights) and notify me when they became 'unavailabe' or when 'unavailable' became 'on' or 'off'
I created an "app", so I could specifty the entities in the 'config.yaml'
I then created a trigger closure (https://hacs-pyscript.readthedocs.io/en/latest/reference.html#trigger-closures)
the trigger condition, I made it a variable.
Like in some examples, they create a "state trigger" for every entity found in the config.yaml for that app.
the condition string would be something like trigger_condition=f"({light} == 'unavailable') or ({light}.old =='unavailable') ", where the {light} is the entity found in config.yaml.
Since for every entity, the exact same thing had to be done; notify me, I thought I would do it differently.
So, in stead of creating a state trigger for every entity , I thought: "I put everything in 1 condition"
What I couldn't do, with trigger closure, create multiple "state triggers" like this:
@state_trigger(f"{trigger_condition1}")
@state_trigger(f"{trigger_condition2}")
I didn't find a way to create a variable amount of state triggers with "trigger closure".
So, I thought, I put it in 1 condition
trigger_condition = "(light.mylight1 == 'unavailable' or light.mylight1.old =='unavailable') or
(light.mylight2 == 'unavailable' or light.mylight2.old =='unavailable') "
@state_trigger(f"{trigger_condition}")
def test_available(......
=>notify here the new and old state
(in case I would specify those 2 lights in the config.yaml.
For a moment, i thought it worked BUT
when light1 changed (e.g. from 'on' to 'off', the state trigger was triggered....
Well, the reason is simple. When the state of light1 changed, the condition was evaluated. My light1 wasn't "unavailable" before or after. It was light2 that was 'unavailable'. Light2 wasn't changed, but because light1 changed state, the condition was evaluated.
The solution was to create a state trigger for every light entity, like you can find in examples.
thoughts: in a trigger, you can see which entitity made the state trigger trigger... by checking var_name.
I don't think you can used "var_name" in the trigger condition. If that was possible, I could have something like
trigger_condition = "((light.mylight1 == 'unavailable' or light.mylight1.old =='unavailable') and VAR_NAME="LIGHT.MYLIGHT1") or
((light.mylight2 == 'unavailable' or light.mylight2.old =='unavailable') and VAR_NAME="LIGHT.MYLIGHT2")
OR, is there another solution?
Beta Was this translation helpful? Give feedback.
All reactions