Send notification when IP (global) changed

If you can integrate your router into ha you can create a template sensor for the ip
As soon this sensor changes you can trigger a notification

For example


The sensor has an Attribut external_ip and this you can check if it changes

Looks like ’Fritz!Box’ is well supported… I use Linksys MR9600, pretty new device on the market.
Not so much support…

or use a web crawler on http://whatismyip.com ?

1 Like

You will want to get your external IP into a sensor that can be monitored.

This works well:

Then create an automation to send you a notification when that sensors state changes. You will need to change your sensor name and the notification service to match your actual setup. Also the condition is optional and may need to be tweaked based on what the sensor can put out. If the sensor cannot pull information or has some other error you don’t want to get a notification. If you get a notification in error, put the state from that notification in the ‘None’ spot of the condition to prevent it happening again.

  - alias: "Send notification upon IP change"
    initial_state: true
    trigger:
      - platform: state
        entity_id: sensor.external_ip
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state != 'None' }}"
    action:
      - service: notify.home_assistant
        data_template:
          title: 'Your External IP address has changed'
          message: 'Your new IP address is {{ states.sensor.external_ip.state }}'

So simple… I’m getting older. It is even example when the REST platform is explained…

ip

I created automation and when tested it works. Must be checked yet when really changed…

alias: Send notification when external IP changed
description: Automation to send notification when external IP changed
trigger:
  - platform: state
    entity_id: sensor.external_ip
condition:
  - condition: template
    value_template: '"{{ trigger.to_state.state != ''None'' }}"'
action:
  - service: notify.mobile_app_dpiphone8
    data:
      title: Your External IP address has changed
      message: 'Your new IP address is {{ states.sensor.external_ip.state }}'
mode: single

@ silvrr - thank you for help…!

I also wanted to set-up this option (notification via gmail).
But the service ‘notify.gmail’ is not appearing in Home Assistant.
What is wrong?
In ‘google’ I have ‘2-step Verification’, but then it is possible to use option ‘App password’. So I have generated this 16-digit password and entered it in ‘configuration.yaml’.
But as I said - this is not working, no such service…
Is that any ‘bug’ in Home Assistant? Or some additional settings are needed…???
Anyone tried to configure ‘gmail’ for notifications?

# SMTP notification
notify:
  - name: gmail
    platform: smtp
    server: smtp.gmail.com
    port: 587
    timeout: 30
    sender: [email protected]
    encryption: starttls
    username: [email protected]
    password: atkbnarycvqegdck
    recipient:
      - [email protected]
#     - [email protected]
    sender_name: Home Assistant

Did you restart? Anything in the logs?

Also, I have my password pasted in with the spaces (e.g. atkb nary cvqe gdck)

Still nothing… With spaces you say. I’ll check morning… Maybe this is problem…

O.K. No space in password - this was the problem. After correction it is O.K.
Should be:

password: atkb nary cvqe gdck

NOT

password: atkbnarycvqegdck

Thank you for the hint!

The ‘gmail’ notify is showing and working.

I also wanted to try -> THIS INSTRUCTION <-, but this is not working for me.
What I’m doing wrong?

alias: Send iPhone notification when external IP changed
description: Automation to send notification to iPhone when external IP changed
trigger:
  - platform: state
    entity_id: sensor.external_ip
condition:
  - condition: template
    value_template: >-
      {% if trigger.from_state.state and trigger.to_state.state %} true {% else %} false {%
      endif %}
  - condition: template
    value_template: >-
      {% if trigger.from_state.state == 'unknown' %} false {% else %} true {%
      endif %}
  - condition: template
    value_template: >-
      {% if trigger.to_state.state == 'unknown' %} false {% else %} true {%
      endif %}
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - service: notify.mobile_app_dpiphone8
    data:
      title: Your External IP address has changed
      message: >-
        Your External IP changed from {{ 'trigger.from_state.state' }} to {{
        'trigger.to_state.state' }}
mode: single

The result of the message is strange:

ip

I’m no YAML expert, but wouldn’t you leave out the single quotes (') in the message: line?

No, it does the same…

As per CaptTom, remove the quotes; this works for me:

Your External IP changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}

O.K. Corrected that, but still nothing. Getting following ‘log’ errors…
Why it says that 'trigger is ‘undefined’ ???

Logger: homeassistant.components.automation.send_notification_when_external_ip_changed
Source: helpers/service.py:146
Integration: Automation (documentation, issues)
First occurred: 25 November 2020, 8:54:40 (230 occurrences)
Last logged: 7:29:33

Send iPhone notification when external IP changed: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
While executing automation automation.send_notification_when_external_ip_changed
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 351, in async_render
    render_result = compiled.render(kwargs)
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'trigger' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 144, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 91, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 92, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 96, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 253, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 432, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 146, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'trigger' is undefined

After further research looks like this is O.K.
This post helped me…

Even though the issue has been solved already, I want to point at the dnsip integration which is made specifically to get the current IP address efficiently (using lightweight DNS requests), regardless of which router is being used (similar to the REST solutions suggested earlier)

Strange… My IP changed in the night, but I have not received any ‘notification’…
Taking about ‘dnsip’ I have following settings - see below.
Still something missed?
When executed manually - the automation works…

#Check external IP (via opendns.com)
  - platform: dnsip
    name: dnsip
    scan_interval: 900

And automation:

alias: Send iPhone notification when external IP changed (dnsip)
description: ''
trigger:
  - platform: state
    entity_id: sensor.dnsip
condition:
  - condition: template
    value_template: '{{ trigger.to_state.state != 'None' }}'
action:
  - service: notify.mobile_app_dpiphone8
    data:
      title: Linksys MR9600 IP address has changed
      message: 'New external IP address is: {{ states(''sensor.dnsip'') }}'
mode: single

Some correction… The version with ‘dnsip’ is working. And I receive notification…
The previous version - still something wrong. No notifications send.

This one is no good:

alias: Send iPhone notification when external IP changed
description: Automation to send notification to iPhone when external IP changed
trigger:
  - platform: state
    entity_id: sensor.external_ip
condition:
  - condition: template
    value_template: >-
      {% if trigger.from_state.state and trigger.to_state.state %} true {% else %} false {%
      endif %}
  - condition: template
    value_template: >-
      {% if trigger.from_state.state == 'unknown' %} false {% else %} true {%
      endif %}
  - condition: template
    value_template: >-
      {% if trigger.to_state.state == 'unknown' %} false {% else %} true {%
      endif %}
  - condition: template
    value_template: '{{ trigger.from_state.state != trigger.to_state.state }}'
action:
  - service: notify.mobile_app_dpiphone8
    data:
      title: Your External IP address has changed
      message: >-
        Your External IP changed from {{ 'trigger.from_state.state' }} to {{
        'trigger.to_state.state' }}
mode: single

After few days of testing - looks like this is O.K.
I would like also have displayed the time/date when last change occured.
Any suggestions the simplest way to do that?

The sensor.external_ip and sensor.dnsip wasn’t working for me. I had to change it to sensor.myip.

trigger:
    - platform: state
      entity_id: sensor.myip
  condition:
    - condition: template
      value_template: '{{ trigger.to_state.state != ''None'' }}'
  action:
    - service: notify.skyler_sms
      data:
        title: Home Assistant - IP changed
        message: "Public IP is now: {{ states.sensor.myip.state }}"