Hello
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.
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 ] 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.
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 Please can someone help me with the code to trigger anything with the label being great than 600.
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.
# -----------------------------------------------------------------
template:
- 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() -}}
attributes:
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
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,
My first post here, and i bring some details on a solved issue : sorry for that, but this topic helped me a lot so i thought i could fire back
As Bill (thank you) stated, the “example got me started” : it doesn’t work out of the box and my understanding is that it’s because of variable scope. This (slightly modified) template works for me :
{% set ns = namespace(lights_on=false) %}
{% for entity in label_entities("light") %}
{% if is_state(entity, 'on') %}
{% set ns.lights_on = true %}
{% endif %}
{% endfor %}
{{ ns.lights_on }}
Sorry to hop on top of this thread. Most of my lights are Z-wave and using the built in UI to select all lights in a label like this results in a lot of slowness with any command.
So I have an automation that employs a 200ms delay between each of the calls to make sure the Z-wave network isn’t too flooded (this is probably a separate issue to solve another time). I manually created all of these delays between calls using the visual editor and my YAML syntax is pretty poor but is there an easy way to make a script that does something like:
For each entity e with label “Interior Light”:
light.turn_off(e)
delay 200ms
I consulted Claude and got stuck with too many errors