Using an input_datetime sensor offset value in an automation

I am wanting to use the offset value of an input_datetime sensor in an automation as a trigger. Essentially, I have a light that is turned on by input_datetime.night_light_on_weekday, and then 20 minutes after that, I want a different automation to run. The input_datetime.night_light_on_weekday may be manually changed from time to time, so I need to use this as the reference point to my automation offset.

WIth a template, it would appear that I can modify the time so that it will run 20 minutes after with this:

{{ as_timedelta(states('input_datetime.night_light_on_weekday')) + timedelta ( minutes = 20 ) }}

But when I do that, the automation refuses to acknowledge that this timestamp ever exists, and fails to trigger the automation. Nothing in the automation trace logs, and nothing in the system log. What’s the best way to accomplish this?

Post the automation or at least the part where it contains your template.

It’s extremely basic, but here you go!

alias: Kitchen Lights Morning
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ as_timedelta(states('input_datetime.night_light_on_weekday')) +
      timedelta ( minutes = 20 ) }}
condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
action:
  - service: light.turn_on
    data:
      brightness_pct: 15
    target:
      entity_id:
        - light.kitchen
        - light.breakfast_room
        - light.island
mode: single

Is the Input Datetime configured to store only time or date and time?

If it’s only time then I suggest you try the following Template Trigger:

trigger:
  - platform: template
    value_template: >-
      {{ (now() - timedelta(minutes=20)).strftime('%H:%M:00') == 
          states('input_datetime.night_light_on_weekday') }}

The template subtracts 20 minutes from the current date and time and reports it as a time string in HH:MM:00 format. It then compares the time string to the state value of the Input Datetime which is also a time string in HH:MM:00 format.

If the two time strings are identical, the Template Trigger is triggered.

1 Like

The input_datetime is set as time only, no date.

This solution appears to work fine, though it is a pretty wild way to accomplish what seems like a relative straightforward task. Thank you!

1 Like

It would be straightforward if there was no need to offset the Input Datetime’s value. Then you could use a Time Trigger like this:

  trigger:
    - platform: time
      at: input_datetime.night_light_on_weekday

An alternative to the Template Trigger I suggested is to create a Template Sensor, whose device_class is timestamp, that subtracts 20 minutes from the Input Datetime.

template:
  - sensor:
      - name: Offset Night Light on Weekday
        device_class: timestamp
        state: >
          {{ (today_at(states('input_datetime.night_light_on_weekday')) - timedelta(minutes=20)).isoformat() }}

Then the automation’s Time Trigger can simply be this:

  trigger:
    - platform: time
      at: sensor.offset_night_light_on_weekday

Interesting concept with the template sensor - I hadn’t thought of doing it that way. Either way, I do appreciate all the help!