Select devices and entities by label

Apologies if this is the wrong section in which to ask this question.

Is there a plan to allow devices and entities to be addressed/selected using newly introduced labels in things such as automations and scripts. For example, a UI display card that shows all entities labelled as “Battery” and an automation that turns of all devices labelled “Lights” and “Sockets” when leaving the house?

I think such a capability would be incredibly powerful and would simplify HASS tremendously.

Thank you in advance

This can already be done in automations and scripts. There are also some cards that already support it on the frontend as well (Auto Entities, etc).

1 Like

Incredible work. Thank you for pointing out the capabilities.

Keep up the good work lads.

1 Like

How does one select trigger devices with a specific label?

I would like to set the trigger part so that any device labelled “humidity” gets to trigger an automation if humidity levels reach a certain threshold? or for devices labelled “radiator” to trigger an event when they begin heating.

For something like that, you’d want to use a template as your trigger and use the label_entities() function to get all the entities with that label. I’m currently mobile at the moment, but this [crappy :rofl:] example should get you started. I don’t have any humidity labels, but the concept is pretty much the same.

{% set lights_on = false %}
{% for entity in label_entities("light") %}
  {% if is_state(entity, 'on') %}
    {% set lights_on = true %}
    {% break %}
  {% endif %}
{% endfor %}
{{ lights_on }}

However, that only applies to entities. You don’t really trigger off devices, just entities.

1 Like

@code-in-progress do you know if there is a plan to add the option to select triggering entities by label to the automations UI?

1 Like

I’ve not seen anything that suggests triggering off a label.

I have 12 different water pump (I am on an island). I had an incident where one of my pumps stayed open and I lost over 2000 gallons of water (like losing gold here!)

I have setup a bunch of session durations using measureIt, so I now have these sensor tell me how long a pump is running or a valve is open - in seconds.

I have set a label on each of these “duration_under_10m”.

What I am trying to do is setup an automation for any label_entities(“duration_under_10m”) > 600 to notify me. But I am really battling with the use of selectattr, select and also sorts of other stuff that I cannot figure out after stumbling around for 3 hours :frowning: Please can someone help me with the code to trigger anything with the label being great than 600.

Thanks in advance!!!

Creating an automation that is triggered by changes to entities with a specific label and / or area is not the most simple of tasks.

This is the solution I have come up with to have the ability to trigger an automation based on entities having a label (‘motion_sensor’).

I am using a template sensor to monitor changes to entities with a specific label

In my example, I am monitoring occupancy. I am watching every motion sensor in and around the property (PIRs, mm Waves and cameras that provide motion sensors)

This is the template sensor I use

# -----------------------------------------------------------------
# sensor.room_motion_sensors_on_count
# -----------------------------------------------------------------
# When any motion sensor gets triggered to *ON* state we store the
# following in this sensor...
# -----------------------------------------------------------------
#  1. state:                  - MD5 Hash of the names of the Sensors that are on (so we can respond to changes)
#                                   The list of sensors (attributes.entities_on) is always sorted prior to 
#                                   hashing because we do not want the hash to change when the same sensors are 
#                                   provided to this sensor, but in a different order to the previous list that
#                                   was provided 
#  2. attributes.count_on:    - The Count of Motion Sensors in the on state
#  3. attributes.entities_on: - The names of the sensors that are 'on'
#                               time it came on in the attributes of this sensor.
# -----------------------------------------------------------------

- sensor:
  - unique_id: room_motion_sensors_on_count
    name: room_motion_sensors_on_count
    # We use md5 hash of the sensors that are on
    # Just using the count would not really indicate a state change.
    # If this sensor reported 4 sensors being on, they may not be the
    # same 4 sensors as was reported in the prior update
    state: >
      {%- from "room_motion.jinja" import room_motion_sensors_on_md5 -%}
      {{- room_motion_sensors_on_md5() -}}
      count_on: >
        {%- from "room_motion.jinja" import room_motion_sensors_on_count -%}
        {{- room_motion_sensors_on_count() -}}
      entities_on: >
        {%- from "room_motion.jinja" import room_motion_sensors_on -%}
        {{- room_motion_sensors_on() -}}

These are the supporting macros from ‘/config/custom_templates/room_motion.jinja’

This Macro returns a list of entity_id of every motion sensor in the *ON* state
These are sorted so that the MD5 changed less often
@returns string - a comma-separated list of entity_id
{%- macro room_motion_sensors_on() -%}
    {{- label_entities('motion_sensor')
    | expand
    | selectattr('state', 'eq', 'on')
    | map(attribute='entity_id')
    | list
    | sort -}}
{%- endmacro -%}

This Macro returns a count of the result of room_motion_sensors_on()
{%- macro room_motion_sensors_on_count() -%}
    {{- ( room_motion_sensors_on().split() ) | count -}}
{%- endmacro -%}

This Macro returns the md5 hash of the result of room_motion_sensors_on()
{%- macro room_motion_sensors_on_md5() -%}
    {{- room_motion_sensors_on()| md5 -}}
{%- endmacro -%}

This is the developer tools view of the template sensor


Hi teskanoo

Wow, I learned more from your post on templates that anything in the past year! When I first saw it, it seemed too complicated and I almost gave up. But then, even though I have never encountered a “macro” in HA before, it made sense!!!

One small question please, how can I use “greater than” logic in the selectattr(‘sate’ portion of your macro?


Hi Wayne. I’m glad that helped.

You can use …
‘gt’ for greater than
‘lt’ for less than
‘ge’ for greater than or equal to

selectattr('temperature', 'gt', 21)

Here is a link to more documentation of the various tests and filters.