Automation / Script / Template, complex, what to do?

Hi,

In the winter I have an automation that when pushing the switch in the bedroom, all devices turn off and also the heating is put into “Eco” (low temperature) preset mode.
When I’m away, I have a template to ignore it, no problem with that.
As the better weather is approaching I’m shutting down the heating.
That means HVAC mode is switched to “off”.

Now the tricky part… When pushing the bedroom switch, it puts the “Eco” mode back on, and the HVAC is pushed into heating again.

I’m having a struggle to wrap my head around this issue…
How to tell the switch to ignore the hvac preset mode when the hvac mode is switched to “off”.

This is the code i have:

action:
  - service: switch.turn_off
    entity_id:
    - switch.printers
  - service: climate.set_preset_mode
    data_template:
      entity_id: climate.slauer
      preset_mode: >-
        {% if is_state("climate.slauer" , "off") %}
          null
        {% elif is_state_attr("climate.slauer" , "preset_mode" , "away") %}
          away
        {% else %}
          eco
        {% endif %}
  - service: homeassistant.turn_off
    entity_id:
    - automation.trap_pir_aan

btw, the null does not work also :wink:

You’ll have to separate the service out into it’s own script and add a condition to the script. The condition will not fire the service if the climate is off

script:
  handle_preset_mode:
    sequence:
    - condition: template
      value_template: '{{ not is_state("climate.slauer" , "off") }}'
    - service: climate.set_preset_mode
      data_template:
        entity_id: climate.slauer
        preset_mode: >-
          {% if is_state_attr("climate.slauer" , "preset_mode" , "away") %}
            away
          {% else %}
            eco
          {% endif %}

and your automation

action:
  - service: switch.turn_off
    entity_id:
    - switch.printers
  - service: script.handle_preset_mode
  - service: homeassistant.turn_off
    entity_id:
    - automation.trap_pir_aan
1 Like

OMG this solution is so simple but awesome effective.
My biggest problem in this was quit staring at the problem. The two different command. But indeed. A script stops if condition doesn’t apply. Exactly the solution I was searching for. Thanks!!!

1 Like