Trigger using input_datetime with time offset

I have used [sleep alarm to HA] to create an input.datetime helper in my HA. This all works well.
I wrote this code to activate the wake up light. However the trigger does not like the time offset. I have used various options but all do not work. I would like some help by setting up the trigger properly to the wake up light starts 20’ before the actual alarm.

alias: Wake Up Light
description: Gradually increase light brightness to a defined percentage before wake-up time
trigger:
  - platform: time
    at: >
      {{ (states('input_datetime.yphone_alarm') | as_datetime - timedelta(minutes=20)).time() }}
variables:
  max_brightness: 102 # 40% of 255
action:
  - service: light.turn_on
    target:
      entity_id: light.bedroom_spots
    data:
      brightness: 5
  - delay: "00:00:30"
  - repeat:
      while:
        - condition: template
          value_template: >
            {{ state_attr('light.bedroom_spots', 'brightness') | int < max_brightness }}
      sequence:
        - service: light.turn_on
          target:
            entity_id: light.bedroom_spots
          data:
            brightness: >
              {{ [state_attr('light.bedroom_spots', 'brightness') | int + 5, max_brightness] | min }}
        - delay: "00:00:30"
  - service: logbook.log
    data:
      name: "Wake Up Light"
      message: "Brightness reached {{ state_attr('light.bedroom_spots', 'brightness') }}."

Does this work?

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

Why not use the offset attribute of the trigger. Take a look at the docs.

1 Like

Templates are not supported everywhere. The majority of the different types of triggers and condition do not allow templates.

If you want to use a template like that, to calculate a time for a trigger you need to use a Template trigger (as demonstrated above by Colton). Though I would suggest using the following simpler version:

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

However, as long as your offset is static, you would be better served using the built-in offset attribute (added in 2024.9) that Pete has linked to.

no unfortunately not.

alias: Wake Up Light
description: Gradually increase light brightness to a defined percentage before wake-up time
trigger:
  - platform: time
    at:
      entity_id: input_datetime.yphone_alarm
      offset: "-00:20:00"
1 Like

This does not work because as far as I understand you can not use "at: input_datetime.xxx in combination with offset: xx
This is my not working code:

alias: Wake Up Light
description: >-
  Gradually increase light brightness to a defined percentage before wake-up
  time
triggers:
  - trigger: time
    at: input_datetime.yphone_alarm
      offset: -00:20:00
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

Compare to what I wrote

Ya, that didn’t work for me either, see photo attached, and what DID work.

BUT… This DID!

trigger:
  - platform: template
    value_template: "{{ (as_timestamp(states('input_datetime.yphone_alarm')) - 1200) | timestamp_custom('%H:%M:%S', false) == states('sensor.time').state }}"

Are you running an old version of HA? Working fine over here.

You need to be on 2024.10+

Don’t think so:

  • Core: 2024.11.3
  • Supervisor: 2024.11.4
  • Frontend: 20241106.2

Is that message from the VSCode addon? If yes, the addon is wrong. This is a recent change in HA and works perfectly.

No, it’s in the automation configuration page.

Well somethings wrong with your system. I took the screenshot from the same version. I even worked on that section of code recently. The YAML I provided 100% works.

How would I troubleshoot this then?

PS: here’s screenshots of both your code and mine showing yours giving me that error and mine working fine. I know it’s apples and oranges. I’d like to figure out why yours doesn’t work on my system though…

Well apparently it’s restricted to sensors only, not input_datetimes and sensors. I’ll put in a PR to amend that

Oh, okay. Thank you for doing that! Sorry to make more work for you!

1 Like

I’ve just read up with the threat. This is indeed the problem. The helper input_datetime is not considered a sensor and thus does not work. When can we expect an update of HA?

PRs can take anywhere between 2 and 4 months to complete