Automation - Entitiy_id - Wildcard

I currently use UNIFI to discover devices. Every HA device I label as ha_device_name. I would like to create a single automation for all device_tracker.ha_… I know automations do not currently support wildcards in the entity_id filed. Anybody have any ideas how I can make this happen?

What are you trying to achieve? What’s the goal here?

Notification when a device goes offline(not_home) and comes back online(home) and I don’t want to keep updating the automation when I add a new home automation device. Just name it ha_device_name in unifi and it gets added to the automation.

Then yes with some templating logic to filter out only entity IDs starting device_tracker.ha_.

Probably something like

{% if 'device_tracker.ha_' in entity_id and old_state == 'unavailable' %}
  ...
{% endif %}

No bueno.

trigger:
         - platform: state
           entity_id: >
            {% if 'device_tracker.ha_' in entity_id and old_state == 'unavailable' %}           
            {% endif %}
           from: 'not_home'
           to: 'home'
           for:
             minutes: 5

You will have to use that template in a template trigger, not a state trigger.

easy enough.
template trigger

  - alias: '[Home Automation] Device Status Online'
    trigger:
      platform: template
      value_template: >
        {% if 'device_tracker.ha_' in entity_id and old_state == 'not_home' and new_state == 'home' %}      ...
        {% endif %}
      for: 00:05:00
    action:
      - service: notify.mobile_app_michael_phone
        data_template:
          title: 'Device {{ trigger.to_state.name }} is'
          message: >
            '{{ trigger.to_state.state }}'  

You need an event trigger, with that template in the condition, sorry for not being explicit about how that works.

Something like trigger every 5 mins?

No, an actual event trigger, to use with the state_changed event…

automation:
- alias: Ping me
  trigger:
  - platform: event
    event_type: state_changed
  condition:
  - condition: template
    value_template: "{{ 'device_tracker.ha_' in entity_id and old_state == 'unavailable' }}"
  action:
  (write the actions)
1 Like

Tested the following and no go. Nothing in the logs and automation does not show it fired. Might see if I can setup something with appdaemon.

Device name - device_tracker.ha_lr_broadlink

  - alias: '[Home Automation] Device Status Online'
    trigger:
      - platform: event
        event_type: state_changed
    condition:
      - condition: template
        value_template: "{{ 'device_tracker.ha_' in entity_id }}"        
    action:
      - service: notify.mobile_app_michael_phone
        data_template:
          title: 'Device {{ trigger.to_state.name }} is'
          message: >
            '{{ trigger.to_state.state }}'   

I realise this thread is quite old, but I’ve just been fiddling with this today and got it to work, so thought I’d share for anyone else who comes across the thread.

The problem with the above is that the trigger is an event, so you can’t refer directly to entity_id in the condition. Instead you have to refer to trigger.event.data.entity_id

So mdleal’s code becomes:

  - alias: '[Home Automation] Device Status Online'
    trigger:
      - platform: event
        event_type: state_changed
    condition:
      - condition: template
        value_template: "{{ 'device_tracker.ha_' in trigger.event.data.entity_id }}"        
    action:
    (etc...)

You’d also have to do similar in the action, so trigger.event.data.new_state.state instead of trigger.to_state.state

4 Likes