Turn off all Thermostats in area_name(trigger.entity_id)

Hi all,
I’d like to create an automation which turns off all thermostats in one room when a door/window is opened in the same room.
I saw that this should somehow be possible via area_name(trigger.entity_id), where the trigger is the door/window binary-sensor.
How can I then address all climate thermostats in that corresponding area?
The idea is to turn off the thermostats for the time a window or door is open and turn them back on, when the door/window is closed again.

Thank you very much for your help.

BR Philipp

I just saw, that I should be able to create a list of all entities in an are by using area_entities(are_name(trigger.entity_id))
How can I now turn off all climate.* entities in that list?

{{ trigger.entity_id | area_name | area_entities | select('match', 'climate') | list }}

Thank you very much!

How can I then set the state of each climate-entity in that list to off?

Use the climate.turn_off service call.

Thanks!
I now have sth like this:

{% for item in area_entities(are_name(trigger.entity_id)) | select('match', 'climate') %}
{% if is_state('trigger.entity_id', 'off') %}
climate.turn_off
{% else %}
climate.turn_on
{% endif %}
{% endfor %}

But now i don’t know how to put this into an automation. Can you help once again?

BR

I suggest something like this:

alias: example
trigger:
  - platform: state
    entity_id:
      - binary_sensor.kitchen_window
      - binary_sensor.front_door
      - binary_sensor.bedroom_window
      - binary_sensor.rear_door
    to: 'on'
    for:
      seconds: 15
condition: []
action:
  - service: climate.turn_off
    target:
      entity_id: "{{ trigger.entity_id | area_name | area_entities | select('match', 'climate') | list }}"

If any of the listed doors or windows are opened for more than 15 seconds, the automation is triggered and turns off the climate entity in the associated area.


EDIT

It appears that a template is unacceptable for entity_id when used with the climate.turn_off service. :thinking:

Okay, i tried the following:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.openclose_5
      - binary_sensor.openclose_6
      - binary_sensor.openclose_7
      - binary_sensor.openclose_8
      - binary_sensor.openclose_15
      - binary_sensor.openclose_16
      - binary_sensor.tur_fensterkontakt
      - binary_sensor.tur_fensterkontakt_2
    for:
      seconds: 15
condition: []
action:
  - service: climate.turn_off
    target:
      entity_id: "{{trigger.entity_id | area_name | area_entities | select('match', 'climate') | list}}"

This shows an error when trying to save the automation:

Message malformed: not a valid value for dictionary value @ data[‘action’][0][‘target’][‘entity_id’]

I then tried the following which saves without an error:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.openclose_5
      - binary_sensor.openclose_6
      - binary_sensor.openclose_7
      - binary_sensor.openclose_8
      - binary_sensor.openclose_15
      - binary_sensor.openclose_16
      - binary_sensor.tur_fensterkontakt
      - binary_sensor.tur_fensterkontakt_2
condition: []
action:
  - service_template: >-
      {% if is_state('trigger.to_state', 'off') %} climate.turn_off {% else %}
      climate.turn_on {% endif %}
    target:
      entity_id: >-
        {{ area_entities(area_name(trigger.entity_id)) | select('match',
        'climate') }}

But when executing nothing happens and the debugger says:

Error: Template rendered invalid entity IDs: <generator object select_or_reject at 0x7f7a71be40>

Any ideas how to solve this?

You’re absolutely right, the example I posted uses entity_id in a way that is unacceptable for climate.turn_off.

Normally, when you use target with entity_id, the entity_id option accepts a template. The service calls for the climate integration appear to deviate from this norm because, according to the documentation, they only accept a “String or list of strings” as described here:

Service data attribute Optional Description
entity_id yes String or list of strings that define the entity ID(s) of climate device(s) to control. To target all climate devices, use all.

What I suggest is to simply avoid using entity_id and to use area_id. It supports templates and the required template is even simpler.

OK, this version definitely passes check configuration on my system:

alias: example 999
trigger:
  - platform: state
    entity_id:
      - binary_sensor.openclose_5
      - binary_sensor.openclose_6
      - binary_sensor.openclose_7
      - binary_sensor.openclose_8
      - binary_sensor.openclose_15
      - binary_sensor.openclose_16
      - binary_sensor.tur_fensterkontakt
      - binary_sensor.tur_fensterkontakt_2
condition: []
action:
  - service: "climate.turn_{{ 'off' if trigger.to_state.state == 'on' else 'on' }}"
    target:
      area_id: '{{ trigger.entity_id | area_id }}'

EDIT

Correction. Change area_name to area_id filter.

1 Like

Now it works! Thank you so much! :slight_smile:

I had to make a small adjustment, since we need the ared_id instaead of area_name for the service to work.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.openclose_5
      - binary_sensor.openclose_6
      - binary_sensor.openclose_7
      - binary_sensor.openclose_8
      - binary_sensor.openclose_15
      - binary_sensor.openclose_16
      - binary_sensor.tur_fensterkontakt
      - binary_sensor.tur_fensterkontakt_2
    for:
      seconds: 15
condition: []
action:
  - service: climate.turn_{{ 'off' if trigger.to_state.state == 'on' else 'on' }}
    target:
      area_id: '{{ area_id(trigger.entity_id) }}'
1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.