Subtract time from date to trigger script

Hi,

I want to trigger an script x-amount of minutes (input_number.generalsettings_timers_wakeup) before my alarm (states.sensor.wakeuptimer_house_timelong.state) goes off.
This is what I’ve already have working:

input_number.yaml:

  wakeuptimer_house_hour:
    name: Hour
    icon: mdi:timer
    initial: 6
    min: 0
    max: 23
    step: 1
    mode: slider
  wakeuptimer_house_minute:
    name: Minutes
    icon: mdi:timer
    initial: 35
    min: 0
    max: 59
    step: 5
    mode: slider

#### amount of minutes to trigger automation before alarm ####
  generalsettings_timers_wakeup:
    name: Wake Up duration (minutes)
    initial: 20
    min: 0
    max: 60
    mode: box

Sensors.yaml:

- platform: time_date
  display_options:
    - 'time'

- platform: template
  sensors:
    wakeuptimer_house_hour:
      value_template: '{{ states.input_number.wakeuptimer_house_hour.state | int }}'
    wakeuptimer_house_minute:
      value_template: '{{ states.input_number.wakeuptimer_house_minute.state | int }}'
    wakeuptimer_house_time:
      friendly_name: 'Wake Up time'
      value_template: >-
        {{ states.sensor.wakeuptimer_house_hour.state }}:
        {%- if states.sensor.wakeuptimer_house_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.wakeuptimer_house_minute.state }}
    wakeuptimer_house_timelong:
      value_template: >-
        {% if states.sensor.wakeuptimer_house_hour.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.wakeuptimer_house_hour.state }}:
        {%- if states.sensor.wakeuptimer_house_minute.state|length == 1 -%}
          0
        {%- endif -%}
          {{ states.sensor.wakeuptimer_house_minute.state }}

Automation.yaml:

  - alias: 'Wake Up'
    hide_entity: False
    trigger:
      platform: template
      value_template: '{{ states.sensor.time.state == states.sensor.wakeuptimer_house_timelong.state }}'
    action:
      service: script.wakeup

For example:
'{{ states.sensor.time.state == states.sensor.wakeuptimer_house_timelong.state }}'
06:35 == 06:35

What I am looking for is something like this (which doesn’t work):
'{{ states.sensor.time.state == states.sensor.wakeuptimer_house_timelong.state - input_number. generalsettings_timers_wakeup}}'
06:15 == 06:35 - 20

Anybody an idea?

You could use something like this in your automation’s template trigger’s value_template:

{% set hours = states('input_number.wakeuptimer_house_hour')|int %}
{% set minutes = states('input_number.wakeuptimer_house_minute')|int %}
{% set delta = states('input_number.generalsettings_timers_wakeup')|int %}
{% set alarm = hours*60 + minutes - delta %}
{{ states('sensor.time') == ((alarm*60)|timestamp_custom('%H:%M',false)) }}
1 Like

Oh My God! That is it!
Have spent 4 hours looking for a solution! :sweat_smile:
Well, at least I learned a thing or two along the way.

Could you explain the last line?
((alarm*60)|timestamp_custom('%H:%M',false))

Glad it’s working for you.

Sure. I calculated the alarm time effectively in minutes after midnight. So I multiply that by 60 to get seconds. Then, treating that as a “timestamp” (i.e., seconds since the “epoch”), I use the timestamp_custom filter to format that as “HH:MM”. The second parameter is false so that it outputs the time in UTC. (The default is true, which means output in the local timezone, which would add or subtract a number of hours, which you don’t want here.)

1 Like

Mindblown :smiley:
Thanks