Help with climate data_template

Here is my automation

- alias: 'Climate Auto Mode'   
  initial_state: true
  trigger:
  - platform: state
    entity_id: climate.mi_acpartner_playroom_2, climate.mi_acpartner_bedroom  
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state != 'unavailable') and  (trigger.to_state.state != 'off') }}"
  action:
  - service: climate.set_temperature
    data_template:
      entity_id: '{{ trigger.entity_id }}'
      temperature: 26
      hvac_mode: >
        {% if ((now().month >= 11) or (now().month <= 3)) and (trigger.to_state.state != 'heat') %}
          'heat'
        {% elif ((now().month >= 6) and (now().month <= 10)) and (trigger.to_state.state != 'cool') %}
          'cool'
        {% else %}
          'auto'
        {% endif %}  

When entity triggered this automation…

Error while executing automation automation.climate_auto_mode. Invalid data for call_service at pos 1: value is not allowed for dictionary value @ data[‘hvac_mode’]

Any tips?

Which version of home assistant are you running? If you go to Developer Tools -> Services and choose “chlimate.set_temperature” do you see “hvac_mode” as one of the parameters?

v0.102.2 from Docker.
Yes, I can see “hvac_mode” at Developer Tools. And if I force “hvac_mode” to heat/cool … no error at all.

Did you try without the single quotes around heat, cool, auto? Like this:

- alias: 'Climate Auto Mode'   
  initial_state: true
  trigger:
  - platform: state
    entity_id: climate.mi_acpartner_playroom_2, climate.mi_acpartner_bedroom  
  condition:
  - condition: template
    value_template: "{{ (trigger.to_state.state != 'unavailable') and  (trigger.to_state.state != 'off') }}"
  action:
  - service: climate.set_temperature
    data_template:
      entity_id: '{{ trigger.entity_id }}'
      temperature: 26
      hvac_mode: >
        {% if ((now().month >= 11) or (now().month <= 3)) and (trigger.to_state.state != 'heat') %}
           heat
        {% elif ((now().month >= 6) and (now().month <= 10)) and (trigger.to_state.state != 'cool') %}
           cool
        {% else %}
           auto
        {% endif %}

Tried but no lucky…

Can you try to change this

entity_id: ‘{{ trigger.entity_id }}’

to this

entity_id: ‘{{ trigger.to_state.entity_id }}’

Sorry, still same error…

Do all of these service calls work without an error?

entity_id: climate.mi_acpartner_playroom_2
temperature: 26
hvac_mode: cool
entity_id: climate.mi_acpartner_playroom_2
temperature: 26
hvac_mode: heat
entity_id: climate.mi_acpartner_bedroom
temperature: 26
hvac_mode: cool
entity_id: climate.mi_acpartner_bedroom
temperature: 26
hvac_mode: heat

No errors… weird…

No errors means they work and no errors?

I recall a similar problem while helping someone else. I performed the test using an older version of Home Assistant that employs operation_mode instead of the newer hvac_mode.

When I used the Services page to call set_temperature, I could specify both the desired temperature and the operation_mode. However, when I tried to do the same thing with an automation, Config Check complained that operation_mode is an “extra key”.

That’s not the same error as you have but still highlights a discrepancy between using the Services page and an automation.

I have not yet repeated the test using a newer version of Home Assistant.

Works good with no errors.

Is this what you are experiencing?

  • Works good with no errors when you use the Services page.
  • Fails with errors when used in an automation.

Yes. If i force to set “hvac_mode” to heat (no if else,…), the automation works great.

Does the automation also work if you force it to “cool” without if …else…?
I ask this because I remeber having read something about not being able to set to “cool” if a temperature is provided.

Still works fine if i force it to ‘cool’ in automation.

I can pass the “Config Check” with no errors. @123

Have you tried manually testing auto mode? Does your thermostat show ‘auto’ under hvac_modes?

image

Did some test here.
If the thermostat in off state, I just can’t turn it on by climate.set_temperature service, tried heat/auto/cool, no feedback at all and no errors in log.
Then I called climate.turn_on to turn on it first, the climate.set_temperature service works good.

That makes sense. Glad you figured it out! Now that I think about it I think I had the same problem when I did my climate automations a while back. You should probably mark your comment as the solution so anyone else coming across the thread sees it right away.