Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

state trigger going wrong ... and it was my fault! #227

Unanswered
stefanuytterhoeven asked this question in Ideas
Discussion options

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?

You must be logged in to vote

Replies: 1 comment

Comment options

Multiple trigger closures can be returned from a single function if they are in a List or something similar.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /