Time subtraction adds one day

Hi folks,

I’ve been struggling with time subtraction for few hours now.

Could you please advise what am I doing wrong here?
Result of subtraction adds one day when I format the time difference

{{now()}}
{{state_attr('automation.system_light_is_on', 'last_triggered')}}
{{(now().timestamp() - as_timestamp(state_attr('automation.system_light_is_on', 'last_triggered'), default=0)) | int }} seconds
{{((now().timestamp() 
- 
as_timestamp(state_attr('automation.system_light_is_on', 'last_triggered'), default=0)) 
| int ) | timestamp_custom("%d :%M:%S") }}

Result:

2024-06-14 18:35:00.156066+03:00
2024-06-14 15:11:35.512921+00:00
1404 seconds
01 :23:24

It’s not adding a day, it’s telling you it’s still the first of January 1970.

Short answer is that you need to either subtract one day from this or just use the seconds and calculate the days, minutes and seconds.

The long answer is that 0 timestamp is 1970-01-01.
So you calculate based on this and you ask what date is it.
Well it will respond with 1.
Keep in mind this will be basing of the calendar. So if your automation hasn’t triggered for a month then it will cycle back to 1.
Subtracting 1 method is not a good solution because you can’t really do it in a good way, you need to do the calculation twice.
Use the seconds you have in your third line and calculate from there.
It will probably not work correctly past DST though.

+1 to what @Hellis81 said… especially the part where this approach might not quite be optimal. if you can say more about what specifically you need, what granularity, etc… maybe we can help. one thing you might look at (but this is very coarse grained) is just this:

{{ relative_time( state_attr('automation.system_light_is_on', 'last_triggered') ) }}

but that’s coarse grained and not highly customizable…

Hey @armedad
Purpose is very simple I would say.
We have electricity blackouts and I have an automation which is triggered by electricity sensor.
So, I have 2 automations:
Electricity is ON
Electricity is OFF

When electricity is ON, I send notification with message for how long electricity was absent, when it’s OFF i send notification with info how long electricity was present.

So usage is very basic, I’d say, I need to count how much time has gone since automation.last_triggered and get it in human readable form in Ukrainian language (context)

So I need to make smth like

now() - state_attr(‘automation.light_is_off’, ‘last_triggered’) and

1day 2hours 15minutes (but wording is in ukrainian language, so i’d like to have it customizable)

Hope the idea is clear

i see. i’m personally not aware of a good helper function that formats time delta in a flexible and localized way like that. relative_time unfortunately rounds it to the largest unit. and strftime and timestamp_custom are intended to specify a particular date, not a time delta. someone else more knowledgable may know of a better way, but short of that, you may need to do the calc yourself:

          {% set delta = now()  - as_datetime(state_attr('automation.system_light_is_on', 'last_triggered')) %}
          
          {% set days = delta.days %}
          {% set hours = delta.seconds // 3600 %}
          {% set minutes = (delta.seconds % 3600) // 60 %}

{{days}} ukrainina_days {{ hours }} ukranian_hours {{minutes}} ukranian_minutes

w/ a bit more code, you can handle plurals better and the 0 case better ,but you get the idea…

hopefully someone else has a better answer than this brute force way…

Includes Ukrainian language option.

1 Like

Localized time helpers:

1 Like

that’s awesome! thanks guys!

Hi folks!
@Didgeridrew @Edwin_D thanks guys!

However I saw @armedad reply first and used it :slight_smile: Works like a charm. There was an issue with formatting since if e.g hours < 10 it says 1 instead of 01 , but that’s solved by if/else workaround with addition of 0 :smiley:

1 Like

glad you got what you wanted. at some point if you get the gumption, I think thefes’s macros that @Didgeridrew linked to for this case is quite nice