Notification when unknown clients (dis)connect to the guest WLAN

Hello all,

I would like to receive a Telegram notification when
:white_check_mark: a new (unknown) device connects to my Unifi guest wifi,
:x: a client connected to the guest wifi disconnects again.

This is my automation:


- id: '1609592805978'
  alias: '1: Guest Test'
  description: ''
  trigger:
  - platform: event
    event_type: state_changed
  condition:
  - condition: template
    value_template: "???"
  action:
  - service: notify.lambdacore_bot
    data_template:
      message: '{{ states.device_tracker | selectattr(''attributes.is_guest'', ''true'')
        | list | map(attribute=''name'') | join(''\n'') }}'

The action provides me a complete list of all entities that have the attribute “is_guest: true”. But I simply want it entity by entity not all.

However, I am now stuck with the condition and notification. My specific questions about this are:

How do I get only the
:one: last, newly connected (unknown) device (is_guest: true),
:two: the device that logged off last (was “is_guest: true” is now “is_guest: false”) with name and
:three: not the complete list?

All this templating is giving me quite a headache :exploding_head:. Maybe someone might pass me some aspirin? :pill:

Thank you very much for your help. If any information is missing, please shout out, I’ll be happy to share. :blush:

I managed to get this thing working. This is the automation - for whom it may concern.


- id: '1609592805978'
  alias: '1: Guest Notification'
  description: ''
  trigger:
  - platform: event
    event_type: state_changed
  condition:
  - condition: template
    value_template: >
      {{ 'old_state' in trigger.event.data and 'new_state' in trigger.event.data and
         trigger.event.data.new_state.attributes.is_guest != trigger.event.data.old_state.attributes.is_guest and
         trigger.event.data.new_state.attributes.essid == 'Ravenholm' or
         trigger.event.data.old_state.attributes.essid == 'Ravenholm' }}
  action:
  - service: notify.lambdacore_bot
    data_template:
      message: >
        {% if trigger.event.data.new_state.attributes.essid == 'Ravenholm' %}
          ✅ {{ state_attr(trigger.event.data.entity_id, 'name') }} connected to Ravenholm.
        {% elif trigger.event.data.old_state.attributes.essid == 'Ravenholm' and trigger.event.data.new_state.attributes.essid != 'Ravenholm' %}
          🚫 {{ state_attr(trigger.event.data.entity_id, 'name')}} disconnected from Ravenholm.
        {% endif %}
  mode: single

Only one issue left, because there are constantly errors in the log:

Error during template condition: UndefinedError: 'None' has no attribute 'attributes'

I tried to put

{ if … is defined }

around the condition but still no success. Any ideas out there?

2 Likes

hallo - I am learning and new to the HA and Uni setup, I did like to use this “notify when guest” - how do I use this code that you shared? Kind reg NewBee