Automation: thermostat without error loop?

Hi everybody,

my current automation for changing thermostats depending on corresponding window state is pasted at the bottom. I use Xiaomi Aqara sensors for my windows and Eurotronic SPZB0001 thermostats for my climate entities.

Unfortunately, those thermostats sometimes randomly disconnect from zigbee2mqtt; when this happens, the current automation will create an infinite error loop, resulting in the entire zigbee2mqtt network eventually going down!! It will constantly send the same command to that (in this case not responding thermostat), which will first slow down all traffic via z2m, then eventually even kill all activities (so I cannot control anything, nor read values, via zigbee2mqtt).

I am trying to create an automation that will

  • be triggered when either a window status, or a climate entity status changes
  • when window got set to open, turn thermostat to “off” if not already off; when window got set to close, turn thermostat to “auto” if not already auto; do this only once, and if there is no response from the climate entity, do x (send message similar to below)
  • when thermostat got set to anything but “off”, check whether window is shut. if yes, do nothing; if no, turn thermostat “off”; if window status cannot be read (for example, if there has been a restart and the state from before the restart had not been saved), do x (for example, send notification “thermostat changed, but window does currently not have a valid status”.

My current test looks like this

{% set my_window = "on" %}
{% set my_heater = "off" %}
{% if (my_window == "on") %}
 {% if (my_heater == "on") %}
   hvac_mode: "off"
 {% else %}
   hvac_mode: "auto"
 {% endif %}
{% else %}
  aus
{% endif %}

This should work, but would be rather confusing when it’s done, as I would need a bunch of nested if statements. Also, I am not sure if I can even figure out the entire structure I’d need to build based on my needs stated above.

The automation I had previously been using is below. It does work, but as soon as an entity does not change accordingly (as mentioned above, those SPZB0001 thermostats often disconnect and/or will not work correctly - even if still connected). I had intense problems with zigbee2mqtt until I deactivated this automation, because it caused the an infinite error loop.

Just to clarify: the automation is fine in theory. It will work. The reason I cannot use it is that those unreliable thermostats make it function other than intended. If the thermostats worked reliably, the automation could be as stated above without any trouble.

automation:
  - alias: "[Heizungen] Auto On/Off"
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.arbeitszimmer_fenster_contact
          - binary_sensor.kueche_fenster_contact
          - binary_sensor.jonna_fenster_contact
    action:
      - service: climate.set_hvac_mode
        data_template:
          entity_id: >
            {% set zimmer = trigger.to_state.object_id.split('_')[0] %}
            climate.{{zimmer}}_heizung_climate
          hvac_mode: >
            {% if trigger.to_state.state == "on" %}
            off
            {%elif trigger.to_state.state == "off" %}
            auto
            {% endif %}

  - alias: "[Heizungen Fenstercheck]"
    trigger:
      - platform: state
        entity_id: 
          - climate.arbeitszimmer_heizung_climate
          - climate.jonna_heizung_climate
          - climate.kueche_heizung_climate
    action:
      - service: climate.set_hvac_mode
        data_template:
          entity_id: >
            {% set raum = trigger.to_state.object_id.split('_')[0] %}
            climate.{{raum}}_heizung_climate
          hvac_mode: >
            {% set fenster = 'binary_sensor.' ~ trigger.to_state.object_id.split('_')[0] ~ '_fenster_contact' %}
            {% if states(fenster) == "on" %}
            off
            {% elif states(fenster) == "off" %}
            auto
            {% endif %}

Thank you for your ideas :slight_smile:

An idea to your approach. I had a thermostat that wouldn’t always accept a command, so I created an input_boolean that was turned on when I sent a command. Once that boolean was on for a couple minutes I would evaluate the entity. If different turn the boolean off, fire a second command, then turn it the boolean back on. If same, turn the boolean off. This would happen slowly as to not flood the system with commands.

Does the entity change when it is unavailable?

Maybe add a boolean or template binary sensor that monitors the state of the device. If that entity is unavailable, send a notification instead of sending a command. This could be done through a service template on your actions. If you send through a script, you could pass the variables trying to be sent as well.

Thank you! That sounds like a good idea. I had thought of something similar before, but was hoping there would be a way that wouldn’t require me to create countless input_boolean sensors (I’d create one per windows as well, just in case they are down as well).

Unfortunately, those entities will not be completely down; when they are “down”, they will not be controllable any longer, but they will be displayed. For example, when I set the one thermostat to x degrees, it will turn the thermostat to that temperature, but will not update the value in Home Assistant (not even when I manually change temperature on the thermostat).

I really like the idea of using a script instead of so many automations. I will give that a shot :slight_smile:

I created input_booleans for all windows and climate entitites, and it works fine so far. Thank you for your help.