Time between events

Hello all!

I’m creating an automation that notifies me when the power has gone out, then notifies me when it’s back on. It’s done using a z-wave sensor with a binary on/off that’s wired into a relay, into a stepdown converter, and into mains power.

When a power loss is detected (Sensor says OFF) I get a notification of this, including the time.
When power is recovered (Sensor says ON) I get a notification of this, including the time.

I’d like to also have it tell me how long the power outage actually lasted. I’m doing this by reading back the time the first automation (power loss) is ran.

This returns
"Power Outage Lasted 2023-07-16 20:21:06.531596+00:00"

Power Outage Lasted {{states.automation.power_loss_detection_off.last_updated}}
This works but only gives the larger amount of time
"Power Outage Lasted 2 hours".

Power Outage Lasted {{relative_time(states.automation.power_loss_detection_off.last_updated) }}
This gives me a bit closer to what I'm wanting, but is leaving data on the end
"Power Outage Lasted 1:43:02.11303"

Power Outage Lasted {{now()-(states.automation.power_loss_detection_off.last_updated) }}

I’m looking to be able to say something along the lines of “Power Outage Lasted 1 hour 2 minutes”, and I feel like I’m close to it, but just can’t do it.

Any help is much appreciated

1 Like