Get Time Difference from 2 input date time helpers


Im sure Im missing something really stupid from documentation, I looked around also and I cant find a very simple way to subtract 2 input datetime helpers from each other to get how much time has passed between the 2.

The 2 datetime helpers are input_datetime.charging_started and input_datetime.charging_stopped… I`m just want to subtract and display in notification the difference between the 2 to get the duration.

The most basic way to get the difference is using a template as follows:

{{states('input_datetime.charging_stopped')|as_datetime - states('input_datetime.charging_started')|as_datetime}}

However, most core Dashboard cards do not allow templating. If the card you want to use doesn’t allow templating, you will need to create a Template sensor and use that in your card.

I just need to send it in Email/Notification.
And thank you so much, I tried so many combinations around what you wrote, but yours was spot on!

This is super helpful - what would the syntax be if the helpers were just Time and not Date/Time. the ‘as_datetime’ did not work with just a time helper

If you know that they are always going to be the same calendar day, you can use the function today_at() to change the time string returned by a Time-only datetime helper into something more easily math-able:

{{ today_at(states('input_datetime.charging_stopped')) - today_at(states('input_datetime.charging_started'))}}

If they aren’t necessarily going to be the same calendar day, it gets more complicated and you will need to provide more details about you use case so we can figure it out.

Thanks @Didgeridrew. The automation I am trying to create is for the daily charging of my solar batteries during the cheap rate period provided by my utility provider - so the day/date isn’t needed as the hours stay the same unless I swap tariff.

What you have provided here is perfect :pray: