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.
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.
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
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.