God, I’m lost
I’ve been trying to get an automation check my binary sensors for my doors and windows at 10pm if they’re open or close and announce via notify.alexa_media which ones are open (or give it a ‘all clear’ if they’re closed).
I’ve obviously missing something, but I can’t get an ‘open’ notification.
Here’s the code:
alias: Check Doors and Windows at 10 PM
trigger:
- platform: time
at: "22:00:00"
condition: []
action:
- variables:
open_entities: >
{%- set entities = [
'binary_sensor.back_door',
'binary_sensor.front_door',
'binary_sensor.kitchen_door',
'binary_sensor.living_room_patio_door',
'binary_sensor.kitchen_window',
'binary_sensor.study_window',
'binary_sensor.utility_room_window',
'binary_sensor.wc_window'
] -%}
{%- set open_entities = entities | selectattr('state', 'eq', 'on') | list -%}
{{ open_entities }}
- choose:
- conditions: "{{ open_entities | length > 0 }}"
sequence:
- service: notify.alexa_media
data:
target:
- media_player.alexa_kitchen_2
- media_player.bedroom
message: >
The following entities are open:
{{ open_entities | join(', ', attribute='entity_id') }}
- conditions: "{{ open_entities | length == 0 or not open_entities }}"
sequence:
- service: notify.alexa_media
data:
target:
- media_player.alexa_kitchen_2
- media_player.bedroom
message: "All windows and doors are closed"
Please help, why are the open sensors not changing the open_entities with the ones that are “on” status? It always return a value of 0
To quote Ziggy Stardust, I’m an absolute beginner, so I deeply apologise in advance for any lame comment.
This is what Github co-pilot tells me about this piece of code.
The provided code selection appears to be written in YAML syntax. YAML is a human-readable data serialization format commonly used for configuration files.
In this code selection, we have a function called selectattr being used. The selectattr function is typically used in YAML templates or filters to select specific attributes or values from a collection of objects.
The selectattr function takes three arguments: the attribute name, the operator, and the value to compare against. In this case, the attribute name is 'state', the operator is 'eq' (which stands for “equals”), and the value is 'on'.
What this code is doing is selecting objects that have the attribute 'state' equal to 'on'. It filters out any objects that do not meet this condition and returns a new collection with only the selected objects.
My reading of that is that selectattr is looking for the attribute ‘state’ of the binary_sensor. Is that a right assumption? I’m not sure how to understand your message to be honest