Detect if IP changes?

Care to share?

Ok, figured out the template and it returns the right values in the development panel.

{%- if is_state(“sensor.downstairs_last_ip”, ‘XX.XX.XX.XX’) -%} False {%- else -%} True {%- endif %}

returns False if the IP matches and True if it doesn’t. Might seem backwards but I am testing for a change in IP so True is a change in this case and false is no change.

So I put that into a template sensor. (I get the same result if I make it a template sensor or template Binary sensor)
- 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 %}’

I am getting the following in the log and HA won’t start.

16-11-30 20:48:09 homeassistant.util.yaml: while parsing a block mapping
in “/home/hass/.homeassistant/sensors.yaml”, line 226, column 8
expected , but found ‘’
in “/home/hass/.homeassistant/sensors.yaml”, line 227, column 71

226 is the entitity ID line 227 is the value template line.

just checking, the XX.XX.XX.XX should not be inside the quoted string right? the quote in front of that closes off the quote in front of the open bracket.

Both sensor.downstairs_last_ip and XX.XX.XX.XX need to be in quotes. I tried both ‘single’ and “double” quotes. Both work in the template developer and return the right result.

I’ve seen this before and I think you have to use a combo of single quotes on the template and double quotes on the string (or vice versa). I’m trying to find the post this happened in.

Im guessing this one?

I think I tried this but will check again tonight. I may have not tried the exact combination of “” on the outside and ‘’ inside.

1 Like

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?