Simple switch schedule automation to control a water heater

Dear all.
I’m trying to implement a simple scheduled switch automation to control a water heater.
For now I just want to turn on the water heater at a given time. For this I’m using a input_datetime. This is working fine. My problem is to turn off the heater. I want to use an input_select to let the user choose a delay (in hours) to turn off the heater. I can’t get this to work. With my above code I get the heater on at the desired time but it doesn’t turn off one hour later.
What I’m doing wrong?
Thank you for your help.

  - alias: 'Water Heather automation'
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == states.input_datetime.water_heater_on_time.state[0:5] }}"
    action:
    - service: switch.turn_on
      entity_id: switch.water_heater
        
  - alias: 'Water Heather OFF 1h'
    initial_state: true
    hide_entity: True
    trigger:
      - platform: template
        value_template: "{{ now().hour * 3600 > states.input_datetime.water_heater_on_time.attributes.timestamp + 3600 | int and is_state('input_select.water_heater_mode','1h') }}"
    action:
    - service: homeassistant.turn_off
      entity_id: switch.water_heater

Please read the docs, especially the Warning (and some more here).
Your template trigger only reacts on change in your input_datetime’s state, which is not enough for it to work.
If you simply add something like

{% set dummy = states('sensor.time') %}

it’ll do the trick (but it’s lazy)

Hi. I always read the docs, but I definetively missed that warning on using now() in templates. It its working fine now. Thank you.
Here’s the code working fine:

  - alias: 'Water Heather automation'
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == states.input_datetime.water_heater_on_time.state[0:5] }}"
    action:
    - service: switch.turn_on
      entity_id: switch.water_heater
        
  - alias: 'Water Heather OFF 1h'
    initial_state: true
    hide_entity: True
    trigger:
      - platform: template
        value_template: "{{ (as_timestamp(states.sensor.time.last_changed) - as_timestamp(states.switch.water_heater.last_changed)) > 3600 }}"
    condition:
      - condition: state
        entity_id: input_select.water_heater_mode
        state: 1h
    action:
    - service: homeassistant.turn_off
      entity_id: switch.water_heater
1 Like

If you go template route, it might be reasonable not to have separate conditions etc.
Here’s how it can be (untested)

- alias: 'Water Heather automation'
  trigger:
    - platform: template
      value_template: >
        {% set time_on = states('input_datetime.water_heater_on_time')[0:5] %}
        {{ is_state('sensor.time', time_on) }}"
  action:
  - service: switch.turn_on
    entity_id: switch.water_heater
      
- alias: 'Water Heather OFF 1h'
  trigger:
    - platform: template
      value_template: >
        {{
          is_state('nput_select.water_heater_mode', '1h') and
          (states.sensor.time.last_changed - states.switch.water_heater.last_changed).total_seconds() > 3600
        }}
  action:
  - service: homeassistant.turn_off
    entity_id: switch.water_heater

By the way, you don’t need initial_state: true unless you know what it does.
And hide_entity: True is a deprecated thing from States UI, isn’t it?

Yes it is more reasonable to put all conditions in the template. Thank you.
I’m using initial_state: true because I want to manually disable the automation from time to time, but I want it to be activated by default when restarting hass.
And yes hide_entity is deprecated and I used them before lovelace existed and I somehow managed to keep them uselessly in my code. I’m deleting those…

The final piece of code should be:

- alias: 'Water Heather automation'
  trigger:
    - platform: template
      value_template: >
        {% set time_on = states('input_datetime.water_heater_on_time')[0:5] %}
        {{ is_state('sensor.time', time_on) }}"
  action:
  - service: switch.turn_on
    entity_id: switch.water_heater
      
- alias: 'Water Heather OFF 1h'
  trigger:
    - platform: template
      value_template: >
        {{
          is_state('input_select.water_heater_mode', '1h') and
          (states.sensor.time.last_changed - states.switch.water_heater.last_changed).total_seconds() > 3600
        }}
  action:
  - service: homeassistant.turn_off
    entity_id: switch.water_heater

Fair enough, but do you know that you can add your automation to a Lovelace card and enable/disable it from there? It’ll keep its state after HA restart.
Maybe it’ll suit your needs?