Using arrays in Automations

I have an automation that checks if a particular (tado) device has detected if a window is open.
I have three such devices, each with their own automation. (Main house, bedroom, office).

I’ve been looking to condense this down into a single automation, which I can do using if’s and checking which of the devices states changed, but if I add more devices, I’ll have to add to the growing list of if statements.

Is there a way to create an array of entities that an automation could iterate through to see which triggered the change ?

All i would then need to do is maintain the entity list.

If that doesn’t directly work for you, you can build a template that expands the group and iterate through that. Post the code you have so far and clearly explain what you’d like to do.

The code so far is very basic, I have 1 automation per device like so:-

alias: "Set Tado: Open Window - Bedroom"
description: Pause heating on Open Window
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bedroom_open_window
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - device_id: a47f7db07a4a9c7633ac807ab9d13457
    domain: mobile_app
    type: notify
    message: "Open Window Detected: Bedroom"
    title: " Tado Alert"
  - service: script.set_tado_open_window_10_bedroom
    data: {}
mode: single

But I also have 2 other devices:-

binary_sensor.main_house_open_window
binary_sensor.office_open_window

The script called just turns off the hvac mode for ten minutes, then turns it back on:-

alias: "Set Tado: Open Window  (10 Minutes) Bedroom"
sequence:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id:
        - climate.bedroom
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: climate.set_hvac_mode
    data:
      hvac_mode: auto
    target:
      entity_id:
        - climate.bedroom
mode: single

It’s generally a bad idea to use long delays because they do not survive restart/reload. You can initiate a timer instead, or use an automation triggered on the windows closings. At a minimum you should include a condition in your script to verify that the windows in the room are closed before turning the heat back on.

1 Like

OK: so you could list all the sensors in the trigger:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.bedroom_open_window
      - binary_sensor.main_house_open_window
      - binary_sensor.office_open_window
    from: "off"
    to: "on"

Then, in the action, you have access to trigger.entity_id which will tell you which sensor has triggered the automation. You could then pass that to your script in the data dictionary.

You’d then need to deal with the mapping of triggered sensors to climate entities in the script, perhaps using choose: Script Syntax - Home Assistant

Alternatively, use the approach linked in the last post!

@Didgeridrew - thanks for the feedback. I used the delay to essentially mimic what tado does, but I get that there are better ways, and I’ll look into that.

As for my original thought, I’ve created a group of windows as suggested by @Troon, so any one of the group can trigger the automation. What I’m not clear about is the syntax to target the right entity to turn off:-

alias: Test of All Windows Open Detection
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.all_open_windows
    to: "on"
    from: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id: climate.bedroom  <--- How do I make this so its the entity from the group that caused the event to trigger.
mode: single

Is your goal to turn off all the climate units if any window is open or only the unit in the room where the window opened?

Only the unit in the room where the window was opened.

That is addressed in my post from the thread linked above. The most critical part for the linked example is that your window sensors (or sensor groups) and climate entities share the same Area designation. Instead of creating a single group containing all your window sensors I would create a group for each Area (you could then combine those into an “all” group for other uses if you want).

alias: Test of All Windows Open Detection
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.group_bedroom1_open_windows
      - binary_sensor.group_bedroom2_open_windows
      - binary_sensor.group_kitchen_open_windows
      - binary_sensor.group_livingroom_open_windows
    to: "on"
    from: "off"
    for:
      seconds: 30
condition: []
action:
  - variables:
      area: "{{ area_name(trigger.entity_id) }}"
      targets: >
        {{ expand(area_entities(area))
        | selectattr('domain','eq','climate')
        | map(attribute='entity_id') | join  }}
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id: "{{ targets }}"
mode: single

There are times when this might not be an appropriate method. It may not work for you if your Areas in HA do not match well with the layout of your climate system. For example, if you have a more open layout where the climate entities of multiple Areas need to be linked to multiple window groups. There are other methods of linking your climate entities to specific windows or window groups if this one doesn’t work for you.