How to subtract two timestamps

Dear all

My Coffee Machine takes roughly 20 minutes to heat up. As I want to have a warm machine after waking up, I need kind of a trigger offset.
But how can I subtract 20 Minutes from sensor.mobile_app_next_alarm?
I was under the impression offset would do the Trick, but failed. Only with sun.
Do you have any Ideas?

If this sensor.mobile_app_next_alarm is a datetime object it is now quite easy:

{{ states('sensor.mobile_app_next_alarm') - timedelta(minutes = 20) }}
1 Like

It’s a timestamp as far as I understand. The ordinary “next Alarm” Sensor from the App.
Yours returns TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

Can you show it’s actual state?

sure


Or in code

2022-01-20T07:00:00+00:00 	

Local Time: Thu Jan 20 08:00:00 GMT+01:00 2022

Package: com.oneplus.deskclock
Time in Milliseconds: 1642662000000
device_class: timestamp
icon: mdi:alarm
friendly_name: Jarvis App Next Alarm

Try this:

{{ states('sensor.mobile_app_next_alarm')|as_datetime - timedelta(minutes = 20) }}
1 Like

Almost. I used +40 Minutes to compensate the local time. But now I am uncertain about daylight saving. Is there some kind of local time cast?

There’s this:

{{ states('sensor.mobile_app_next_alarm')|as_datetime|as_local - timedelta(minutes = 20) }}
1 Like

Ah, I now understand that code even more, thanks :slight_smile:

Well, almost.
sensor.mobile_app_next_alarm returns 2022-01-20T07:00:00+00:00, where the other returns 2022-01-20 07:40:00+01:00 (without the ‘T’)
Is there a different formatting possible?
Would this be the right source for additional reads: datetime — Basic date and time types — Python 3.10.2 documentation ?