Trigger at a Time with an Offset

I have an automation that works using the following trigger:
value_template: '{{ states(''sensor.time'') == states(''sensor.wake_time_1'') }}'

But I want to have another automation that triggers with that same wake_time_1 minus 30 minutes and don’t know how to do that.

There may be an easier way to do it but this will get you a time 30 minutes before “wake_time_1”

{{ as_timestamp((strptime(states('sensor.wake_time_1'), '%H:%M') - timedelta(minutes = 30))|as_local)|timestamp_custom('%H:%M') }}

then you can use that to test if sensor.time is equal.

@curtis-r You have left out some important details…

Assuming that your sensor.wake_time_1 returns just a time in HH:MM:SS or HH:MM format:

value_template: >
  {{ now() >= today_at( states('sensor.wake_time_1')) - timedelta(minutes = 30) }}

If you’re using the standard sensor.time, there is no point using it… just use now().

For time comparison based Template triggers it is best to use a “>=” instead of “==” so you aren’t limiting the trigger to having to hit the time value exactly.

@finity, thank you so much. I’ll give that a try.

@Didgeridrew, I wonder if using “==” has been why occasionally my alarm doesn’t sound. I think it’s more likely related to the Browser Mod I use to play the alarm sound, but I’ll try “>=”.

the “==” could be an issue but in this case the sensor.time (if it’s the built-in one) will update every minute so if you are comparing another entity that has minutes as well then it shouldn’t be a problem to use the “==”.

but either will work and using “>=” will be a bit safer.

For some reason the trigger suggestions are not working. Of course I first change the alarm time to 31 minutes in the future, then wait for the switch to trigger. There is nothing in the trace in regards to triggers. Here are the entire YAML’s.

alias: Towel Warmer - ON (alarm-based) (Duplicate)
description: Based on alarm1 time minus 30 minutes
trigger:
  - platform: template
    value_template: >
      {{ as_timestamp((strptime(states('sensor.wake_time_1'), '%H:%M') -
      timedelta(minutes = 30))|as_local)|timestamp_custom('%H:%M') }}
condition: []
action:
  - type: turn_on
    device_id: b31f670e26b07fb3708b9ab35649959b
    entity_id: switch.towel_warmer_switch_1
    domain: switch
mode: single

I also tried the other suggestion:

value_template: >
  {{ today_at( states('sensor.time') ) >=
  today_at( states('sensor.wake_time') ) - timedelta(minutes = 30) }}
alias: Towel Warmer - ON (alarm-based) (Duplicate)
description: Based on alarm1 time minus 30 minutes
trigger:
  - platform: template
    value_template: "{{ now() >= today_at(states('sensor.wake_time_1')) - timedelta(minutes = 30) }}"
condition: []
action:
  - service: switch.turn_on
    target:
      entity_id: switch.towel_warmer_switch_1
mode: single

1 Like

you missed this part:

the template you have in the trigger will never be “true” (so the automation will never trigger) because the template only returns a time. It doesn’t compare that time to anything.

in your first post you compared it to the sensor.time.

you need to add the comparison back into the trigger template.

Sorry for being such a newb but @Taras, that worked! Thanks to you both!.