I get my external IP fed from my ISP’s router (upnp). I would like to get notified when my external IP changes. Easy enough. I did it and it’s sort of working. Code below.
BUT… here’s the issue that I’m hoping someone smarter than me can figure out.
On a reboot, I get 2 back to back notifications: “IP went from xx.xx.xx.xx to unavailable” and then I get “IP went from unavailable to xx.xx.xx.xx”
I know it’s an artifact of the delay from the service connecting to the router, but can I avoid it in my notifications? Can I “filter” it out somehow?
If not… I’ll just use one of the other methods (rest or dnsip).
alias: System - New External IP
description: ""
triggers:
- entity_id:
- sensor.fios_external_ip
trigger: state
conditions:
- condition: template
value_template: >-
{% if 'trigger.from_state.state' and 'trigger.to_state.state' %} true {%
else %} false {%
endif %}
- condition: template
value_template: |2-
{% 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' }}"
actions:
- data:
message: >
The External IP changed from {{ trigger.from_state.state }} to {{
trigger.to_state.state }}
action: notify.galaxy_note
mode: single
alias: System - New External IP
description: ""
triggers:
- entity_id:
- sensor.fios_external_ip
trigger: state
not_from: unavailable
not_to: unavailable
That explains it. When you restart Home Assistant, the UPnP/IGD integration restarts too. Obviously, the sensor doesn’t exist at all while Home Assistant isn’t running, but there is a period before Home Assistant shuts down during which the integration has already been shut down, and then just after Home Assistant comes back up before the integration is enabled again. During those periods when Home Assistant is still alive but the integration is not, the sensor’s state is “Unavailable,” rather than an IP address.
Try something like this for the trigger, but note the caveat below:
The caveat is that the sensor might be “Unavailable” at times other than when Home Assistant is rebooting, for whatever reason–perhaps if your Internet link is down, or the router malfunctions, or any number of other conditions. That “Unavailable” state can therefore contain useful information.
Depending on whether you care or not, or what situations (if any) other than Home Assistant restart might cause the integration to report “Unavailable” through this sensor, you could create another automation that does something to alert you that the sensor has been Unavailable for some period of time longer than it typically takes to restart Home Assistant. For example:
Actually, one more note. If the sensor goes Unavailable due to something other than a Home Assistant restart, it’s possible that when it reports an IP address again it will report a new IP address that differs from the last one. If that can happen, your automation won’t capture the new IP address, because the suggested trigger won’t fire when changing from Unavailable to an IP address. (It can definitely theoretically happen if your IP address happens to change during a Home Assistant reboot.)
To really do something foolproof here, you would need to create an Input Text helper and store the current IP address there. When you get a state change trigger, you would check whether the new state is an IP and, if it is, compare that IP with the one in the helper. If it’s a new IP address, update the helper. If the new state is “Unavailable,” you can do nothing.