Subtract time from a helper

Hi! I’ve tried to use the timedelta function described here.

The following automation works:

alias: Testi
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.date_time') ==
      as_timestamp(states.sensor.oms8_next_alarm.state)|
      timestamp_custom('%Y-%m-%d, %H:%M') }}
condition: []
action:
  - service: light.turn_on
    data:
      effect: "1"
    target:
      entity_id: light.all_lights
mode: single

However, when I try to make it trigger one minute earlier, nothing happens:

alias: testi5
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.date_time') ==
      as_timestamp(states.sensor.oms8_next_alarm.state) - timedelta(minutes=1)|
      timestamp_custom('%Y-%m-%d, %H:%M') }}
condition: []
action:
  - service: light.turn_on
    data:
      effect: "2"
    target:
      entity_id: light.all_lights
mode: single

i’ve tried to debug this now for hours. Please help! :slight_smile:

as_timestamp produces an integer value (representing the number of seconds since the Unix Epoch).

timedelta(minutes=1) produces a timedelta object. You can use it for subtracting from a datetime object but not from an integer value.

Just subtract 60 seconds from the integer value.

    value_template: >-
      {{ states('sensor.date_time') ==
      as_timestamp(states.sensor.oms8_next_alarm.state) - 60 |
      timestamp_custom('%Y-%m-%d, %H:%M') }}

Thanks for your reply! This is what I suspected myself, but adding that " - 60" to the code doesn’t trigger the automation anymore at all:

alias: Testi - 60s
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.date_time') ==
      as_timestamp(states.sensor.oms8_next_alarm.state) - 60 |
      timestamp_custom('%Y-%m-%d, %H:%M') }}
condition: []
action:
  - service: light.turn_on
    data:
      effect: "2"
    target:
      entity_id: light.all_lights
mode: single

So weird!

I need additional information to help you fix this problem.

  1. Go to Developer Tools > States.
  2. Find sensor.date_time and sensor.oms8_next_alarm
  3. Let me know the value of each sensor’s state.
1 Like

Thanks for the help! Here are the details: