Input.datetime automation to offset another input.datetime

Hey I’m stuck trying to get this to work. I’ve got an automation that sets the house bedtime based on if it’s a school night/weekend/holiday. I’ve got that part figured out in an automation that is triggered at 7pm every night.
I’ve got another automation that then takes the time from ‘input.datetime.bedtime’ and shuts down the house.
I would like to also set another ‘input.datetime.pre_bedtime’ that a separate automation uses to give a warning that there is only 30 minutes until bedtime. I cannot figure out how to subtract 30 minutes from the bedtime and update the pre_bedtime. Any help would be appreciated.
Here is my automation:

alias: Set bedtime
description: ''
trigger:
  - platform: time
    at: '19:00:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.schoolnight_sensor
            state: 'on'
        sequence:
          - service: input_datetime.set_datetime
            data:
              time: '{{ states(''input_datetime.weekday_bedtime'') }}'
            entity_id: input_datetime.bedtime
    default:
      - service: input_datetime.set_datetime
        data:
          time: '{{ states(''input_datetime.weekend_bedtime'') }}'
        entity_id: input_datetime.bedtime
  - service: input_datetime.set_datetime
    data:
      time: '{{ states(''input_datetime.bedtime'') - 1800| int | False) }}'
    entity_id: input_datetime.pre_bedtime
mode: single

According to the documentation, the value you use to set time must be a string in HH:MM:SS format, not an integer.


I suggest this:

      time: "{{ (state_attr('input_datetime.bedtime', 'timestamp') - 1800) | timestamp_custom('%H:%M:%S', false) }}"
2 Likes

Hi,

It’s fun because it’s exactly what I tried to do yesterday and finally it works now.

Here is what I do :

  • I use a shortcut on my iPhone that send my next alarm value to Home Assistant. It is written into a input_datetime.my_next_alarm
  • I use an automation that is triggered by any change on the entity “input_datetime.my_next_alarm” and set another input_datetime.offset_alarm that is 30 minutes before the first one.

Here is my automation :

alias: Time - Update offset for morning routing
description: ''
trigger:
  - platform: state
    entity_id: input_datetime.my_next_alarm
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      time: "{{ strptime(states('input_datetime.my_next_alarm'), '%H:%M:%S') - strptime('30', '%M') }}"
    target:
      entity_id: input_datetime.offset_alarm
mode: single

Then I have my final automation that is triggered at the time set in the input_datetime.offset_alarm :

alias: Radiateur - Salle de bain - Allumage au reveil
description: ''
trigger:
  - platform: time
    at: input_datetime.offset_alarm
condition:
  - condition: numeric_state
    entity_id: sensor.ms_salle_de_bain_temperature
    below: '18'
action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.thermostat_sdb
    data:
      hvac_mode: heat
  - service: climate.set_temperature
    target:
      entity_id: climate.thermostat_sdb
    data:
      hvac_mode: heat
      temperature: 19
mode: single

PS: Please excuse my english as it’s not my native language.

This got it! Thanks so much @123, I tried something like this to begin with using a value template but when I didn’t get it to work I assumed I had to use an integer instead.
It’s one of those things where I was staring at the problem too long and needed to step back and let someone show me how simple it could be
Thanks again!

1 Like