Incorrect Name in Home Assistant Notification Automation

Hi everyone,

I’m having trouble with an automation that’s supposed to send a notification when a specific device joins my home Wi-Fi network. The automation seems to be triggering correctly, but it’s consistently displaying the wrong name.

Here’s the automation code:

alias: TEST
description: Notifica su televisore e OnePlus 8 di chi si connette al WIFI
trigger:
  - platform: state
    entity_id:
      - device_tracker.redmi_XxxXxx
      - device_tracker.ZzzZzz
      - device_tracker.AaaAaa
      - device_tracker.SssSss
      - device_tracker.YyyYyy
    from: 'not_home'
    to: 'home'
condition: []
action:
  - service: notify.mobile_app_smartphone
    data:
      message: >
        {% set names = {
          'device_tracker.redmi_XxxXxx': 'XxxXxx',
          'device_tracker.ZzzZzz': 'ZzzZZ',
          'device_tracker.AaaAaa': 'AaaAaa',
          'device_tracker.SssSss': 'SssSss',
          'device_tracker.YyyYyy': 'YyyYyy'
        } %}
        {% set device = names.get(states.device_tracker|selectattr('state', 'equalto', 'home')|map(attribute='entity_id')|list|last, 'Somebody') %}
        {{ device }} is at home!
      title: Look who's here!
  - service: notify.lg_webos_tv_
    data:
      message: >
        {% set names = {
          'device_tracker.redmi_XxxXxx': 'XxxXxx',
          'device_tracker.ZzzZzz': 'ZzzZZ',
          'device_tracker.AaaAaa': 'AaaAaa',
          'device_tracker:SssSss': 'SssSss',
          'device_tracker.YyyYyy': 'YyyYyy'
        } %}
        {% set device = names.get(states.device_tracker|selectattr('state', 'equalto', 'home')|map(attribute='entity_id')|list|last, 'Somebody') %}
        {{ device }} is at home!
      title: Look who's here!
mode: single

The intended behavior is that the notification should display the correct name of the person who just arrived home. However, it’s consistently showing “YyyYyy is at home” regardless of who actually arrives.

I’ve tried troubleshooting by:

  • Checking device tracker states: The device tracker states are updating correctly.
  • Verifying the names dictionary: The dictionary is mapping the correct device entity IDs to names.

I’m unsure why the automation is selecting the wrong name. Any help or suggestions would be greatly appreciated.

Additional Information:

  • Home Assistant Version: 2024.11.0
  • Device Tracker Platform:
    RedMi11S;OnePlus6,RedMi12Pro,OnePlus8T,RedMi13Pro

Thank you in advance for your assistance!

It’s not… your template essentially says “Give me the last entity in the list of all device trackers that are home” you haven’t applied any sorting or selection criteria to make it return the most recent tracker to arrive home.

If you use the trigger variable instead using states.device_tracker... you can get the device tracker entity that actually caused the trigger… Also, there’s no need to calculate the name twice:

alias: TEST
description: Notifica su televisore e OnePlus 8 di chi si connette al WIFI
trigger:
  - platform: state
    entity_id:
      - device_tracker.redmi_XxxXxx
      - device_tracker.ZzzZzz
      - device_tracker.AaaAaa
      - device_tracker.SssSss
      - device_tracker.YyyYyy
    from: 'not_home'
    to: 'home'
condition: []
action:
  - variables:
      names:
        device_tracker.redmi_XxxXxx: XxxXxx
        device_tracker.ZzzZzz: ZzzZZ
        device_tracker.AaaAaa: AaaAaa
        device_tracker.SssSss: SssSss
        device_tracker.YyyYyy: YyyYyy
      device: "{{ names.get( trigger.entity_id, 'Somebody') }}"
  - service: notify.mobile_app_smartphone
    data:
      message: "{{ device }} is at home!"
      title: Look who's here!
  - service: notify.lg_webos_tv_
    data:
      message: "{{ device }} is at home!"
      title: Look who's here!
mode: single

And if you set friendly names for the device trackers, you can just use that without having to maintain a map. It’s available as an attribute.

1 Like

The initial code had syntax errors that I was not aware of. I also included two groups of devices, which belong to the respective people. So, I have reached this point:

alias:Test_1
description: "0.2"
triggers:
  - trigger: state
    entity_id:
      - device_tracker.device1
    to: home
    from: not_home
  - trigger: state
    entity_id:
      - device_tracker.device2
    from: not_home
    to: home
  - trigger: state
    entity_id:
      - group.devices_group1
    from: not_home
    to: home
  - trigger: state
    entity_id:
      - group.devices_group2
    from: not_home
    to: home
conditions: []
actions:
  - action: notify.mobile_app_device
    metadata: {}
    data:
      title: "Look who's here!"
      message: Hey, {{ trigger.to_state.attributes.friendly_name }} is at home
      data:
        color: 007FFF
  - action: notify.lg_webos_tv
    metadata: {}
    data:
      title: "Look who's here!"
      message: Hey, {{ trigger.to_state.attributes.friendly_name }} is at home
      data:
        color: 007FFF
mode: single

The code is certainly rudimentary and not very user-friendly, but I have achieved my goals; there is definitely room for improvement, and that is why I am here seeking advice and ideas to improve this type of automation. It’s absurd that I haven’t found anything “plug & play” online, given that this is a fairly common request.

For example, I thought about creating a script that automatically populates a group of devices that connects to the WiFi to keep track of all new connected devices; or merging the two devices and the two groups in this code and seeing if the automation continues to work. Who knows…

Thank you for any advice and ideas you can suggest.

Since your triggers are otherwise identical, you can list all the entity IDs under one trigger.

You can also make a notify group instead of duplicating that action.

You can also write an automation to set your device groups automatically. It sounded like you want to do that too.

HA’s power is precisely that it’s not packaged with only premade setups, but instead give you the control to do almost anything you’d like. Premade setups are fine — until you hit the exceptions.

What part of what you’ve done don’t you find user friendly? There’s a balance to be kept between ease of use and learning the abilities of a new platform. Many things become easier by learning and practicing.

alias: test_1
description: 0.3
trigger:
  - platform: state
    entity_id:
      - group.devices_xxxx
      - device_tracker.yyyy
      - group.devices_zzzz
      - group.devices_wwww
    from: not_home
    to: home
condition: []
action:
  - service: notify.mobile_app_xxxx
    data:
      title: "Look who's here!"
      message: "Hei, {{ trigger.to_state.attributes.friendly_name }} è a casa"
      data:
        color: 'FF0000'
  - service: notify.lg_webos_tv_xxxx
    data:
      title: "Look who's here!"
      message: "Hei, {{ trigger.to_state.attributes.friendly_name }} è a casa"
      data:
        color: 'FF0000'
mode: single

Here is the updated code; I followed your advice by using a single id_trigger. Unfortunately, I couldn’t implement the notification group because Home Assistant doesn’t accept services as an option for groups. I probably set up the code incorrectly, so I’m open to any suggestions:

group:
    name: all_devices
    services:
      - service: notify.mobile_app_xxxx
      - service: notify.lg_webos_tv_xxxx

actions:
  - service: notify.all_devices
    data:
      title: "Look who's here!"
      message: "Hey, {{ trigger.to_state.attributes.friendly_name }} is at home"
      data:
        color: FF0000

Regarding the user-friendly aspect, I was specifically referring to the repetitiveness of declaring entities, but now it is much better. It’s a shame that I couldn’t use a tenant_group! I tried until I realized that to send the notification, Home Assistant was waiting for all devices in the tenant_group to be in the HOME state.
Any suggestion about this?