Trigger using input_datetime with time offset

Not willing to wait for the update from HA I hava tried idea. However it doesn’t seem to work for me. I’ve added a printscreen of my log files where basically nothing happens when the set time is reached.

This is now my code (same as yours)

alias: Wake Up Light
description: >-
  Gradually increase light brightness to a defined percentage before wake-up
  time
triggers:
  - value_template: >-
      {{ (as_timestamp(states('input_datetime.yphone_alarm')) - 1200) |
      timestamp_custom('%H:%M:%S', false) == states('sensor.time').state }}
    trigger: template
actions:
  - target:
      entity_id: light.bedroom_spots
    data:
      brightness: 5
    action: light.turn_on
  - delay: "00:00:30"
  - repeat:
      while:
        - condition: template
          value_template: >
            {{ state_attr('light.bedroom_spots', 'brightness') | int <
            max_brightness }}
      sequence:
        - target:
            entity_id: light.bedroom_spots
          data:
            brightness: >
              {{ [state_attr('light.bedroom_spots', 'brightness') | int + 5,
              max_brightness] | min }}
          action: light.turn_on
        - delay: "00:00:30"
  - data:
      name: Wake Up Light
      message: >-
        Brightness reached {{ state_attr('light.bedroom_spots', 'brightness')
        }}.
    action: logbook.log
variables:
  max_brightness: 102

That template requires extra sensors.

If you just want it to trigger 20 minutes before the datetime without needing the sensor.time sensor…

  - value_template: >-
      {{ states('input_datetime.yphone_alarm') | as_datetime | as_local - timedelta(minutes=20) >= now().replace(microsecond=0) }}

In your screen print of the yaml, my entity ids are present. But in your yaml it’s yours. So can you double check the automation now?

Yes I checked. Basically:
yphone alarm date and time: input_datetime.yphone_alarm
lights: light.bedroom_spots

Do I have to setup a sensor for ‘sensor.time’? No idea how I should do that.

In order for me to test, I created a Helper, and selected Date and Time Helper (there are helpers for date and a helper for time separately but I chose the combined one)

I have tried this. I am not sure what happens.
It triggered the automation directly after I changed the time to current time +21 min. This happened twice but not always.
The automation was never triggered when the time was changed to more than 21 min in the future and the time -20 min was reached.

What are you using for your alarm? That piece I don’t have. Can you share it?

These triggers only execute once a minute on the minute, and you have to wait for the template to return to false before it can trigger again.

This is the nature of template triggers.

If you want this to trigger 2 consecutive minutes, you cannot use a template trigger.

Seeing that this is such a hassle, I suggest you make a timestamp template sensor. Use the following template in a template sensor through the UI.

{{ states('input_datetime.yphone_alarm') | as_datetime | as_local - timedelta(minutes=20) }}

Then use the created entity in a time trigger.

  - platform: time
    at:
      entity_id: sensor.<your_template_sensor>
      offset: "-00:20:00"

Excuse me if this is a stupid question I have just started working with HA and yaml coding. I think I understand the idea. However how do I make a timestamp template sensor? According to Chat GPT I have to change the configuration.yaml code using the add-on file editor. Is that right?

Go to the helpers page, add a template helper, choose sensor, choose display as timestamp

All through the ui

As a sidebar, AI frequently confidently lies in regards to HA

Very true.

Okay I’m getting there. I have now got a sensor. giving the time until the alarm should be triggered. Wouldn’t it be clearer if the sensor just copies the time from the alarm so I can then use the code suggested by you earlier?

Thanks a lot so far!

Yeah sure

I have a similar use case. I have an input_datetime with time only, that specifies the time for the house to wakeup. So for example a value of “06:00:00” would indicate a wakeup time of 6 AM.

  hvac_wakeup_time:
    name: Wakeup Time
    has_date: false
    has_time: true  

This worked fine as an automation trigger when done like this:

  - triggers:
      - trigger: time
        at: input_datetime.hvac_wakeup_time

Then i decided I really needed to use offsets. And so I attempted using the input_datetime with an offset, which is not supported. So I created a template sensor and that did not work either. It turns out that the time trigger requires the sensor to have a device_class of timestamp and if it doesn’t have the device_class it silently ignore the trigger :-(. Because it’s a timestamp it needs to be a fully formed ISO timestamp e.g. (2024-12-16T12:00:00+00:00)

So to go from the input_datetime with time only to trigger an alarm for tomorrow morning, i go to this solution. Note, if you want the alarm to trigger every day; you’ll want to have the sensor recalculate at 23:59 (I don’t need that since I have an automation that sets the input_datetime every day)

template:
  - trigger:
      - platform: state
        entity_id: input_datetime.hvac_wakeup_time
    sensor:
      - name: "hvac_wakeup_time"
        state: >-
          {% set input_time = states("input_datetime.hvac_wakeup_time") %}
          {% set tomorrow = (now() | as_timestamp + 86400) | timestamp_custom("%Y.%m.%d", True) %}
          {% set time_str= tomorrow + " " + input_time %}
          {% set time = strptime(time_str.strip(),"%Y.%m.%d %H:%M:%S") %}
          {{ (time | as_local).isoformat() }} 
        device_class: timestamp