I was hoping I could get some help, I’m extremely knew to templatign/Jinja2. I’m trying to just get an alert if something is open. I found someones example template and was able to modify it to suite my needs however I feel that its very verbose. I think the message portion is fine but the “title” portion seems like overkill, I just couldn’t figure out how to stick “Title” within the message template, if that makes sense. I basically just don’t want a message at all if nothing is open, and if something is open I want the message and the title.
data_template:
message: >
{% set open_things = states | selectattr('entity_id', 'in',
state_attr('group.downstairs_openings','entity_id')) |
selectattr('state','in',['on','open']) | map(attribute='name') | list %}
{% if open_things | length == 1 %}
The {{ open_things[0] }} is open.
{% elif open_things | length > 1 %}
The {{ open_things[:-1] | join(', ') }}{{',' if open_things | length > 2 else ''}} and {{ open_things[-1]}} are open.
{% endif %}
title: >
{% set open_things = states | selectattr('entity_id', 'in',
state_attr('group.downstairs_openings','entity_id')) |
selectattr('state','in',['on','open']) | map(attribute='name') | list %}
{% if open_things | length >= 1 %}
Something is open!
{% endif %}
service: notify.notify
NOTE:
If the group contains only binary_sensors then you don’t need to check if the state can be open because a binary_sensor’s state is on or off. However, if the group contains Template Sensors that report open and closed then you do need to check if the state is either on or open.
Your condition’s template does what I proposed but takes a longer route. It begins by acquiring a collection of State Objects for every entity (that’s what states does) then whittling it down by only selecting those whose entity_id matches the entity_id of the group’s members but only those members whose state is either on or open. If that sounds like a long-winded description, it is, but that’s what the template is doing.
The template I proposed begins by expanding the group into a collection of State Objects. Then it selects all objects whose state is on. The rest is the same as your template.
It isn’t checking if the entity’s state is either on or open because it assumes all members of the group are binary_sensors (a binary_sensor’s state is never open). Does the group contain anything other than binary_sensors?