Help with climate data_template

Unfortunately it doesn’t work anyway, I also tried

{% if states.climate.mi_acpartner_bedroom.state == 'cool' %}
    'heat'
{% elif states.climate.mi_acpartner_bedroom.state == 'heat' %}
    'cool'
{% else %}
    'auto'
{% endif %}

Well my friend you have me stumped. I’ve run out of ideas for you. I’ll be watching the thread to find out what the solution is so if you figure it out please update us!

Yeah, that’s very weird… I know the undefined error if I trigger the automation manually, but it’s a different story here.

Thank you folks, I am afraid I will just force to set hvac_mode to ‘heat’ for now, tried all the solutions as much as I can image. :sweat_smile:

@123 I think I may have figured it out!

@sephrioth I got bored and had another good look at this last night. When I used your original automation in my config I got the exact same error as you did. After looking at the logic again I realized you were recursively triggering the automation. So I added in the steps to make sure that doesn’t happen and it seemed to do the trick! The following automation runs as expected on my system now without error.

EDIT: I was concentrating so hard on getting this automation to work I completely overlooked some of the other issues it has.

  • Your automation is going to fire every time you touch the thermostat and every time the HVAC system turns on to heat or cool the house. I don’t think that is what you intended. I added a condition to prevent it from running if the temperature hasn’t been adjusted and the hvac mode hasn’t changed.

  • The temperature is always going to be reset back to 26 degrees. So if you’ve got a little chill or are a little warm and want to bump your temperature either way you can’t because the automation will run and will reset it back to 26. So as it stands you’ll have to manually turn this automation off any time you want to have manual control over the temperature. It also means your temperature will be the same value all year long, which most people don’t do. I added a template to demonstrate.

  • With my thermostat (Nest) including the hvac_mode in the set_temperature service does not produce an error but it also doesn’t do anything. I have to call them separately. I’m not sure if yours will work the same way or not. You can try it but I have them separated below.

  • What happens when this runs anytime between November and March? It hits the first part of your template and it asks if the new state was not set to heat then set it to heat which is fine if it wasn’t. But if it WAS set to heat it heads down to the else statement and sets the HVAC mode to auto. Removing the trigger.to_state check fixes this problem. I don’t think it’s necessary. If the system is in heat mode and you set it again to heat mode nothing happens except an additional call to the API.

- 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') }}"

    - condition: template
      value_template: >
        {{ (trigger.to_state.attributes.temperature != trigger.from_state.attributes.temperature)
          or (trigger.to_state.attributes.hvac_mode != trigger.from_state.attributes.hvac_mode) }}

  action:
    - service: automation.turn_off  # you need to turn this off or you get a recursive trigger!
      entity_id: automation.climate_auto_mode

    - service: climate.set_hvac_mode
      data_template:
        entity_id: "{{ trigger.entity_id }}"
        hvac_mode: >
          {% if ((now().month >= 11) or (now().month <= 3)) %}
            heat
          {% elif ((now().month >= 6) and (now().month <= 10)) %}
            cool
          {% else %}
            auto
          {% endif %}

    - service: climate.set_temperature
      data_template:
        entity_id: "{{ trigger.entity_id }}"
        temperature: >
          {% if ((now().month >= 11) or (now().month <= 3)) %}
            26
          {% elif ((now().month >= 6) and (now().month <= 10)) %}
            28
          {% else %}
            27
          {% endif %}

    - delay: '00:00:02'  # this is needed to allow states to change.

    - service: automation.turn_on # remember to turn it back on!
      entity_id: automation.climate_auto_mode

1 Like

Jason, you are so great, huge thanks!
Here is some feedback before I try your new solution. :smiley:

Actually I thought about the recursively trigger issue, so I tried to add for “00:00:03” to heat and cool in trigger, but still old errors so I just gave up.

  • And I also I aware the fire issue, so I modified the code to
  trigger:
  - platform: state
    entity_id: climate.mi_acpartner_playroom_2, climate.mi_acpartner_bedroom  
  condition:
    condition: or
    conditions:
    - condition: template
      value_template: "{{ (trigger.to_state.state == 'heat') and  ((now().month >= 6) and (now().month <= 10)) }}"
    - condition: template
      value_template: "{{ (trigger.to_state.state == 'cool') and  ((now().month >= 11) or (now().month <= 3)) }}"  
  • Based on my code above, I think temperature change won’t trigger the automation right (Will test later)?

  • I need test first to see if I have to call them separately.

  • Should do nothing because the condition not matched based on my new code, so I can manually change it, I hope.

Good luck to me, I will do the test and back later, thanks again, dude! @jazzyisj

The new conditions you show here won’t stop the automation from triggering when the hvac turns on to heat or cool the house. They also prevent any climate adjustments at all in April and May. The heat will only run from June until October and the A/C will only run from November to March. I’m pretty sure that’s not what you want unless you happen to live in Australia (maybe you do?).

The automation I wrote for you should be plug and play for your config. Give it a try as it is written and see if it does what you need it to do. If it does not, and you can’t figure out why on your own, I’m happy to keep working with you on it.

It finally works!!!

And I did some tests found something even more weird. @Burningstone asked me to remove single quote in previous post and I got same error that time, and today I just removed the single quote and no errors at all, even I didn’t add automation control.

BTW, my action code is this, so I am not in Australia :joy:

        {% if trigger.to_state.state == 'cool' %}
          heat
        {% elif trigger.to_state.state == 'heat' %}
          cool
        {% else %}
          auto
        {% endif %}

Whew. Glad we finally got it figure out for you. That was a tough nut to crack! :grinning:

Yeah, not an easy one, but I am still confused with single quote usage now, in previous test

"{{ 'heat' if true else 'auto' }}" 

works good with single quote?

Yes. I don’t think the quotes were ever the issue here.

This should be a helpful page for you, especially the templates section.

Thanks, I will try my best to understand this… :rofl:

The goal of this automation appears to be the prevent the thermostat from being set to a temperature and mode that are inappropriate for a given month. In fact, there are only two months when the thermostat is allowed to be used without constraints. Is this correct?

My solution was based on the original automation which I interpreted as:

  • November thru March, always set hvac to heat mode, regardless of user setting
  • June thru October, always set the hvac to cool, regardless of user setting
  • April and May, always set the hvac mode to auto, regardless of user setting
  • The temperature would always be set to 26 degrees no matter the mode. (Personally I don’t think that is desirable so I tried to show how he could adjust it for different time periods with the hvac mode.)

The last automation he posted approaches it quite differently also with no temperature being set. It’s actually seems like a more logical way to approach it so kudos to him for working it out.

I approach my climate automation from an entirely different angle. It was fun trying to figure this one out though. Definitely had me stumped for a bit! :upside_down_face:

BTW this automation would never fly at my place. I guarantee we’d get a freak cold snap in June when I’m outta town and the wife would try to put the heat on. I’d come home to my Nest in pieces in the trash and a $200 bill from the schmuck that installed the replacement while I was gone :rofl:

The motivation I create this automation is my climates sometimes always set to ‘cold’ if I call climate.turn_on service, so I am trying to set to ‘heat’ in winter automatically.

:joy:

BTW, base on my latest config, manual temperature change doesn’t trigger the automation, which means I can still change the target temperature.

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

That would have been helpful information in step one! I probably over thought this one…lol
So if that’s all you’re trying to accomplish why use climate.set_temperature and not just climate.set_hvac_mode? I thought you had dropped the temperature set, you had only posted part of your automation!

I would be curious to find out what happens if you went to your thermostat right now and set the temperature to 30 and then set the mode to cool, in that order.

Sorry for the missing part… I really only focused on the error that time. :sweat_smile:

I didi what you asked, it worked as expected.

  1. Turn on thermostat.
  2. Trigger the automation and set to ‘heat’ to 26 degree
  3. Set the temperature to 30.
  4. Set to cool.
  5. Trigger the automation, back to ‘heat’ to 26.

So you do want to force the temp to 26. Gotcha! I guess you’re all set then!

Yeah, I just want to set a default comfortable temperature, thank you so much Jason. :clap: