Automation trigger on time 15 minutes

I have time sensor with info " 2021-09-13 17:01:00 "
I want Automation that trigger at time but 15 min before.

I have try with

alias: Train1
description: ''
trigger:
  - platform: template
    value_template: >-
      {{
      as_timestamp(states('sensor.tag1'))|timestamp_custom('%H:%M',
      true) == states('sensor.time')  - 15  }}
condition: []
action:
  - service: notify.telegram
    data:
      title: 'Tåg Status:'
      message: 15 minuter kvar
mode: single

But is not working.

If I remove -15 is tiggor on time but I want to get notify 15 min befor.

Please Help…

This will subtract 15 minutes from your sensor and trigger the automation if the value is the same with your sensor.time-

alias: Train1
description: ''
trigger:
  - platform: template
    value_template: >-
      {{ (as_timestamp(states('sensor.tag1')) | int - 900) | timestamp_custom('%H:%M') == states('sensor.time') }}
action:
  - service: notify.telegram
    data:
      title: 'Tåg Status:'
      message: 15 minuter kvar
mode: single

To make sure it is correct, copy this to Developer Tools → Template
{{ (as_timestamp('2021-09-13 17:01:00') | int - 900) | timestamp_custom('%H:%M') }}. The result should be 16:46.

Thanks!!!
Is working…
I try with 60 sec and wait…

Hmm…
Is is possible to have " int - 900 " as varibel?
eg.

 {{ (as_timestamp(states('sensor.tag1')) | int - "input_number.walktrain") | timestamp_custom('%H:%M') == states('sensor.time') }}

{{ (as_timestamp(states('sensor.tag1')) | int - states('input_number.walktrain') | int) | timestamp_custom('%H:%M') == states('sensor.time') }}

Your time sensor includes a date. However, your Template Trigger ignores the date and only uses the time. That means it will trigger at the same time every day, even if the time sensor’s value always contains 2021-09-13. Is that the way you want it to work?

If you want the automation to trigger only at the time and date shown in the time sensor, here’s a simple way to do that and it doesn’t require the use of sensor.time.

alias: Train1
description: ''
trigger:
  - platform: template
    value_template: >-
      {{ now() >= states('sensor.tag1'))|as_datetime|as_local - timedelta(minutes=15) }}
condition: []
action:
  - service: notify.telegram
    data:
      title: 'Tåg Status:'
      message: 15 minuter kvar
mode: single

If you want to incorporate input_number.walktrain into the template, and its value is in seconds, change the template to this:

    value_template: >-
      {{ now() >= states('sensor.tag1'))|as_datetime|as_local - timedelta(seconds = states('input_number.walktrain')|int) }}

Thanks.
After some test I find this is working…

{{ now() >= states('sensor.tag1') |as_datetime|as_local - timedelta(minutes = states('input_number.walktrain')|int) }}

Glad to hear the suggested technique solved the issue.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information refer to guideline 21 in the FAQ.