Best way to build a trigger for any device of the same class?

Is there a better way to do this in HA automations? This works but it makes the script debugger useless as it triggers on every state change then filters for the ones I want. There are state changes every few seconds so the debug history rolls over instantly.

Main goal here is to have an always active automation that notifies if ANY Leak / Moisture sensor added to HA changes state. Maintaining individual entity IDs or groups of entity IDs seems unsafe once you get into having larger numbers of the same type of device that has the same behaviour.

alias: 'Notification: Water Leak'
description: Notification for all Moisture sensors
trigger:
  - platform: event
    event_type: state_changed
    event_data: {}
condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == "moisture" }}'
  - condition: template
    value_template: >-
      {{ trigger.event.data.new_state.state !=
      trigger.event.data.old_state.state }}
  - condition: template
    value_template: '{{ trigger.event.data.old_state.state != ''unknown'' }}'
action:
  - service: notify.notify
    data:
      title: Leak / Water Detected!
      message: >-
        Leak notice, {{ trigger.event.data.new_state.attributes.friendly_name }}
        is now {{ "wet" if trigger.event.data.new_state.state == "on" else "dry"
        }}
1 Like

Hello,

I found this in a blueprint and it works well:
I bedazzled it a little.

alias: Leak Detection - Notifier and Shutdown
description: ''
trigger:
  - platform: event
    event_type: state_changed
    event_data: {}
condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == "moisture" }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state == "on" }}'
action:
  - service: script.alexa_disable_do_not_disturb_all_devices
    data: {}
  - type: turn_off
    device_id: dfd03c8879f516f34824e0ade307bdf8
    entity_id: switch.water_supply_valve
    domain: switch
  - service: scene.turn_on
    target:
      entity_id: scene.leak_detection_scene
    data: {}
  - service: notify.mobile_app_sarge_s_phone
    data:
      message: >-
        {{ trigger.event.data.new_state.attributes.friendly_name }} has detected
        a leak.
  - service: notify.mobile_app_iphone
    data:
      message: >-
        {{ trigger.event.data.new_state.attributes.friendly_name }} has detected
        a leak.
  - service: notify.alexa_media
    data:
      target: >-
        media_player.kitchen_echo_show,
        media_player.2nd_floor_hallway_echo_dot,media_player.bathroom_1_echo_dot,
        media_player.bedroom_2_echo_show,media_player.bedroom_echo_show,
        media_player.office_echo_show,media_player.garage_entryway_echo_show,
        media_player.garage_echo_dot
      data:
        type: announce
      message: >-
        {{ trigger.event.data.new_state.attributes.friendly_name }} has detected
        a leak.
mode: single

That is basically the same thing, I was asking if there was a more efficient way than listening to all state changes and then filtering.

My code works, I was just wondering if there was a more efficient way.

Also noting that the automation debugger is useless if you do it this way by listing to all state changes.