-
Notifications
You must be signed in to change notification settings - Fork 58
alarm clock based on input_datetime #547
-
I am trying to make a light to switch on based on the time stored in a input_datetime helper. I came up with below code, but it never triggers at the time in the input_datetime helper.
What am I doing wrong?
@state_trigger("input_datetime.alarm_time")
@time_trigger("once(now)")
def update_alarm():
log.info(f"time has changed, updating alarm to {input_datetime.alarm_time}")
@time_trigger(f"once({input_datetime.alarm_time})")
def alarm():
log.info("New alarm time set")
light.turn_on("light.someroom")
Beta Was this translation helpful? Give feedback.
All reactions
First, have you confirmed the string f"once({input_datetime.alarm_time})"
produces a correctly formatted time?
The main problem is that the alarm()
function is destroyed as soon as update_alarm()
returns, since there is no longer any reference to it in any scope, so there will no longer be any trigger. See the docs. You need to keep some reference to the closure function so it remains defined after the enclosing function returns, eg, by setting a global variable.
Replies: 1 comment 1 reply
-
First, have you confirmed the string f"once({input_datetime.alarm_time})"
produces a correctly formatted time?
The main problem is that the alarm()
function is destroyed as soon as update_alarm()
returns, since there is no longer any reference to it in any scope, so there will no longer be any trigger. See the docs. You need to keep some reference to the closure function so it remains defined after the enclosing function returns, eg, by setting a global variable.
Beta Was this translation helpful? Give feedback.
All reactions
-
that makes sense, thanks for explaining.
Beta Was this translation helpful? Give feedback.