Automation with Entity-Domain as trigger and not Entity List

Dear All

Is there a simple way of writing an automation that uses all entities of a specific domain as trigger?

I am using a large amount of entities and it would be super handy to have a whole domain as trigger…

I also checked some forum posts and feature requests on github about similar ideas (like Regex Wildcard / Entitiy Groups -> Which are not supported - for good reasons)

Thank you very much for your help.
Best regards
Fabian Baumann

1 Like

I’d just use an event trigger and filter in a condition.

I’m assuming youw ant a state change, so here’s an example of that.

mode: parallel # Need to be able to do multiple of these at a time
max: 10        # Set the max to handle at once, based on CPU probably
trigger:
  platform: event
  event_type: state_changed
condition:
  - "{{ 'my_domain' == trigger.new_state.domain }}"
action:
  ...

You might have to set this one to parallel mode if you have a ton of state_changes happening so you can handle each one at the same time.

1 Like

Thank you very much for your response, i tried to test the event platform but I did not manage to get the automation firing.

  - alias: "Testautomation EVENT"
    trigger:
      platform: event
      event_type: state_changed
    mode: parallel
    max: 25
    condition: 
      - '{{ trigger.new_state.domain == "sensor" }}'
    action:
      - service: notify.telegram
        data:
          title: Testtitel
          message: Test

Using the developer tools i could find the relevant event. Am I missing something?

Oh, yeah…my bad. Its’ “trigger.event.data.new_state” lol.

"{{ trigger.event.data.new_state.domain == 'sensor' }}"

The ‘domain’ doesn’t show up in the data. It is a property of the state object itself.

1 Like

Thank you very much for the help. I just tested the automation with the domain filter and it works perfect…

Follow up question:
Is there a way to check for a delay? We would like to have an automation that will fire 5 minutes after ariving at home.

Example:

  - alias: "Testautomation that should fire 5 minutes after coming home"
    trigger:
      - platform: event
        event_type: state_changed
    mode: parallel
    max: 25
    condition: 
      condition: and
      conditions:
        - condition: template 
          value_template: '{{ trigger.event.data.new_state.domain == "person" }}'
        - condition: template 
          value_template: '{{ trigger.event.data.new_state.state == "home" }}'
    action:
      - some action which is not relevant

I know that the automation like that is working but we prefere to filter on the domain, beause we have about 50 people to track:

  - alias: "Automation using States Platform"
    trigger:
      - platform: state
        entity_id:
          - person_health.fabian_baumann
          - etc.
        to: "home"
        for: "00:05:00"
    action:
      - some action which is not relevant

Thank you very much for your help and best regards
Fabian Baumann

The ‘for’ clause just makes sure the entity was in that state for at least that long. It’s to help with false updates and debouncing.

If you just want to wait, just wait in the actions.

action:
  - delay:
      minutes: 5
  - < my other actions >

If you want a wait with an abort, we can use a wait_template

action:
  # Wait for 5 minutes to see if this entity transitions away from home.
  # As soon as this transitions state, the wait_template will finish. 
  # Or, the timeout will happen.
  - wait_template: "{{ not is_state_attr(trigger.event.data.entity_id, 'home') }}"
    timeout: "00:05:00"
    continue_on_timeout: true
  - condition: template
    # wait.completed is true if the condition was met. False otherwise. 
    # Halt the sequence if the condition was met. i.e. return false if the state went away from home.
    value_template: "{{ not wait.completed }}"
  # Timeout happened. This person is still home. Do the rest of the actions. 
  - my_normal_actions ...
1 Like

Perfect, Thank you very much for your help :+1:

Thank you very much for the template. Actually the trigger.event.data.entity_id is not defined in that context or am i missing something?

  - alias: "Test"
    trigger:
      - platform: event
        event_type: state_changed
    mode: parallel
    max: 25
    condition: 
      condition: and
      conditions:
        - condition: template 
          value_template: '{{ trigger.event.data.new_state.domain == "person" }}'
        - condition: template 
          value_template: '{{ trigger.event.data.new_state.state == "home" }}'
    action:
      - service: notify.telegram
        data:
          title: |
            *Test Trigger State Object*
          message: |
            {{ trigger.event.data.entity_id }}
            {{ states('trigger.event.data.entity_id') }}
            {{ is_state('trigger.event.data.entity_id', 'home') }}
      - wait_template: '{{ not is_state("trigger.event.data.entity_id", "home") }}'
        timeout: "00:00:05"
        continue_on_timeout: true
      - condition: template
        value_template: "{{ not wait.completed }}"
      - service: person_health.overwrite_state
        data_template:
          entity_id: '{{ trigger.event.data.entity_id }}'
          state: 'OK'

unknown

Somehow states(‘trigger.event.data.entity_id’) is not returning “home”. According to the docs i think it should. But maybe i am wrong.

Yeah, don’t put quotes around it. Quotes would mean you pass in literally “trigger.event.blah”. We want the string that that variable represents instead. So, no quotes around all of them.

so states(trigger.xxxx), is_state(trigger.xxx), state_attr(trigger.xxx), etc.

1 Like

It works :partying_face:
Thank you so much…