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

alarm clock based on input_datetime #547

Answered by craigbarratt
phsdv asked this question in Q&A
Discussion options

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")
You must be logged in to vote

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

Comment options

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.

You must be logged in to vote
1 reply
Comment options

that makes sense, thanks for explaining.

Answer selected by phsdv
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 によって変換されたページ (->オリジナル) /