Help needed with templating for climate

Hi, I’m stuck on an automation that I’m trying to setup.
I’m trying to trigger a switch when one of my thermostats is in heat mode and turn off when not. it is probably way simpler than what I am doing.
Some help would be great as my brain is not working today.

- id: autoheat
  alias: autoheat
  trigger:
  - platform: state
    entity_id: climate.kitchen
    from: idle
    to: heat
  - platform: state
    entity_id: climate.living_room
    from: idle
    to: heat
  - platform: state
    entity_id: climate.guestroom
    from: idle
    to: heat
  - platform: state
    entity_id: climate.bedroom
    from: idle
    to: heat
  - platform: time
    at: 06:41:00
  condition:
    condition: and
    conditions:
    - condition: time
      after: 07:00
      before: '23:00'
    - condition: state
      entity_id: group.all_devices
      state: home
  action:
    - service_template: >
        {% if states.climate.kitchen.state | heat %}
          switch.turn_on
        {% elif states.climate.living_room.state | heat %}
          switch.turn_on
        {% elif states.climate.guestroom.state | heat %}
          switch.turn_on  
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: switch.heating  
    - service_template: >
        {% if states.climate.bedroom.state | heat %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: switch.water

Please follow the yaml code formatting listed at the top of the page in the blue box.

Sorry , I thought I had

I also tried this, it passes the config check but does not toggle the switch.

  action:
    - service: notify.html5_mi5s
      data:
        message: "automation met conditions."  
    - service_template: >
        {% if states.climate.kitchen.attributes["heat"] %}
          switch.turn_on
        {% elif states.climate.living_room.attributes["heat"] %}
          switch.turn_on
        {% elif states.climate.guestroom.attributes["heat"] %}
          switch.turn_on  
        {% else %}
          switch.turn_off
        {% endif %}
        entity_id: switch.heating  
    - service_template: >
        {% if states.climate.bedroom.attributes["heat"] %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
        entity_id: switch.water`

You’re very close, the problem is that your triggers are only working in one direction: from idle to heat. You need to update it to work with all states. Here is a breakdown of changes:

Few things to note:

  • Removed redundant and in conditions list. This is because that list is treated as an and. No point to have it.
  • Added a condition to check if the trigger is coming from heat or going to heat.
  • Simplified your action service_templates to use the trigger object. That way you don’t have to hard code the entities.
  • Added a condition for your water switch so that it will only fire if the bedroom triggers.
  • removed at 6:41:00 trigger. The system would never know what service to actually use because you’re expecting the system to have heat states. In almost all scenarios, your heat would be turned off at 6:41, instead of turning on. Best to add this as a separate ‘turn_on’ at 6:41 instead.

Few things you did wrong in your original post:

This is not the correct way to check values. The pipe symbol | is for filters. You want to check the state so use == or the method is_state('entity_id','state').

Adding that to each trigger makes it so that this auotmation will only work when going from idle mode to heat mode. What happens when you go from cold to heat? Nothing.

These time formats are wrong. Always use hour:minute:second encapsulated in quotes. "08:00:00"

Here is what It’s been simplfied into:

- id: autoheat
  alias: autoheat
  trigger:
  - platform: state
    entity_id: climate.kitchen
  - platform: state
    entity_id: climate.living_room
  - platform: state
    entity_id: climate.guestroom
  - platform: state
    entity_id: climate.bedroom
  condition:
    - condition: time
      after: "07:00:00"
      before: "23:00:00"
    - condition: state
      entity_id: group.all_devices
      state: home
    - condition: template
      value_template: "{{ state.from_state.state.lower() == 'heat' or state.to_state.state.lower() == 'heat' }}"
  action:
    - service_template: "switch.turn_{{ 'on' if state.to_state.state.lower() == 'heat' else 'off' }}"
      entity_id: switch.heating
    - condition: template
      value_template: "{{ trigger.entity_id == 'climate.bedroom' }}"
    - service_template: "switch.turn_{{ 'on' if state.to_state.state.lower() == 'heat' else 'off' }}"
      entity_id: switch.water
3 Likes

Props for one of the best replies I’ve ever seen on this forum!

@conor just an FYI you could still use the if/else multiline block syntax with the trigger to state template, petro just wrote it in a very succinct format. I only mention this because for a programming noob like me it’s much easier to visualize. Obviously I wouldn’t go and rewrite petro’s beautiful code, but might be good to know for your future templating pleasure.

1 Like

Put all your thermostats into a group and the use the state of the group being on/off to trigger trigger the switch.

That won’t work for the second half of his action, which needs to look at the single ‘climate.bedroom’ status.

Thanks for all the replies, This looks great I’m going to test it now and I’ll report back later.

Bad news guys the code did not work. I tested it without the time the and group conditions to have the least amount of variations, I also changed the switch to a light for instant feedback. it still did not work.
Also I tried Crhass idea of grouping them this also did not work.
I also tried is_state('climate.kitchen','heat') I also could not get this working.

@petro Could you point me to the docs for state.lower, I have not seen this before and would like to look more into it.
thanks, for your help so far.

It’s probably not in the docs. This is just python/jinja. .lower() takes all characters and lowercases them. So if you have this string SLKJFIVONeskjfOIFSOIDHVklj it would turn it into slkjfivoneskjfoifsoidhvklj

EDIT: Makes it easier to compare strings.

1 Like

If i was to not use this for testing purposes would it be just state.from_state.state

@Dolores could you show me show me how you would do this with if else statements as nothing else has worked.

Did you try the automation I supplied?

also, if statements he is refering to would be:

{{ if is_state('climate.kitchen','heat') }}

or

{{ if states('climate.kitchen') == 'heat' }}

or

{{ if states.climate.kitchen.state == 'heat' }}
1 Like

Sorry, I was away for a couple of days. Yes I tried the automation you supplied, I had no errors with this it just didn’t fire the switch.
I also tried the if statements above but no luck, I may have implemented them incorrectly.
Is this the correct format.

- service_template: >
    {{ if states.climate.kitchen.state == 'heat' }}
      switch.turn_on
    {{ elif states.climate.living_room.state == 'heat' }}
      switch.turn_on
    {{ elif states.climate.guestroom.state == 'heat' }}
      switch.turn_on
    {% else %}
      switch.turn_off
    {% endif %}
  entity_id: switch.lava_lamp

can you verify that the states of these items are ‘heat’? or are they a different case. Remember, if statements are case sensitive.

I think so i’m using the generic thermostat component, the following is a screenshot of my states tab in the developer section.

@petro Hi again,
I got your automation working.
My only change was I changed the following

    - condition: template
      value_template: "{{ state.from_state.state.lower() == 'heat' or state.to_state.state.lower() == 'heat' }}"
  action:
    - service_template: "switch.turn_{{ 'on' if state.to_state.state.lower() == 'heat' else 'off' }}"
      entity_id: switch.heating

to

  - condition: template
      value_template: "{{ trigger.from_state.state.lower() == 'heat' or trigger.to_state.state.lower() == 'heat' }}"
  action:
    - service_template: "switch.turn_{{ 'on' if trigger.to_state.state.lower() == 'heat' else 'off' }}"
      entity_id: switch.heating

Thanks for your help and patience

2 Likes

The only issue I have with this is when climate.bedroom is trigger it turns on both switch.water and switch.heating.
Would it be possible to have a nested template or 2 separate automations to control it?