Automatically find state of particular sensors without using group?

Hi everybody,

I use anniversaries and am trying to integrate this in a script to create a daily briefing. The integration creates sensors such as this sensor.anniversary_<name>.

I’d like to find out whether any of those sensors’ current state = 0. And then do something like this

{% set current_anniversary = <code to determine the entity_id of the sensor here>  %}
{% set is_annivery = <boolean whether there is an anniversary today here> %}
{% if is_anniversary == true %}
  Today is {{ state_attr("current_anniversary", "friendly_name") }} ( {{ state_attr("current_anniversary", "current_years") }} years ).
{% else %}
  No anniversary today.
{% endif %}

When states("sensor.anniversary_<name>") | int = 0 then there is an anniversary today. But since there are multiple of these sensors (and the exact amount changes each time an anniversary is added/removed). I am still having a hard time with jinja2; my first idea was to create a group containing all relevant sensors, then expanding this group - but that’d require me to manually add/remove each newly added/removed sensor to/from this group each time I do so.

Can this be automated somehow? I’d guess so, as all relevant sensors start with sensor.anniversary_, but I don’t know how.

Thank you in advance for your input :slight_smile:

Give this a try:

{% set ns = namespace(entity_ids=[]) %}
{% for state in states.sensor
   if state.object_id.startswith('anniversary_') 
   and state.state | int == 0 %}
  {% set ns.entity_ids = ns.entity_ids + [state.entity_id] %}
{% endfor %}
{% for entity_id in ns.entity_ids -%}
  Today is {{ state_attr(entity_id, 'friendly_name') }} ( {{ state_attr(entity_id, 'current_years') }} years ).
{% else %}
  No anniversary today.
{% endfor %}
1 Like

Perfect, thank you so much. I gotta admit that I don’t quite understand what’s going on there, but it works :smiley: Good enough for now, but hopefully I’ll be able to build templates like this eventually.

@pnbruckner could you please help me once more with a quite similar template?

I am trying to display all doors and windows that are currently open. I can do this in lovelace via custom:auto-entities, but this is for a telegram notification. I tried to adapt your template to it, but it won’t work:

            {% set tueren = namespace(entity_ids=[]) %}
            {% set fenster = namespace(entity_ids=[]) %}
            {% for state in states.binary_sensor %}
              {% if state.object_id.endswith('tuer_contact') and state.state == 'on' %}
                {% set tueren.entity_ids = tueren.entity_ids + [state.entity_id] %}
              {% endif %}
              {% if state.object_id.endswith('fenster_contact') and state.state == 'on' %}
                {% set fenster.entity_ids = tueren.entity_ids + [state.entity_id] %}
              {% endif %}
            {% endfor %}
            🚪 offene Türen
            {% for entity_id in tueren.entity_ids -%}
              - {{ state_attr(entity_id, 'friendly_name') }} ({{ states.entity_id.last_updated }})
            {% endfor %}
            🪟 offene Fenster
            {% for entity_id in fenster.entity_ids -%}
              - {{ state_attr(entity_id, 'friendly_name') }} ({{ states.entity_id.last_updated }})
            {% endfor %}

Will return

           🚪 offene Türen
            - BO (None)
            - JO (None)
            - SZ (None)
            - WZ (None)
            
            🪟 offene Fenster
            - BO (None)
            - JO (None)
            - SZ (None)
            - WZ (None)
            - Vorne WZ (None)

I tried all sorts of variations for ({{ states.entity_id.last_updated }}), for e xample, state_attr(entity_id, 'last_updated'), and everything I could think off, but it won’t display any actual time value.

Also, there is at least one window AZ that is not in the list. I quadrouple-checked and it is definitely open - both physically and according to developer tools => state.

{% set ns = namespace(tueren=[], fenster=[]) %}
{% for state in states.binary_sensor|selectattr('state', 'eq', 'on') %}
  {% if state.object_id.endswith('tuer_contact') %}
    {% set ns.tueren = ns.tueren + [state] %}
  {% elif state.object_id.endswith('fenster_contact') %}
    {% set ns.fenster = ns.fenster + [state] %}
  {% endif %}
{% endfor %}
offene Türen
{% for state in ns.tueren -%}
  - {{ state.attributes.friendly_name }} ({{ state.last_updated }})
{% endfor %}
offene Fenster
{% for state in ns.fenster -%}
  - {{ state.attributes.friendly_name }} ({{ state.last_updated }})
{% endfor %}
1 Like

Wow! Thank you so much, works perfectly!

1 Like