Time calculation -1 hour

Hello everyone,

I have the following code

{{ as_timestamp(state_attr('sensor.agenda_bart','Vandaag_Events')[0].end )| timestamp_custom("%H:%M") }}    

resulting in “12:10”. I would like to subtract 1 hour from this. Can someone tell me how to do this?

Thanks in advance.

Is state_attr('sensor.agenda_bart','Vandaag_Events')[0].end a datetime object or a datetime string?

You can determine this using the following in the Template Editor:

{{ state_attr('sensor.agenda_bart','Vandaag_Events')[0].end is datetime }}

If it returns true, it’s a datetime object.

#For a datetime object
{{ (state_attr('sensor.agenda_bart','Vandaag_Events')[0].end - timedelta(hours=1)).strftime("%H:%M") }}


#For a datetime string
{{ (state_attr('sensor.agenda_bart','Vandaag_Events')[0].end|as_datetime|as_local - timedelta(hours=1)).strftime("%H:%M") }}

#The `as_local` may not be necessary if the string already includes tz data.
1 Like

Thank you for your response.

But I would like to have it an hour earlier.

Sorry, forgot to add that part, I’ve fixed it above.

Thank you very much