Detect if IP changes?

Yep - that was it! Forgot it was me asking the question as well!! LOL

OK, got this working. If anyone is interested here is the config.

Under sensors:

  - platform: template
    sensors:
      ipchange:
        entity_id: sensor.downstairs_last_ip
        value_template: "{%- if is_state('sensor.downstairs_last_ip', 'XX.XX.XX.XX') -%} False {%- else -%} True {%- endif %}"

Under automation:

  - alias: "NOTIFY IF IP CHANGES"
    trigger:
      platform: state
      entity_id: 'sensor.ipchange'
      from: 'False'
      to: 'True'
    action:
      service: notify.home_assistant
      data:
        title: "Home IP Address has Changed"
        message: Home IP Address was changed.  The new home IP address is "{{ states('sensor.downstairs_last_ip') }}"

Replace the xx.xx.xx.xx with your current IP and the sensor with your appropriate entity ID that has your IP address. Fabaff posted above a sensor to pull your IP without a nest.

2 Likes

Did last_ip go away with the 0.34.3 upgrade to HA??

Yes, use the restful sensor suggested by @fabaff above. I’m set up with it and it’s working fine on 34.3.

1 Like

Here is my solution for IP change notification using the DNS IP sensor:

sensor:
  - platform: dnsip
automation:
  trigger:
    platform: state
    entity_id: sensor.myipopendnscom
  action:
    service: notify.pushover
    data_template:
      message: 'IP changed: {{ trigger.from_state.state }} → {{ trigger.to_state.state }}'
3 Likes

How often does it check for a new public IP ?

set this up ages ago and never checked it worked for me…
and i have just rebooted my router remotely and it’s changed public ip :roll_eyes:

The DNS IP sensor has 120 seconds as its default scan_interval, which you can also override to check more or less frequently.

Many thanks, i worked out that my PFsense virtual had pooped it’s self on boot, but it’s good to know.

Would you mind to share the code that you have using the restfull component? How do you detect a change in the IP address obtained by restfull?

See that link for setting up the IP sensor.

There are a few examples above on how to test if the IP changed. If you need help with the template, post your entity ID for the restful IP sensor.

Easy one
https://github.com/skalavala/smarthome/blob/c3af59d5ca4d724e40549a93a6c9e3523dd27566/packages/homeassistant.yaml

# Notify me when I get a new public IP from my ISP
######################################################

  - alias: Notify Of New External IP
    initial_state: true
    hide_entity: true
    trigger:
      platform: state
      entity_id: sensor.external_ip
    condition:
      - condition: template
        value_template: "{% if trigger.from_state and trigger.to_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: script.notify_me
        data_template:
          message: "Your External IP changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"

I did get it

my old script is like that:

switch:
 - platform: broadlink
   type: sp3
   host: 192.168.1.13
   mac: '00:11:33:44:55:66'

and after change that should be like that ?

switchs:
  - platform: broadlink
    - platform: template
      sensors:
      ipchange:
       entity_id: sensor.downstairs_last_ip
       value_template: "{%- if is_state('sensor.downstairs_last_ip', '192.168.137.181') -%} False {%- else -%} True {%- endif %}"
  mac: '34:EA:34:xx:xx:xx'
  type:  sp3

no.

Switches go under switch. Sensors go under sensor. They cannot be combined.

Also you cannot reuse my entity is unless you also have a entity named sensor.downstairs.

1 Like

Lets start with the basics, do you have a sensor setup to detect your external IP?

Let me say like that:
I have a sensor that has dynamic IP if I reset router that will get new IP and I have to config it again.
certain configuration is:

switch:
 - platform: broadlink
   type: sp3
   host: 192.168.1.13
   mac: '00:11:33:44:55:66'

do you have any idea?

I am not aware of a method to detect internal IPs like this. This thread is about detecting your external IP.

Does your router not allow IP reservation?

I use a laptop as a router.
i find a sulotion for that. use dhcp server
that can help
thanks

I am trying to get this working but not succeeded yet. I have my ip address in sensor.myip. I should be possible to check if sensor.myip is the same as 62.163.118.83 (not my real IP). If it is then do nothing and if it is not the same then start the notify service.

A disadvantage is that If it is changed I have to change the automations yaml file.

Both this post

and this post

within this thread have a solution to do what you are looking for. The former uses a hardcoded IP address the latter just looks for a change in the IP address. In both examples you would need to update the entity IDs to your application.

What I did is the following and this seems to work (in automations.yaml):

alias: IPchange
  description: Notification if IP changed
  trigger:
  - entity_id: sensor.myip
    platform: state
  condition: []
  action:
  - data:
      message: IP changed! New IP is {{ states('sensor.myip') }}
    service: notify.joinha

That’s all. When I try this with a fast altering sensor (like cpu temperature pi) then I get a notification after each change. I assume this will be the same when my IP will change.

1 Like