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’”
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,
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: