Subtract x minutes from datetime

I would like to automate something x minutes before an amazon echo next alarm. So for example start my bedside light fade up script 30 minutes before the alarm. When set the entity of the next_alarm looks like this : 2022-11-05T15:00:00+00:00

However, when I try the below template, it ends up with a result like 44868 days, 14:30:00 - I can’t see a way to use this as an automation trigger.

{{ strptime(states('sensor.office_dot_next_alarm'), '%Y-%m-%dT%H:%M:%S+00:00') - strptime('30', '%M') }}

I also tried something with setting a time helper, but this also does not work

service: input_datetime.set_datetime
data:
  time: >-
    {{ strptime(states('sensor.office_dot_next_alarm'),
    '%Y-%m-%dT%H:%M:%S+00:00') - strptime('30', '%M') }}
target:
  entity_id: input_datetime.test_time_3

Any idea how I could use either a direct trigger of alarm time - 30 minutes or via a helper please?

1 Like

Here’s how to subtract 30 minutes from the sensor’s datetime value:

{{ states('sensor.office_dot_next_alarm') | as_datetime - timedelta(minutes=30) }}

Here’s an example of using it in a Template Trigger:

alias: example
trigger:
  - platform: template
    value_template: "{{ now() >= states('sensor.office_dot_next_alarm') | as_datetime - timedelta(minutes=30) }}"
condition: []
action:
   ... etc ...
3 Likes

Thank you - this worked perfectly

Is it possible to use a number helper rather than a fixed value of 30?

Assuming the input_number’s value represents minutes:

    value_template: >
      {{ now() >= states('sensor.office_dot_next_alarm') | as_datetime - timedelta(minutes = states('input_number.whatever') | int(0)) }}"


1 Like

This gives me:

TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘datetime.timedelta’

You’ll get an error message like that if sensor.office_dot_next_alarm doesn’t exist or the as_datetime filter cannot convert the sensor’s value into a datetime.

What are the values reported by the following templates?

{{ states('sensor.office_dot_next_alarm') }}

{{ states('sensor.office_dot_next_alarm') | as_datetime }}

I have been struggling for some time with this. Here is my code:

alias: K1 Test Alarm
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.russ_s_echo_spot_next_timer
    from: unknown
condition: []
action:
  - if:
      - condition: template
        value_template: >-
          value_template: "{{ now() >=
          states('sensor.russ_s_echo_spot_next_timer') | as_datetime -
          timedelta(minutes=1) }}"
    then:
      - service: notify.alexa_media_russ_s_echo_spot
        data:
          message: Test
          data:
            type: announce
mode: single

I can watch the Value template change to true using Dev Tools > Template:

value_template: "{{ now() >= states('sensor.russ_s_echo_spot_next_timer') | as_datetime - timedelta(minutes=1) }}"

But the then statement doesn’t fire.

Any suggestions would be greatly appreciated.

TIA

Check the automation’s trace.

You have a duplicated the phrase value_template.

Change this:

action:
  - if:
      - condition: template
        value_template: >-
          value_template: "{{ now() >=
          states('sensor.russ_s_echo_spot_next_timer') | as_datetime -
          timedelta(minutes=1) }}"

To this:


action:
  - if:
      - condition: template
        value_template: >-
          {{ now() >= states('sensor.russ_s_echo_spot_next_timer') | as_datetime - timedelta(minutes=1) }}

Taras, thanks for the assist, but it still doesn’t work. I have noticed that Dev Tools states: This template updates at the start of each minute. So I’m working toward changing timedelta to 30 seconds. Still I can be watching Dev Tools and see it change from false to true, but the action still doesn’t fire. I have been reading from other threads and I can see you’re heavily involved in this topic. I just have so much to learn, I will keep pushin’ on.

It definitely will not work with two instances of value_template. What I posted is, at bare minimum, a valid configuration and template.

Your automation is triggered when it detects a change in the sensor’s state value. It then tests if the current date and time is greater than the sensor’s value less one minute.

If you want to examine the result of that test, check the automation’s trace.