How to check for a condition in a triggered function? #360
-
I have a function that is called from triggers in several places: def volets_time(when="", entity_id=None, howMuch=None, condition="on"):
log.info(f"setting up volets for {entity_id} at {when} → {howMuch}")
@time_trigger(f"once({when})")
def volets():
if condition == "on":
cover.set_cover_position(entity_id=entity_id, position=howMuch)
log.info(f"volets {entity_id} → {howMuch}")
else:
log.info(
f"volets {entity_id} should go {howMuch}, but automation is switched off"
)
return volets It does not really matter what it does, the key point is the condition The problem of course is that I would need to pass the binary switch to th function, and not only its current state, as I do it incorrectly now: triggers = [
volets.volets_time(
entity_id="cover.martin_volets",
howMuch="100",
when="11:00",
condition=input_boolean.martin_volets_matin,
),
volets.volets_time(
entity_id="cover.martin_volets",
howMuch="0",
when="sunset+1h",
condition=input_boolean.martin_volets_soir,
),
] What would be the correct way to pass (for instance) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It seems you need to defer the fetching of the state variable value from trigger creation to trigger running. The easiest way would be to make |
Beta Was this translation helpful? Give feedback.
It seems you need to defer the fetching of the state variable value from trigger creation to trigger running. The easiest way would be to make
condition
a string (eg,"input_boolean.martin_volets_soir"
), and usestate.get()
inside the inner trigger function to get its current value (ie, replacecondition
withstate.get(condition)
).