m4rk
(Mark)
February 17, 2020, 12:52pm
1
Hello,
I would like to get iOS push notifications after invalid login attempts but my code does not triggering:
- alias: "Send notification upon failed login attempt"
trigger:
- platform: state
entity_id: persistent_notification.httplogin
condition:
- condition: template
value_template: "{{ trigger.to_state.state != 'off' }}"
action:
- service: notify.mobile_app_iphone
data_template:
title: "Failed Home Assistant login attempt!"
message: "{{ trigger.to_state.state }}"
data:
url: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ states.persistent_notification.httplogin.state.split ("from ") [1]}}
Could you help me please?
1 Like
Try with single quotes around off
value_template: "{{ trigger.to_state.state != 'off' }}"
m4rk
(Mark)
February 17, 2020, 1:16pm
3
Thank you for your reply!
Unfortunately still does not working with the single quotes.
Can you please format your code correctly, correct indentation etc.? Do you see any errors in the logs?
m4rk
(Mark)
February 17, 2020, 1:38pm
5
Now its formatted properly. There is no error related to this automation.
Can you please try without the url: part in the action section?
m4rk
(Mark)
February 17, 2020, 1:57pm
7
I have commented out the url line but still not working.
persistent_notification.httplogin is might not triggered or I don’t know.
Try this as a trigger:
trigger:
platform: template
value_template: "{{ states('persistent_notification.httplogin') != 'unknown' }}"
and remove the condition.
Otherwise you can also test this, to see whether it triggers at all:
- alias: "Send notification upon failed login attempt"
trigger:
- platform: state
entity_id: persistent_notification.httplogin
action:
- service: persistent_notification.create
data_template:
message: "{{ trigger.to_state.state }}"
m4rk
(Mark)
February 17, 2020, 2:29pm
9
Burningstone:
alias: “Send notification upon failed login attempt” trigger: - platform: state entity_id: persistent_notification.httplogin action: - service: persistent_notification.create data_template: message: “{{ trigger.to_state.state }}”
Non of them was working and there is no error in the logs.
VDRainer
(🍻)
February 17, 2020, 2:39pm
10
On my system, after a failed login, the entity for the persistent notification is:
persistent_notification.http_login
EDIT:
Also, to extract the IP from the message, this should work.
{{ state_attr('persistent_notification.http_login', 'message').split('from')[1] }}
m4rk
(Mark)
February 17, 2020, 3:25pm
11
The notification works but the IP in the message is not:
- alias: "Send notification upon failed login attempt"
trigger:
- platform: state
entity_id: persistent_notification.http_login
condition:
- condition: template
value_template: "{{ trigger.to_state.state != off }}"
action:
- service: notify.mobile_app_iphone
data_template:
title: "Failed Home Assistant login attempt!"
message: "{{ trigger.to_state.state }}"
data:
url: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ state_attr('persistent_notification.http_login', 'message').split('from')[1] }}
Could you help me please?
VDRainer
(🍻)
February 17, 2020, 4:12pm
12
No clue about the ios notify data and url options, but in a persistent notification this works:
title: "Failed Home Assistant login attempt!"
message: http://www.ip-tracker.org/locator/ip-lookup.php?ip={{ state_attr('persistent_notification.http_login', 'message').split('from')[1].replace(' ', '') }}
You need to remove the leading space in the extracted IP.
1 Like
m4rk
(Mark)
February 17, 2020, 4:19pm
13
It works! Thank you very much!
How could I miss the underscore I kept copying OPs incorrect code over and over again ouch (and I even use the same method in my own system)
1 Like
ManuelV
(Manuel Vasco)
January 5, 2022, 5:33pm
15
On latests HA core the persistent notifications shows “from xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx). See the log for details.” and the message shows this, show ip-tracker doesn’t work. Could you tell me how to take only the ip? I’m using the exact code you gave on your last message.
VDRainer
(🍻)
January 5, 2022, 10:48pm
16
ManuelV:
how to take only the ip?
try
{{ state_attr('persistent_notification.http_login', 'message').split('(')[1].split(')')[0] }}
https://python-reference.readthedocs.io/en/latest/docs/str/split.html
1 Like
ManuelV
(Manuel Vasco)
January 6, 2022, 11:56am
17
Works perfectly!! I was looking for info in yaml, not about python. My fault.
Thanks!!
fsamper
(Fran)
November 3, 2022, 1:31pm
18
Hi everyone,
I’m trying to use this automation but I have no luck. Here is my code:
alias: Bad login
description: ""
trigger:
- platform: state
entity_id:
- persistent_notification.http_login
condition:
- condition: template
value_template: "{{ trigger.to_state.state != off }}"
action:
- service: notify.mobile_app_myios
data:
message: Bad login
I don’t know what I’m doing wrong. If I run this automation it works but if I do a bad login it doesn’t.
Scoff
(Scott)
May 4, 2023, 10:10am
19
My automation for failed login attempts seems to have started generating errors in my logs, even though the automation is ‘working’ and sending me a push notification to my mobile device. I’ve been trying to debug my automation, and it seems the error messages only generate when I clear/dismiss the persistent notification from within HA. So the entity ‘persistent_notification.http_login’ no longer exists after clearing the notification, yet the automation must be getting triggered at this point, but there is nothing to reference.
here is 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 != '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]
}}
and the error messages:
Error while executing automation automation.send_notification_upon_failed_login_attempt: Error rendering data template: UndefinedError: 'None' has no attribute 'attributes'
10:58:50 – (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'
10:58:50 – (ERROR) Automation
Template variable error: 'None' has no attribute 'attributes' when rendering '{{ states.persistent_notification.http_login.attributes.message.split('from')[0] }}'
10:58:50 – (ERROR) helpers/template.py
so I ‘think’ I need to somehow prevent the automation from firing upon clearing the notification within HA, via another condition perhaps, but I’m not sure how to achieve that?