Figuring out the best way to "merge" many to one

I have a set of thermostats (Better Thermostat, but it doesn’t matter) and i want to create an automation so that when all of them are in “off” a specific MQTT topic is published, and if any one of them is “on”, then a different MQTT automation is published. Bonus point, i want to collect this info (all off, at least one on) in an helper so that i can use it with other automations.

This is what i use now:

alias: Send MQTT topic
description: send topic
triggers:
  - trigger: state
    entity_id:
      - sensor.termo_box_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_cucina_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_disimpegno_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_bagno_sopra_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_bagno_sotto_hvac_action
  - trigger: state
    entity_id:
      - sensor.scaldasalviette_bagno_sopra_hvac_action
  - trigger: state
    entity_id:
      - schedule.sempre
  - trigger: state
    entity_id:
      - input_boolean.quick_heat
  - trigger: state
    entity_id:
      - input_boolean.usa_pellet
  - trigger: state
    entity_id:
      - sensor.termo_camera_eli_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_camera_ale_hvac_action
  - trigger: state
    entity_id:
      - sensor.termo_bagno_eli_hvac_action
actions:
  - action: mqtt.publish
    data:
      payload: >-
        {{  is_state('sensor.termo_box_hvac_action', 'heating') or 
        is_state('sensor.termo_cucina_hvac_action', 'heating') or
        is_state('sensor.termo_disimpegno_hvac_action', 'heating') or
        is_state('sensor.termo_bagno_sopra_hvac_action', 'heating') or
        is_state('sensor.termo_bagno_sotto_hvac_action', 'heating') or
        is_state('sensor.termo_camera_eli_hvac_action', 'heating') or
        is_state('sensor.termo_camera_ale_hvac_action', 'heating') or
        is_state('termo_bagno_eli_hvac_action', 'heating') or
        is_state('sensor.scaldasalviette_bagno_sopra_hvac_action', 'heating') }}
      topic: riscaldamento/feedback
      retain: true
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: true
      topic: riscaldamento/usegas
      payload: |-
        {{  is_state('input_boolean.usa_pellet', 'off') or
        is_state('input_boolean.quick_heat', 'on') }}
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: true
      topic: riscaldamento/usepellet
      payload: |-
        {{  is_state('schedule.sempre', 'on') and 
        is_state('input_boolean.usa_pellet', 'on') }}
mode: single

But there are a few issues:

  • Adding more thermostats require a script modification
  • if i want to know the combined status outside the script, i cannot

What is a better approach? I tried creating an helper to group the thermostats, but with no luck.

Depending on how you use climate entities perhaps looping the entities could be an option?

{% for state in states.climate %}
  {{ state.state}} 
{%- endfor %}

Obviously it needs more work than this but just as proof of concept.

How does this work? Such a loop what would accomplish?

You could count the number of climates that is on, if above 0 or = 0 then make true/ false binary sensor

Good idea, can you give me an example of how to count them?

Group helper?

1 Like

That depends on what the previous code outputs.
Does it give you heat/off or is there an attribute you need to look at to get the on/off state?

Jack, as far as I know you can’t group thermostats.
There is a feature request for it.

Yes, thermostats cannot be grouped today (yet?)

Give them all a label of “thermostat”, then use this as the template state for a Template Binary Sensor helper:

{{ label_entities('thermostat')|map('states')|max }}

will return on if any are on or off if all are off. It will also return unknown or unavailable if any of the sensors are in that state. If you want to filter those out first:

{{ label_entities('thermostat')|map('states')|select('in',['on','off'])|max }}
1 Like

I’ll let you first try this method before posting my longer option.
Troops code is far better if it works. (Which I assume it does)

You can do it with “old style” groups, which still work.