Automation triggered by any entity with a certain label?

Can anyone comment on if labels could now be used to tag specific entities as “critical” and then have an automation to notify me if any critical sensor goes offline for more than 15 minutes?
Then when I get a new device I can just label one of the entities as “critical” to include it…

This is my automation so far but I’d like to make it more dynamic.

platform: state
entity_id:
  - sensor.ibs_th2_p01b_09a6_temperature
  - sensor.ibs_th2_p01b_06c1_temperature
  - sensor.ibs_th2_p01b_15b8_temperature
  - sensor.ibs_th2_p01b_17f6_temperature
  - switch.athom_plug_v2_04
  - sensor.esphome_07_temperature
to:
  - unknown
  - unavailable
for:
  hours: 0
  minutes: 30
  seconds: 0
id: not available
2 Likes

You can use a template trigger, but that will only work for the first sensor to go offline for 15 minutes. It won’t alert you again if a second sensor goes offline.

platform: template
value_template: "{{ label_entities('critical') | reject('has_value') | list | count > 0 }}"
for:
  minutes: 15

If you want to have it trigger on every sensor, you need to create a template sensor with a count of the sensors which are unavailable, and then use that template sensor in a state trigger.

1 Like

Thanks! Hopefully something available in the future and I’ll just need to hard code the specific sensors for the time being.

I would also be much intrested in this feature. Would give even more meaning to setting labels on entities.

I have sort of found a workaround, but not sure if I will keep it.

  1. Create an automation with an “event”-trigger with the type “state_changed”

  2. Set conditions in an AND-group:

    a.)
    Verifies that the currently triggered entity is contained in the “battery” label collection

    {{ label_entities(“battery”)
    | expand
    | selectattr(‘entity_id’, ‘eq’, trigger.event.data.entity_id)
    | map(attribute=‘state’)
    | list
    | length > 0 }}

    b.)
    Checks for relevant state change.

    {{
    trigger.event.data.old_state.state not in [‘unavailable’, ‘unknown’, ‘none’, ‘off’]
    and
    trigger.event.data.new_state.state in [‘unavailable’, ‘unknown’, ‘none’, ‘off’]
    }}

  3. Add your wanted actions

The BIG down size to this, is that the automation listens on all state_changed events. Feels like a lot of computation that is not really required.

1 Like

Same here, i labeled all alarm-related sensors with an alarm label, and would love to be able to do:

alias: Detect offline alarm devices
description: ""
triggers:
  - label:
      - alarm
    to: unavailable
    for:
      hours: 0
      minutes: 15
      seconds: 0
    trigger: state
conditions: []
actions:
  - action: notify.phone
    metadata: {}
    data:
      message: Lost critical device {{trigger.to_state.name}} !

instead of:

alias: Detect offline alarm devices
description: ""
triggers:
  - entity_id:
      - binary_sensor.first_contact
      - binary_sensor.second_contact
      - binary_sensor.third_contact
    to: unavailable
    for:
      hours: 0
      minutes: 15
      seconds: 0
    trigger: state
conditions: []
actions:
  - action: notify.phone
    metadata: {}
    data:
      message: Lost critical device {{trigger.to_state.name}} !

The workaround mentioned above by TheFes is still the way to do this. Create a template sensor whose state is driven by those labeled entities, then in your automation use a state trigger based off that template sensor.

Created this to trigger by labels.

Note sure about the duration part. Maybe this might give somebody ideas.