Persistent_notification.httplogin does not triggering after invalid login attempts

Try with single quotes around off

value_template: "{{ trigger.to_state.state != 'off' }}"

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?

Now its formatted properly. There is no error related to this automation.

Can you please try without the url: part in the action section?

I have commented out the url line but still not working. :frowning:
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 }}"

Non of them was working and there is no error in the logs. :frowning:

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] }}

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?

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.

20200217_16:55:04_001

1 Like

It works! Thank you very much! :partying_face:

How could I miss the underscore :upside_down_face: I kept copying OPs incorrect code over and over again ouch (and I even use the same method in my own system)

1 Like

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.

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

Works perfectly!! I was looking for info in yaml, not about python. My fault.

Thanks!!

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.

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?

Maybe that helps?

It was not working for me either in HAOS version 2023.7.3, it might be due to newer version has different names eg: http-login instead of http_login in notification_id

Finally I made this code and it works for me:

alias: Failed Login Attempts Notification
description: Sends a notification when unauthorised login is attempted
trigger:
  - platform: persistent_notification
    update_type:
      - added
      - updated
    notification_id: http-login
condition: []
action:
  - service: notify.my_alert_group
    data:
      title: "{{ trigger.notification.title }}"
      message: >-
        {{ trigger.notification.message }}
      data:
        actions:
          - action: "URI"
            title: "Check IP"
            uri: "https://whatismyipaddress.com/ip/{{trigger.notification.message.split('(')[1].split(')')[0]}}"
  - service: persistent_notification.dismiss
    data:
      notification_id: http-login
  - service: persistent_notification.create
    data:
      notification_id: custom-http-login
      title: "{{ trigger.notification.title }}"
      message: >-
        {{ trigger.notification.message }} url:
        https://whatismyipaddress.com/ip/{{
        trigger.notification.message.split('(')[1].split(')')[0]}}

Please note my_alert_group is a custom notification group I have created, you can simply replace it with a device that you want notification on.

It also dismisses the original notification, and creates a new one to add a url to check more information on the ip from which login was attempted.

Hope this helps!