#Using ``trigger.event.data.device_id`` response to automate an event.

1 messages · Page 1 of 1 (latest)

brazen swift
#

During a tag_scanned event I get an ID which is formatted as a1a4bb240f7ecbd60cbc3515164b0168 and I want to use this in an action IE

  - data:
      song_str: >-
        read:
        'simpsons:d=4,o=5,b=160:32p,c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g'
    action: "esphome.{{trigger.event.data.device_id}}_play_rtttl"

But this fails, I'm wondering if I need to change the device_id to the device_name?

I've tried

message: "esphome.{{ device_attr('trigger.event.data.device_id', 'id') }}_play_rtttl"
and

name..

Essentially I want the device that creates the tag_scanned to have an automation delivered at it.. Perhaps I should be using blueprints but right now I want to get my head around the ID's assigned by HA and how to access those properties reliably (IE can I target a device by it's ID (as represented above) or do I need some other ID?

I was expecting the ID to be ``esphome_web_dee4ac` as that's what is presented to me as the humanly readable name in HA

#

Now I'm wondering if I need to iterate over each entity within the device_id to find rtttl and set that as my target action ID?

thorn jetty
#

In that last screenshot you posted, you need to click on the “go to YAMl mode” button on bottom. Then you will see what the target action actually is

#

And, assuming your trigger in your automation is set up properly (because you haven’t shared it), after you scan a tag and your automation triggers (but then doesn’t do anything)… go to the automation trace and check out the variables. It will tell you exactly what is in the trigger variable. If you can’t figure it out, click the three dots on top left while looking at the trace, and select “download trace”. Then share that data.

brazen swift
#
action: esphome.esphome_web_dee4ac_play_rtttl
data:
  song_str: test

Is the action YAML.. It doesn't specify an entity ID or device ID.

#
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: event
  event:
    event_type: tag_scanned
    data:
      tag_id: 04-BD-B5-22-99-3C-80
      name: Tag 04-BD-B5-22-99-3C-80
      device_id: a1a4bb240f7ecbd60cbc3515164b0168
    origin: LOCAL
    time_fired: '2024-12-07T20:43:13.782480+00:00'
    context:
      id: 01JEHDSDFPMA7CJV3WYR3R3ZK0
      parent_id: null
      user_id: null
  description: event 'tag_scanned'
#

Note how the event > context > device_id doesn't give the device_id I expect which I guess would be esphome_web_dee4ac

#

I usually get the entity_id of the target device by doing this

    {% set entities = device_entities(trigger.event.data.device_id) %} {% for
    entity in entities %}
      {% if 'lock.' in entity %}
        {{entity}}
      {% endif %}
    {% endfor %}

but as the action doesn't target an entity I'm not sure how to get the endpoint I want/need.. I feel like I'm missing something like device_actions(...)

thorn jetty
#

There is a different action for each target. ```
action: esphome.esphome_web_dee4ac_play_rtttl

That is specific to one esphome node. There will be a different action for every other node.
#

The name of that action is controlled by the esphome node’s config that is flashed onto each device

#

My wild guess is that the six hexadecimal characters in the action are the last 6 characters of the device’s MAC address.

brazen swift
#

Yea, I get that.. so the device_id is basically esphome_web_dee4ac so I can take that and append _play_rtttl and I have the action /entity/...

#

But the device_id I'm getting from the trigger event is a unique identifier that is in a different format from what I expect to see and I can't target esphome.a1a4bb240f7ecbd60cbc3515164b0168_play_rtttl

thorn jetty
#

No, the device ID is not esphome_web_dee4ac

#

The devicename used in the ESPHome config is esphome_web_dee4ac

#

I think your question is how to get the devicename that is used in the ESPHome config (which has nothing to do with the device_id assigned in home assistant)

brazen swift
#

I wonder if I need name_by_user

#

name_by_user doesn't work.. If I use name I get "Abades Door New" which is the humanly readable name of the device..

#

This could well be the cause

brazen swift
#

tag_scanned isn't firing as a state change but a device trigger... So yea, the name value is not useful and name_by_user is not available.

thorn jetty
#

Do you have the ability to change the esphome YAML?

#

All the device attributes and properties aren’t useful because that is all HA-specific. I can’t think of a way to determine the mode name that ESPHome uses. That information I don’t think is available in HA.

#

But if you manually ensure the HA device name is the same as the esphome node name, then you’d be able to do the lookup

#

You might try asking in the esphome discord if there is a way for HA to know the devicename property used by esphome

thorn jetty
brazen swift
thorn jetty
#

whatever you define as the name in the ESPhome YAML will be the text that is in that action.

esphome:
   name: "thistexthere"

results in this action inside of HA:

esphome.thistexthere_play_rtttl
#

if you have

esphome:
   name: "thistexthere"
   name_add_mac_suffix: true

it will result in this action inside of HA:

esphome.thistexthere_AABBCC_play_rtttl

where AABBCC are the last 3 bytes of the device's MAC address

#

if you have control over the ESPHome yaml, I'd probably just define a text sensor (with a unique and consistent name) that reports the ESPHome's device name. Then in HA, you can look up that uniquely-named entity from the device_id that is reported during the tag.

#

which you could simplify to:

{{ device_entities(trigger.event.data.device_id) | select('match','lock.') | first }}
#

I asked some of the other crows here, a suggestion from @rustic steeple was to just define a map in whatever automation you're using. You can go through your devices and figure out what their device id's are, and what their actions are.

variables:
  action_map:
    a1a4bb240f7ecbd60cbc3515164b0168: esphome.esphome_web_dee4ac_play_rtttl
    abc123abc123abc123abc123abc123ab: esphome.esphome_web_ababab_play_rtttl
    def456def456def456def456def456de: esphome.esphome_web_efefef_play_rtttl

then you can just use

action: "{{ action_map[trigger.event.data.device_id] }}"
data:
  song_str: test
brazen swift
#

I'd have to modify the action map for each device then which is not sustainable.

#

The way I hacked around it is by iterating over each entity of the ID and finding one with a fixed value IE _lock then splitting post esphome. and pre _lock to get a usable name within an action. It's and requires that the _lock entity always exists but it's okay for my use case as there isn't a world where _lock doesn't exist.

#

entityname.split(".")[1].split("_lock")[0] for example..

thorn jetty
#

That’s one way to do it.

#

Another way (not sure if it’s less hacky or not) is to get the last 3 bytes of the Mac and form the service call with that

#
{{ device_attr(trigger.event.data.device_id) , 'connections') | first | last).split(':')[3:] | join }}
brazen swift
#

oh, lol, thats way better IMHO 😄

#

Whats the probability of the last 3 bytes not being unique?

rustic steeple
#

Depends how many devices you have
Say you have 100 devices, the chance of at least one collision is 0.03%

rustic steeple
#

(assuming a random distribution - if your manufacturer only varies the last 2 bytes for example, then it will reduce the chances massively)

thorn jetty
#

the collision issue (if you even want to call it an issue) is caused by how you specified the device name in your ESPHome YAML config; it's not caused by the method you use to determine the the service call in HA.

In other words, based on what you've posted previously, this is what I expect you currently have in each of your node's configs:

esphome:
   name: esphome
   name_add_mac_suffix: true

And if you have a collision, you're going to have an issue when that second ("colliding") node attempts to come online. I think if the last 3 bytes are the same, and the device come is the same manufacturer/model then the rest of the bytes will also be the same. So your router is going to have some problems handling that and will assign them to the same IP, and things will devolve from there. So you'll never even get a chance to see whether or not you have a problem with using the correct service call in HA.

#

But based on hilburn's math, I wouldn't spend any more time worrying about it

brazen swift
thorn jetty
#

Ah sorry. There’s an extra closing parenthesis in there