Automation to detect open windows / lights etc -> Issue with templating and select

Hi all,

I tried really hard but are stuck.

I want to have an automation that notifies me once nobody is home if there are windows open, light on, venetian blinds closed or awnings open.

I want to also include WHAT it is exactly that caused the trigger.

I came up with selecting the entities via labels as this seems structured to me and will help once I include new entities, I just have to add labels to them.

Approach:

conditions:

#Doors/Windows
{{ label_entities('Offen-Melder') | select('is_state', 'on') | list | length >0 }}
#Venetian blinds
{{ label_entities('Jalousien') | map('state_attr','current_position') | select('lessthan',100) | list | length >0 }}
#awnings
{{ label_entities('Markisen') | select('is_state', 'open') | list | length >0 }}
#lights
{{ label_entities('Licht') | select('is_state', 'on') | rejectattr('friendly_name','equalto','Hoflicht') | list | length >0  }}

Message to be sent something like this:

#Doors/Windows
{% for state in label_entities('Offen-Melder') | select('is_state', 'on') %}
{{ state_attr(state, 'friendly_name') }} ist {{ state_translated(state) }},
{% endfor %}
#venetian blinds
 {% for state in label_entities('Jalousien') | map('state_attr','current_position') | select('lessthan',100) %}
{{ state_attr(state, 'friendly_name') }}ist {{ state_translated(state) }},
{% endfor %}
#and so on...

I do get plausible true and false for the conditions, however I was struggling hard with the Venetian blinds condition and it seems to be string comparison, but seems to be correct behavior wise.

However I am not able to use the condition statement for the message. There is an error in the template. I tried to simplify it without the |map but I do seem to have a misunderstanding here.

Can someone help with a hint? The message für Doors/Windows seems to work but not for Venetian blinds as I have to use an attribute as condition, so I struggle with the correct filter.

i’m going to focus (mostly) on venetian blinds… sounds like you have the rest to your liking. is that correct? if not holler…

#Venetian blinds
{{ label_entities('Jalousien') | expand | selectattr('attributes.current_position', 'defined')  | rejectattr('attributes.current_position', 'eq', none)  |  selectattr('attributes.current_position', '<', 100) |  list | length > 0 }}

(EDIT: added expand that @x4N70pHyLL found that i missed initially)

i don’t have any venetian blinds so i don’t know how the entity works… so what i have might be overkill… but i’ve added the “defined” check in case the “current_position” doesn’t exist for some entities. also if the “current_position” is like “brightness” for lights, when it’s 0, the value is “none”… so i check for that. both of those might be unnecessary in your case.

you need to use “attributes.current_position” when using selectattr. i’m surprised that this does what you want:

rejectattr('friendly_name','equalto','Hoflicht') 

i would have expected you to need it to be:

rejectattr('attributes.friendly_name','equalto','Hoflicht') 

regardless, i hope this helps you along with the venetian blinds condition. holler if you need more…

Thanks so much for taking the time!

I did a little try and error in the meantime. It was indeed one problem that I needed to add “ATTRIBUTE.xyz”.

The rejectattr did indeed not work, I seem to have it not properly checked when creating this thread. I now use

reject('eq','light.hoflicht')

to reject it.

Overall the special knowledge needed was (I think) that for label_entities(‘label’) you get back a list of the IDs of the entities, not the objects. So it needed to look like this with expand, then go to the attribute.

{{ label_entities('Jalousien') | expand | rejectattr('attributes.current_position','eq', 100) | list | length >0 }}

Again, thanks for taking the time, I do struggle to get into the code details - sometimes as I never learned filters in my studies and it takes some time to get the concept, but often also because you do not really find many similar examples or snippets and Jinja documentation itself does not always help as well.

great. glad it worked! sorry about the expand miss… i eyeballed it and didn’t test it :frowning: . but you’re right! and i’m glad you figured it out! :slight_smile:

will denote that in mine above so no one is led astray