Run automation X minutes before next alarm

Hello,

I’m trying to get an automation to run 20 minutes before my next alarm.

Reading from here, I got to:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.tiago_alarm
data:
  timestamp: "{{ as_timestamp(states.sensor.tlmtiago_next_alarm) - 60 * 20 }}"

However, when I try to test it, I get the error:

Error rendering data template: ValueError: Template error: as_timestamp got invalid input ‘<template TemplateState(<state sensor.tlmtiago_next_alarm=2024-05-22T21:00:00+00:00; Local Time=Wed May 22 22:00:00 GMT+01:00 2024, Package=com.samsung.android.app.routines, Time in Milliseconds=1716411600000, device_class=timestamp, icon=mdi:alarm, friendly_name=TlmTiago Next alarm @ 2024-05-22T17:02:19.765273+01:00>)>’ when rendering template ‘{{ as_timestamp(states.sensor.tlmtiago_next_alarm) - 60 * 20 }}’ but no default was specified

Any suggestions?

use this format:

states.sensor.tlmtiago_next_alarm - timedelta(minutes=20)

and also no need to use timestamp since you’re starting in datetime. just use datetime.

resulting in:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.tiago_alarm
data:
  datetime: "{{ states.sensor.tlmtiago_next_alarm - timedelta(minutes=20) }}"

That results in:

Error rendering data template: TypeError: unsupported operand type(s) for -: ‘TemplateState’ and ‘datetime.timedelta’

what does

{{ states('sensor.tlmtiago_next_alarm') }}
give you if you put it in dev-tools->template?

does it give you a datetime? if so. this:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.tiago_alarm
data:
  datetime: "{{ states('sensor.tlmtiago_next_alarm') - timedelta(minutes=20) }}"

if it doesn’t then post what it gives you.

It does.

Upon using that code:

Error rendering data template: TypeError: unsupported operand type(s) for -: ‘str’ and ‘datetime.timedelta’

ok, that error means you’re trying to substract a string. not a datetime.

i’m suspecting now that the sensor you have is not a datetime. but that it’s a string that looks like datetime

could you paste in what this gives you?

{{ states('sensor.tlmtiago_next_alarm') }}

if my hypothesis is true AND if it’s in the proper format, then

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.tiago_alarm
data:
  datetime: "{{ as_datetime(states('sensor.tlmtiago_next_alarm')) - timedelta(minutes=20) }}"

that should convert it to a proper datetime.

however if it’s not a proper datetime (e.g. if it’s time only) or not the right format, then it won’t work… so if it doesn’t, please share what that states() template gives.

That worked! Thank you!

I’ve ran into a new issue though… And it’s a weird one.

My trigger for this automation is everyday at 5:30… And for some reason, my phone reports my next alarm at 5:30. Changed the automation to 6:30, the next alarm also changed to 6:30.

Dafuq.

1 Like

Nevermind, Samsung phones are dicks apparently. I’ll figure it out. Thanks!

cool! glad this is working. strings are deceptive… they can look like the real object but not be the real object!