Set input_datetime to time earlier than other input_datetime

I’m trying to figure out an automation to set input_datetime.prealarm to 20 minutes before input_datetime.alarm. When trying this:

- alias: set_prealarm
  id: set_prealarm
  trigger:
  - platform: state
    entity_id: input_datetime.alarm
  action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.prealarm
    data:
      timestamp: "{{ (states.input_datetime.alarm.attributes.timestamp | int) -20*60 }}"

the prealarm is 40 minutes later than the alarm. The timestamp shown in the entity attributes (in dev tools) is also a larger number. When testing the template in dev tools it calculates the correct lower number.
Both input_datetimes are configured as time only.
Can onyone help?

- alias: set_prealarm
  id: set_prealarm
  trigger:
  - platform: state
    entity_id: input_datetime.alarm
  action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.prealarm
    data:
      time: "{{ (state_attr('input_datetime.alarm', 'timestamp') - 1200) | timestamp_custom('%H:%M:%S', false) }}"

Works perfectly! Thanks for the quick help!

1 Like