Automation with condition using input_datetime and sensor.date_time

I am trying to set up an automation that will turn a switch off after a period of time using a condition with input_date. I do not want to us the for portion of the time trigger or the last changed attribute of the switch as neither survive a reboot of HA and with all the configuring I do that occurs often.

I cannot figure out how to write the template. I have been testing it every way I can think or find online with no luck.
Here is my automation so far:

alias: Timer - Fireplace Off after 2 hours (Duplicate)
description: Turns off the fire place after it’s been on for 2 hours.
trigger:
  - platform: state
    entity_id:
      - input_datetime.fireplace_on_timer
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: template
      value_template: 
        {% set t = states('sensor.date_time') %}
        {% set m = state_attr('input_datetime.fireplace_on_timer','timestamp') | int %}
       {{ t >= ( m + 7200 ) }}
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.fireplace
mode: single

Thanks in advance.

Conditions are checked immediately after the trigger fires, if they are true then the actions happen, if not the automation ends. An automation does not wait for a condition to become true. You need a template Trigger, not a template Condition.

alias: Timer - Fireplace Off after 2 hours (Duplicate)
description: Turns off the fire place after it’s been on for 2 hours.
trigger:
  - platform: template
    value_template: >
      {{ now() >= states('input_datetime.fireplace_on_timer')|as_datetime|as_local + timedelta(hours=2) }}
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.fireplace
mode: single
1 Like

See post above. I forgot about the two hour offset.

If you use a template trigger like {{ now().timestamp > as_timestamp(states('input_datetime.fireplace_on_timer')) }}
Then it will also work after a reboot/config reload.

That is at least as far as I have understood

Thank you for getting back to me. I realize now I left out some key information that you were able to identify in my code thanks.
I do want a 2 hour offset. I created the input_datetime.fireplace_on_timer and call the input_datetime.set_datetime to populate it whenever the switch.fireplace is turned on.
Thanks again so much I now understand not only the template but all my error in the trigger.

Also I am confused, on the template to use, is it:

value_template: >
      {{ now() >= states('input_datetime.fireplace_on_timer')|as_datetime|as_local + timedelta(hours=2) }}

Or

{{ now().timestamp > as_timestamp(states('input_datetime.fireplace_on_timer')) }}

With a couple tweeks, all three of the templates so far should produce the same result. The biggest issues was how you had used it, so use whichever one is easiest for you to understand…

{% set t = as_timestamp(strptime(states('sensor.date_time'), '%Y-%m-%d, %H:%M')) %}
{% set m = state_attr('input_datetime.fireplace_on_timer','timestamp') %}
{{ t >= ( m + 7200 ) }}
 {{ now() >= states('input_datetime.fireplace_on_timer') | as_datetime | as_local + timedelta(hours=2) }}
{{ now().timestamp() >= as_timestamp(states('input_datetime.fireplace_on_timer')) + 7200 }}

Thanks, I used the 2nd option. The first one was fro my original and it does not work. It gives the following error:
TypeError: ‘>=’ not supported between instances of ‘str’ and ‘float’