I have an automation that notifies me via email whenever HA reconnects to the internet, after a disconnection :
alias: HA Healthcheck
description: ""
triggers:
- trigger: state
entity_id:
- binary_sensor.8_8_8_8
id: Internet Reconnection
from: "off"
to: "on"
conditions: []
actions:
- if:
- condition: trigger
id:
- Internet Reconnection
then:
- action: notify.gmail
metadata: {}
data:
message: >-
HA System is now reconnected to the internet.
title: ⚠️🚨 HA Reconnected ⚠️🚨
So basically, when the binary sensor, 8.8.8.8, changes to “on”, internet connectivity has been re-established. What I’d also like to know is when the disconnection happened, ie. when was 8.8.8.8 last in the “off” state - having this timestamp in the email alert will be super helpful.
from_state is a state object in itself and so one may directly reference the last_changed attribute that way instead of having attributes.last_changed?
Quick follow up question - how does one compute the following duration : trigger.to_state.last_changed - trigger.from_state.last_changed? This should give me the duration for which HA was disconnected before reconnecting again, which I think helps to include in the notification email.