Help with automation for any entity within device_class

I’m trying to get this automation to fire for every entity with device_class of door. When the state changes I want to get notified. I also don’t want to have to list every entity_id manually, looking for an automated way to get this automation to work. Thoughts?

Can’t quite figure out the trigger.

- id: '1590437448747'
  alias: Notify if any door opens
  description: ''
  trigger:
    platform: template
    value_template: '{%- for item in states.binary_sensor -%}{%- if item.attributes.device_class == "door" -%}true{%- endif -%}{%- endfor -%}'
  condition:
    - condition: template
      value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
  action:
  - data: {}
    service: notify.discord_webhook
    data_template:
        message: '
        {% set option = trigger.to_state.state %}
        {{trigger.to_state.name}} is now {{ "open" if option == "on" else "closed" }}'

I have something similar for lights; and can likely be modified to do what you want. Code below. You can’t use a template like you propose as it’s not updated as not specific entities.

You can make it into a sensor, and trigger on that, as you can specify it to update on the sensor.time.

I prefer just using the event trigger

##########################################################
## Update brightness when light turned on
##########################################################
- alias: Adjust brightness as lights are turned on
  initial_state: True

  trigger:
    - platform: event
      event_type: state_changed
      
  condition:
    - condition: template
      value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
    
    - condition: template
      value_template: "{{ trigger.event.data.new_state.state == 'on' }}"
      
    - condition: template
      value_template: "{{ trigger.event.data.old_state.state == 'off' }}"
   
    # Don't change these lights.
    #   - condition: template
    #     value_template: >-
    #       {{ trigger.event.data.entity_id != 'switch.outdoor_front_switch' }}
   
  action:

    - event: event_update_lights
      event_data:
        state: 'on'

Interesting approach. This won’t give me the name of the triggered entity will it? Or even, will it only give me 1 name at a time if 2 doors open (2 separate notifications)?

Or do I have to specify each entity_id for that granularity?

You can decode the trigger entity to get this info:

#    - service: logbook.log
#      data_template:
#        name: >-
#          Auto Dim: {{ trigger.event.data.entity_id }}
#        message: >-
#          {{ trigger.event.data.old_state.state }}
#          {{ trigger.event.data.new_state.state }}

This was helpful, thanks! After reading this and the documentation and learning how to watch events, my final working automation is

- id: '1590615795365'
  initial_state: true
  alias: Door Notification
  description: ''
  trigger:
  - event_data: {}
    event_type: state_changed
    platform: event
  condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == "door" }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state != trigger.event.data.old_state.state }}'
  action:
  - data_template:
      message: '{{ trigger.event.data.new_state.attributes.friendly_name }} is now {{ "open" if trigger.event.data.new_state.state == "on" else "closed" }}'
    service: notify.discord_webhook

Great, you can probably clean up the message a bit using the technique below with replace:

  - service: logbook.log
      data_template:
        name: >-
          DOOR: {{ trigger.to_state.attributes.friendly_name }}
        message: >-
          {{ trigger.from_state.state|replace('on', 'open')|replace('off', 'closed') }} -> {{ trigger.to_state.state|replace('on', 'open')|replace('off', 'closed') }}

I did something similar for my zones and an input_select.destinations and a select.destinations for use with Waze travel times…
Dynamic list of destinations based on zones and useable by Waze

Hey guys, also have an automation with the same trigger:

alias: Feuer erkannt
description: ''
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == ''smoke'' }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state == ''on'' }}'
action:
...

It works but sometimes throws the following error in the log:

2022-03-14 08:50:16 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{{ trigger.event.data.new_state.attributes.device_class == 'smoke' }}'

Could i solve this with a change on the first condition to:

value_template: '{{ (trigger.event.data.new_state is defined) and (trigger.event.data.new_state.attributes.device_class == ''smoke'') }}'

What do you think, how i can solve this?
thx