Input_datetime Offset

I’ve set up an input_datetime to trigger my alarm clock. I’d like to have an offset of 2 minutes before the alarm clock rings to turn slowliy fade up the light. How can I implement the offset? So far I’m using this to trigger the alarm:

trigger:
- platform: template
  value_template: "{{ states('sensor.time') == (states.input_datetime.alarm_clock.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

Try this:

trigger:
  platform: template
  value_template: '{{ ((now().strftime("%s") | int + 120) | timestamp_custom("%H:%M:%S")) == states.input_datetime.alarm_clock.state }}'

The first part gets the time two minutes from now. Then it’s comparing it to the input_datetime. When they equal each other it’s two minutes before your alarm time.

3 Likes

Where do I have to place the offset part in my given example with sensor.time and the comparison to states.input_datetime.alarm_clock.attributes.timestamp? Can’t get it to work.

You can just replace your value_template in your trigger with the one I provided. It should work as is.

Your template won’t work for this because you need to convert the current time to seconds before adding 120 to it. Sensor.time is a string so it can’t be converted to seconds. Now() gets you the current time as a datetime object so you can convert it to seconds using strftime.

1 Like

FYI, this would also work instead of using strftime:

trigger:
  platform: template
  value_template: '{{ ((as_timestamp(now()) + 120) | timestamp_custom("%H:%M:%S")) == states.input_datetime.alarm_clock.state }}'
1 Like

Strangely all variants stopped working for me. I’m currently on v0.69:

- platform: template
   value_template: '{{ ((now().strftime("%s") | int - 120) | timestamp_custom("%H:%M:%S")) == states.input_datetime.alarm_clock.state }}'

- platform. template
  value_template: '{{ ((as_timestamp(now()) - 120) | timestamp_custom("%H:%M:%S")) == states.input_datetime.alarm_clock.state }}'

- platform: template
  value_template: "{{ states('sensor.time') == (states.input_datetime.alarm_clock.attributes.timestamp | int - 120 | timestamp_custom('%H:%M', False)) }}"

Can someone verify?

Not running 69 yet. What are the errors in the log?

I dont’t see something suspicious in the logs. The automation simply doesn’t trigger.

Ah, so that is an automation trigger.

For shits and giggles, copy that code into a sensor template and watch whether the template turns true or false. I’m wondering if templates are broken in triggers or if now() is broken everywhere. Also, have you looked at the release notes and checked to see if there are any breaking changes?

The other option is to toss that into the template editor and verify that it works as expected as well.

It works as expected in template editor but it is not triggering. Have you tried it, petro?

I haven’t tried it in an automation. The only way these would trigger in an automation is based off sensors updating. using now() will not cause triggers to occur when now() changes, but using sensor.time should. I would expect the last variation you posted to trigger the most.

I think I’ve found the mistake. The last one does not accept the offset - at least not the way I’ve used it :confused:

This one triggers:

- platform: template
  value_template: "{{ states('sensor.time') == (states.input_datetime.alarm_clock.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}"

This one (the one I want) does not trigger:

- platform: template
  value_template: "{{ states('sensor.time') == (states.input_datetime.alarm_clock.attributes.timestamp | int - 120 | timestamp_custom('%H:%M', False)) }}"

Do you have an idea why the offset (int - 120 )is not accepted?

could be your order of operation. Have you tried the following?

- platform: template
  value_template: "{{ states('sensor.time') == ( states.input_datetime.alarm_clock.attributes.timestamp | int - 120 ) | timestamp_custom('%H:%M', False) }}"
2 Likes

Holy cow! You’re a god petro. That one worked! Thanks a lot.

1 Like

Trying to run an automation based on template trigger @petro posted above. It doesn’t trigger. Actually its a test for an alarm clock automation that is more complex but I try it to run it simple first. I’ve made an input_boolean and an input_datetime with ui card.

alias: alarm_clock
description: ''
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.time') == (
      states.input_datetime.alarm_clock.attributes.timestamp | int - 180 ) |
      timestamp_custom('%H:%M', False) }}
condition:
  - condition: state
    entity_id: input_boolean.weckertoggle
    state: 'on'
action:
  - service: light.turn_on
    data:
      color_name: red
      brightness: 255
    target:
      device_id: 3a5a0598fc245293feee43fc2efa6aa1
mode: single

I’m new to all the HA coding but I think there is a code for sensor.time in my config missing. Would you guys please check my automation code.

sensor.time is not automatically created. If you want to use it you need to follow the instructions at: Time & Date - Home Assistant

However this is a 4 year old thread and there are ways to do it now that do not require setting up sensor.time or using timestamps.

alias: alarm_clock
description: ''
trigger:
  - platform: template
    value_template: >-
      {{now() >= today_at(states('input_datetime.alarm_clock')) 
      - timedelta(minutes=3) }}
condition:
  - condition: state
    entity_id: input_boolean.weckertoggle
    state: 'on'
action:
  - service: light.turn_on
    data:
      color_name: red
      brightness: 255
    target:
      device_id: 3a5a0598fc245293feee43fc2efa6aa1
mode: single
2 Likes

The only thing I wanted was an offset for my alarm clock to fade up the light before the media service starts. After research was template the only way to realize an automation. If there is a more elegant way I would be thankful for a way to go.

You do realize that the solution is the part of code, that is posted in the answer you’re reffering to (aka the post above yours), do you? :wink: :slight_smile:

@Didgeridrew Thanks a lot! Now its triggering.

3 Likes