Using variable / helper for grouping entities and reducing redundancy

Hey guys,

I’m new in HA…
I’m struggling with maybe a simple problem. I want to reduce the redundancy in my automation for my entities. So I wrote two times something like this:

{{ expand('sensor.sensor_fenster_klein_badezimmer_contact_state', 
    'sensor.sensor_fenster_gaste_wc_contact_state',
    'sensor.sensor_fenster_gastezimmer_contact_state',
    'sensor.sensor_fenster_kuche_contact_state',
    'sensor.sensor_fenster_wohnzimmer_contact_state',
    'sensor.sensor_haustur_contact_state',
    'sensor.sensor_hwr_tur_contact_state',
    'sensor.sensor_terrassentur_esszimmer_contact_state',
    'sensor.sensor_terrassentur_wohnzimmer_contact_state',
    'sensor.sensor_fenster_hwr_contact_state')
	| selectattr('state', 'search',
    '(Tilted|Open)') }}

one time a condition and one time the same in an action. So how can I prevent myself from repeating?
I just tried to use group helper and got a new entity “sensor.alle_fenster_turkontakte”:

But when I change my code to

{{ expand('sensor.alle_fenster_turkontakte')
	| selectattr('state', 'search',
    '(Tilted|Open)') }}

nothing happens. So I think ‘sensor.alle_fenster_turkontakte’ is not set / available?!

But what is the recommended way for this issue?

By the way this is my whole YAML of the automation with the redundancy:

alias: Bei Alarmanlage an, Fenster/Türen offen prüfen
description: ""
trigger:
  - platform: state
    entity_id:
      - scene.homepilot_alarmanlage_an
    from: null
    to: null
    id: Alarmanlage_An
  - platform: state
    entity_id:
      - scene.homepilot_gute_nacht
    from: null
    to: null
    id: Gute_Nacht
condition:
  - condition: template
    value_template: "{{ expand('sensor.sensor_fenster_klein_badezimmer_contact_state', \n    'sensor.sensor_fenster_gaste_wc_contact_state',\n    'sensor.sensor_fenster_gastezimmer_contact_state',\n    'sensor.sensor_fenster_kuche_contact_state',\n    'sensor.sensor_fenster_wohnzimmer_contact_state',\n    'sensor.sensor_haustur_contact_state',\n    'sensor.sensor_hwr_tur_contact_state',\n    'sensor.sensor_terrassentur_esszimmer_contact_state',\n    'sensor.sensor_terrassentur_wohnzimmer_contact_state',\n    'sensor.sensor_fenster_hwr_contact_state')\n\t| selectattr('state', 'search',\n    '(Tilted|Open)') |  list | count > 0 }}"
action:
  - service: notify.notify
    data:
      title: Fenster / Türen offen
      message: >-
        Folgende Fenster / Türen sind noch offen:

        {% for e in
        expand('sensor.sensor_fenster_klein_badezimmer_contact_state', 
        'sensor.sensor_fenster_gaste_wc_contact_state',
        'sensor.sensor_fenster_gastezimmer_contact_state',
        'sensor.sensor_fenster_kuche_contact_state',
        'sensor.sensor_fenster_wohnzimmer_contact_state',
        'sensor.sensor_haustur_contact_state',
        'sensor.sensor_hwr_tur_contact_state',
        'sensor.sensor_terrassentur_esszimmer_contact_state',
        'sensor.sensor_terrassentur_wohnzimmer_contact_state',
        'sensor.sensor_fenster_hwr_contact_state') | selectattr('state',
        'search', '(Tilted|Open)') %} - {{ e.name }} {% endfor %}
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.homepilot_alarmanlage_aus
mode: single

Thanks in advance!

A list of all members of a Sensor Group entity is contained in its entity_id attribute.

{{ expand(state_attr('sensor.alle_fenster_turkontakte', 'entity_id'))
  | selectattr('state', 'search', '(tilted|open)') | list | count > 0 }}

To report their names, you can do it like this:

{{ expand(state_attr('sensor.alle_fenster_turkontakte', 'entity_id'))
  | selectattr('state', 'in', ['Tilted', 'Open']))
  | map(attribute='name')
  | list | join ('\n') }}

Question for you:

In Developer Tools > States, does sensor.sensor_fenster_gaste_wc_contact_state report its state in titlecase (Tilted) or in lowercase (tilted)?

Home Assistant normally uses lowercase for state values.

FYI is_state can be used as a test now without needing to use the process hungry expand.

Also, is_state accepts lists.

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') 
   | select('is_state', ['Tilted', 'Open']) | list | count > 0 }}

Or using states as a filter.

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') 
   | map('states') | select('search', '(Tilted|Open)') | list | count > 0 }}

I think something else is wrong. When I use the template editor just for this expression:

{{ expand(state_attr('sensor.alle_fenster_turkontakte', 'entity_id')) 
   | list | join('\n') }}

I’m getting nothing… (I just removed filtering on the state to reduce the error cause).

But this works:

{{ states.sensor.alle_fenster_turkontakte.attributes.friendly_name }}

and results into:
Alle Fenster-/Türkontakte

In Developer Tools > States, does
sensor.sensor_fenster_gaste_wc_contact_state report its state in titlecase
(Tilted) or in lowercase (tilted)?

these are attributes of the entity “sensor.sensor_fenster_gaste_wc_contact_state”:
options: Open, Tilted, Closed
device_class: enum
icon: mdi:square
friendly_name: Fenster Gäste-WC

It’s not lowercase, I think because of the rademacher integration: https://github.com/peribeir/homeassistant-rademacher

By the way these are attributes of the helper group entity “sensor.alle_fenster_turkontakte”:
device_class: enum
icon: mdi:window-open
friendly_name: Alle Fenster-/Türkontakte

So whats wrong?

You won’t because you cant join state objects, you have a syntax error.

Try this instead

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') }}

Test this in the Template Editor:

{{ expand(state_attr('sensor.alle_fenster_turkontakte', 'entity_id')) 
   | map(attribute='name') | list | join('\n') }}

If this is an enum sensor, you’ll have to post the raw state’s of the entities. Enums are translated states, the state you see in the UI is not the actual state of the entity.

Post the result of this please

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') | map('states') | list }}

Look at how the sensor’s state value is displayed in Developer Tools > States.

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') }}

Result:
None

{{ expand(state_attr('sensor.alle_fenster_turkontakte', 'entity_id')) 
   | map(attribute='name') | list | join('\n') }}

Result: Nothing

{{ state_attr('sensor.alle_fenster_turkontakte', 'entity_id') | map('states') | list }}

Result:
(empty array)

Maybe my group helper entity is wrong?! Where can I find the YAML for it?

You have the wrong entity_id, or the helper doesn’t exist.

If you created the helper via the UI, there is no yaml. Find it in the UI and look at the entity_id.

But as I wrote this works:

{{ states.sensor.alle_fenster_turkontakte.attributes.friendly_name }}

and results into:
Alle Fenster-/Türkontakte

Ok, is that a helper though? Or a sensor group?

sensor.alle_fenster_turkontakte

click on the group options

Screenshot in the post above. But here again:

Go to Developer Tools > States and find sensor.alle_fenster_turkontakte in the list.

Does it have an entity_id attribute?

If it’s a Sensor Group, it should have that attribute and its value should contain a list of all sensors that are members of the group.

I just renamed the friendly name… (but not the entity_id).

I don’t believe sensor groups work with enums, and that is the root of your problem.

You’ll have to make a traditional group via yaml.

okay, is there another option? How to use a traditional group via yaml?

Using the group domain via yaml

group:
  alle_fenster_turkontakte:
    name: Alle Fenster-/Türkontakte
    entities:
    - sensor.entity_1
1 Like