Time subtraction

Hi. I have difficulty with time stamps. If I use

now().strftime(“%H:%M”)

I get i.e. 07:30 as output.

states.input_datetime.time_helper.state
I get the time stored in the helper in the same format i.e. 07:35

When I try to subtract them

{{ now().strftime(“%H:%M”) - states.input_datetime.time_helper.state }} I get an error and no output.

I tried to use as_timestamp for both values but also get the errors.

Hope someone can find out the solution

I think I had the same type of problem (state is a string) , I solved it by using timestamps
My code :

{{now()|as_timestamp|timestamp_custom('%H:%M:00') == (state_attr('input_datetime.reveil','timestamp')-(15*60)-3600)| timestamp_custom('%H:%M:00')}}

You are trying to subtract two strings, which obviously will not work. Your timestrings cannot be converted to timestamps, only datestrings can be converted directly to timestamps. For your usecase it is probably easiest to use the today_at filter. Something like this:

{{ now() - states('input_datetime.time_helper') | today_at }}

Edit: Or perhaps this is even easier, using timestamps with result in seconds:

{{ now().timestamp() - state_attr('input_datetime.time_helper', 'timestamp') }}
1 Like

There is also a jinja macro lib for working with time.

Just search for it in HACS.