How to get the IP address from a failed login notification

I’m using this automation to get the failed login attempt with the IP but it’s not firing as it’s saying “Error while executing automation automation.failed_login_attempt: Error rendering data template: UndefinedError: ‘None’ has no attribute ‘attributes’”

Here is my code:

- alias: "Failed Login Attempt"
  initial_state: true

  trigger:
    - platform: state
      entity_id: persistent_notification.http_login

  condition:
    - condition: template
      value_template: "{{ trigger.to_state.state is not none }}"

  action:
    - service: notify.pushover
      data:
        title: "{{ states.persistent_notification.http_login.attributes.message }}"
        message: 'url: https://whatismyipaddress.com/ip/{{ states.persistent_notification.http_login.attributes.message.split ("from ") [1]}}'

I’ve been using this code for notifications and just ignoring the errors that are generated afterwards, because the automation ‘works’ and sends a push notification with the correct information about the IP address of the device that attempted the login.

I’m curious though, as like @pergola.fabio upon clearing the notification, I immediately get errors generated in my logs.

My automation code:

alias: Send notification upon failed login attempt
description: ""
trigger:
  - platform: state
    entity_id: persistent_notification.http_login
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state is not none }}"
action:
  - service: notify.mobile_app_sm_g996b
    data_template:
      title: >-
        {{
        states.persistent_notification.http_login.attributes.message.split('from')[0]
        }}
      message: >-
        Attempt from {{
        states.persistent_notification.http_login.attributes.message.split('(')[1].split(')')[0]
        }}
      data:
        actions:
          - action: URI
            title: Lookup IP
            uri: >-
              http://www.ip-tracker.org/locator/ip-lookup.php?ip={{
              states.persistent_notification.http_login.attributes.message.split('(')[1].split(')')[0]
              }}

The automation triggers upon an invalid login attempt, I receive the push notification on my mobile device, and can see the notification in Home Assistant. When I clear the notification, immediately I get these errors:


Error while executing automation automation.send_notification_upon_failed_login_attempt: Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'
12:11:37 – (ERROR) Automation
Send notification upon failed login attempt: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'
12:11:37 – (ERROR) Automation
Template variable error: 'None' has no attribute 'attributes' when rendering '{{ states.persistent_notification.http_login.attributes.message.split('from')[0] }}'
12:11:37 – (ERROR) helpers/template.py

How can I prevent these errors upon clearing the notification? I can see the state that the entity named ‘persistent_notification.http_login’ has when an invalid login is triggered, which is a state of ‘notifying’, however upon clearing the notification, the entity disappears,

If the entity disappears then there is no state for the automation to check against - is this maybe the reason for the errors?

In the meanwhile, the automation script can be much more simplified.

First: The notification_id is now: http-login (with a dash). So, no extra condition is needed. Next: The trigger.* contains now all the information. Last: the extract of the IP address is a little bit more complicated.

Here is the solution that should work with HA 2024’er versions:

alias: Login Alarm
description: Send notification upon failed login attempt
trigger:
  - platform: persistent_notification
    notification_id: http-login
    update_type:
      - added
      - updated
action:
  - service: notify.home_assistant
    data_template:
      title: "{{ trigger.notification.title }}"
      message: >-
        {{ trigger.notification.message }} - Lookup
        https://whatismyipaddress.com/ip/{{
        trigger.notification.message.split('(')[1].split(')')[0] }}
  - service: persistent_notification.dismiss
    data:
      notification_id: http-login

I use as service the Home Assistant notify integration to send it to my android.

2 Likes