Template with time calculation and input_datetime helpers

Hi,

My ultimate goal is to have Voice Announcement on my Google devices to inform it is bedtime for the children. I made two input_datetime helpers called ‘bedtime_childname’ and a third input_datetime helper with a delay. Both bedtime helpers are set to the time that they need to go to bed, lets say 8PM. When it’s weekend, or when it’s holiday the delay should extend the bedtime with the input_datetime delay (30 minutes). The automation should kickin when it is actually bedtime, but this depends on the day of the week and an input_boolean whether it is vacation or not.
I’m thinking about two additional input_datetime helpers with the calculated bedtimes, but I cannot find articles explaining how to calculate with time, based on input_datetime and delay helpers.
Anyone any idea on how to approach my situation?

You can’t do it as an Input Datetime, but you could do it as a Template sensor with a device class of timestamp.

template:
  - sensor:
      - name: Calculated Bedtime Child 1
        state: |
          {% set base_time = today_at(states('input_datetime.bedtime_child_1')) %}
          {{ base_time + states('input_datetime.delay') | as_timedelta if 
          (now().isoweekday() >= 6 or is_state('input_boolean.vacation', 'on')) else base_time }}
        device_class: timestamp
      - name: Calculated Bedtime Child 2
        state: |
          {% set base_time = today_at(states('input_datetime.bedtime_child_2')) %}
          {{ base_time + states('input_datetime.delay') | as_timedelta if 
          (now().isoweekday() >= 6 or is_state('input_boolean.vacation', 'on')) else base_time }}
        device_class: timestamp

Once set up, either as Template Helpers or in your configuration, the sensors’ entity IDs can be used for the value of at in Time triggers.

trigger:
  - platform: time
    at: sensor.calculated_bedtime_child_1
condition: []
action:
  - #Your notification action for child 1

EDIT Corrected boolean issue mentioned below.

Hi @Didgeridrew ,
There is a small mistake in your template, but with a bit of googling I got it working. The if statements expects booleans and therefore the state check needs an ’ == “on”’ addition.

The actual template I now have is:

{% set base_time = today_at(states('input_datetime.bedtime_child_1')) %}
{{ base_time + states('input_datetime.delay') |
as_timedelta if (now().isoweekday() >= 6 or states('input_boolean.vacation') == 'on' ) else base_time }}

Thanks a lot!!

I think I meant to throw a bool filter on there… :man_facepalming:
I have fixed it above.

For anyone curious, there are a few ways to fix it:

{{ if ... states('input_boolean.vacation') | bool .... }}
{{ if ... is_state('input_boolean.vacation', 'on') ....  }}
{{ if ... states('input_boolean.vacation') == 'on' .... }}