Reload config after internet outage

This is beyond my skill set so I wonder if anyone can help. My issue is that I rely on mobile broadband, this drops out 2 or 3 times a day, only for a few minutes usually. HA then starts working again but it seems that it starts up faster than my SmartThings setup. So the integration fails to load, I have to recognise this and manually reload it.

So I setup an automation that twice a day which works but of course if an outage occurrs 5 minutes after this then ST is still unavailable.

So is it possible to write an automation that:

Recognises the internet is available again
Waits 5 minutes
The reloads the ST integration.

Many thanks

Hi,

You can use a simple ping to a public DNS adress as a trigger for your automation.

Hello Dave,

One way [SOLVED] Home Assistant start event and stop event

well I get the ping idea so I guess I could set up an automation to carry out the ping every 15 minutes but that would reload the integration every 15 minutes. Is that a good thing to do?

There is a lot to this thread, I will read, digest and hopefully understand! Thanks

I think you want a two-part solution.

  1. A binary sensor, based on ping, that tells you whether the internet is up. I will follow up with example code in a bit.
  2. An automation that triggers when the sensor transitions from “internet down” to “internet up” that restarts the problematic integration.

EDIT: On point 1, I forgot that you don’t actually need any code to get the sensor up. You just need the Ping integration. It will create the binary sensor for you. I use 8.8.8.8, which is a Google DNS server.

On point 2, here is a simple automation I have that calls a notification script I wrote when the Internet has been restored after being down. You can repurpose this to make a call to restart your integration.

automation internet_restored:
  - triggers:  
      - trigger:    state
        entity_id:  binary_sensor.home_inet_google
        from:       off
        to:         on
    actions:
      - action:    script.notify_me
        data:
          title:    "Internet restored"
          message:  "[{{now()}}]"        

Great thank you. I will give a go.

Thanks for this but clearly I am a bit slow on this one. I added the code snippet you provided as an automation but HA doesn’t like it. See attached.

Thanks

It wasn’t meant to just be copied and pasted. First, make sure you followed step 1 and added the integration. It’s fine if you want to use the name home_inet_google, but you have to make sure that you rename the sensor that the integration creates, or otherwise put the actual name into the automation.

Second, you almost certainly don’t have a script called notify_me in your system, so don’t put that into your automation. I assume you were just trying to get a starting point where the automation is created and then you can tinker with it.

Once you’ve got the “inet” sensor, my recommendation would be to use the visual editor to create an automation that is triggered by “state”, find the “inet” sensor, specify the from and to states, and then pick the action you want it to do. You can peek at the YAML and compare it to the example to get your bearings. This way you get more footing for creating another automation in the future.

The problem you hit off the bat with the example code, which caused the error message, is that that code wasn’t meant for the UI automation editor. I think in the UI, it ends up looking something like this:

description: "Call script.notify_me when the Internet sensor detects restoration of Internet connectivity"
triggers:  
      - trigger:    state
        entity_id:  binary_sensor.home_inet_google
        from:       off
        to:         on
actions:
      - action:    script.notify_me
        data:
          title:    "Internet restored"
          message:  "[{{now()}}]" 

Thanks, sorry to be a bit of a muppet! I am new to the HA environment. But learning fast!

So thanks again for this, I tried to write a script to notify me with help from AI but it says message malformed. Can you correct this script for me? I appreciate your help!

Example script in configuration.yaml

script:
send_notification:
alias: “Send Notification”
description: “Send a notification when triggered”
mode: single
sequence:
- service: notify.mobile_app_your_phone
data:
message: “Notify Me.”
title: “Home Assistant Alert”
data: “Alert”
channel: “alerts”
importance: “high”
sticky: true

Please repost the code using the preformatted text option in the editor (</> button).

# Example script in configuration.yaml
script:
  send_notification:
    alias: "Send Notification"
    description: "Send a notification when triggered"
    mode: single
    sequence:
      - service: notify.mobile_app_your_phone
        data:
          message: "Internet Restored."
          title: "Home Assistant Alert"
          data:
            channel: "alerts"
            importance: "high"
            sticky: true

A few pointers:

(1) Don’t use AI. It has a spectacularly bad success rate with Home Assistant for a large number of reasons I won’t get into here.

(2) Surely the error message has more information than just “message malformed”? Like what comes after the colon?

(3) Did you just try to paste that code lock-stock-and-barrel into the UI YAML editor? That won’t work, since that editor doesn’t take the script: header, or the identifier that you have on the following line. That’s why when you create a blank script, it starts with something like

sequence: []

It’s a lot easier to step through the UI’s New Script tool and build the script that way. All you need to do is find the notify action associated with your phone and then put in the title etc. (And I’m pretty sure the notify action entity is not called notify.mobile_app_your_phone… unless you named your phone “Your Phone”).

Thanks you again, you have been more than helpful. I will follow your guidance and try again!

So near (thanks to your help), I have created a ping called home_inet_google

I have created a script called internet_re_connect that works

I have created an automation that calls the notification, it works

All that is left is to work out how to get the ping to run every (say) 5 minutes?

It’ll run every 30s by default. If you want to change that, it’s explained in the Ping integration

1 Like

Thanks, I live and learn!

2 Likes

Hi again,

What does this mean, it occurs when the script below is run?

To put this in context. I am trying to get HA to send me a notification when the Internet is restored following an outage. The people above have been incredibly helpful and I am so close now.

I have a Ping setup, I know this works as the Ping log shows the disconnects and reconnects correctly. The Ping entity is called home_inet_google I get its entity ID from the URL.

I then have an automation called Internet_Now_back. The automation is as follows:

description: >-
  Call script.notify_me when the Internet sensor detects restoration of Internet
  connectivity
triggers:
  - trigger: state
    entity_id: binary_sensor.home_inet_google
    from: "off"
    to: "on"
actions:
  - action: script.internet_restored
    data: {}

I then have a script called internet_restored, as follows:

sequence:
  - action: notify.notify
    metadata: {}
    data:
      message: Now Reconnected
      title: internet_restored
  - device_id: a8b5d7fff1cd800611125be27f76eb7b
    domain: mobile_app
    type: notify
    message: internet_restored
    title: internet_restored

This does indeed send a notification but also gives the error shown above.

The end result is that the sequence does not work. I assume because of this error message.

Your further help would be great, thanks.

Edit: I just noticed the error above says script/internet_re_connect! But after resaving all 3 things the error now says scrit/internet_restored. But the problem remains.

This is resolved now, thanks to all who assisted me.

1 Like