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

Are dynamic @state_trigger's possible? #273

Unanswered
UnbridledGames asked this question in Q&A
Discussion options

I'm trying to write a script that will upon startup find all door/window sensors, (and notice when new ones are added, both these functions I have written already), and then set up a @state_trigger for each. Is there a way to define these dynamically? Poking around I've found that I can do an event trigger on state_change, but there are so many of those happening all the time I feel like thats inefficient. Or, is that what a @state_trigger does internally anyway? If so I'll just go that route.

You must be logged in to vote

Replies: 1 comment

Comment options

If I understand your question, I think what you are (probably) looking for is done here; Basically, write the function you want you want to apply to each door/window and wrap that in another function which takes a single door/window as an argument and dynamically generates the function with appropriate decorators. That should work as long as your function doesn't need to coordinate between the different entities.

Another option is using string formatting along these lines:
doors_and_windows = [binary_sensor.first, binary_sensor.second]
@state_trigger('True or {}'.format(" or ".join(doors_and_windows)))

I would lean toward the first, but take all of this with a grain of salt as I'm very new to pyscript and just trying to sort a lot of this out for myself. Hope it helps!

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
Q&A
Labels
None yet

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