Using a input_datetime in Trigger (time)

Hi All, I’ve done some “googling” on this and haven’t found a clear answer. My current config turns some lights out at “bedtime”, but the time is hard-coded in the automation:

 alias: Set Kids Bedtime Flag
  trigger:
  - at: '19:30'
    platform: time
  condition: []
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.is_bedtime

The boolean change triggers the actual lights off and allows us to manually trigger the “bedtime” routine / scene also. BUT, I would like the “at” time to be an input_datetime also. I have tried a few options but no real success, I’ve seen “templates” used to access vars, but hear also they have a performance impact.

Is there an easy way to reference the “input_datetime.xxxx” value in the "at: " part of the condition.

Thanks in advance,

2 Likes

The problem with using an input_datetime to store only a time is that it has no date when you compare it with now(). So you have to add today’s date back in before comparing them as timestamps.
.
Much kudos goes to petro for helping me work this out.

(d is the date now as a timestamp, t is the time and date now as a timestamp).

  trigger:
    platform: template
    value_template: >-
      {% set d = now().strftime("%Y-%m-%d ") %}
      {% set t = now().timestamp() %}
      {% set am_start = strptime(d + states('input_datetime.rumpus_ac_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
      {{ am_start <= t  }}

I’m not sure this is going to work since this template trigger will only get evaluated once at startup, and then again only when input_datetime.rumpus_ac_am_on_time changes. (Remember, templates aren’t evaluated just when now() changes anymore.)

Assuming the input_datetime being used just has time (and not date), then I’d recommend something like this:

- alias: Set Kids Bedtime Flag
  trigger:
    platform: template
    value_template: >
      {{ states('input_datetime.XXX').rsplit(':',1)[0] == states('sensor.time') }}
  condition: []
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.is_bedtime

This template trigger will be re-evaluated whenever either input_datetime.XXX changes or sensor.time changes, and the automation itself will be triggered whenever the result of the template changes from False to True. Of course, you’ll also need to enable sensor.time.

You’re right. I actually have that template in a template binary sensor with the time sensor entity id declared so it updates. I trigger off the binary sensor.

1 Like

Thanks for the responses - appreciate.

Seems a shame we have to go down a template path vs. a straight var reference. Honestly, it’s seems like a pretty poor limitation with HA. (I’m quite new to HA)

Do I recall reading there’s performance / processing impact to taking this approach?

2 Likes

For anyone finding this via search, to trigger time comparisons via a template you need

entity_id: sensor.time

in the Binary Sensor, so the comparison is triggered. Took me quite a while to find this…