Use lambda as time trigger

Hello community,

I’ve an automation triggered by an input_datetime, like so:

alias: Time to shower
description: ''
trigger:
  - platform: time
    at: input_datetime.shower

But, this time is for the weekdays when there is school/work the next day. Otherwise, Id like to add one hour to the trigger.
So, in pseudocode, something like so

trigger:
  - platform: time
    at: > -
      if weekday in (fri, sat)
        input_datetime.shower + 1 hour
      else
        input_datetime.shower
      fi

Is this something that can be done in any way?

My next challenge will be to mix that with calendar event about holidays ans stuffs like that but that’s another story.

EDIT: First idea : duplicate automation, one for week, one for weekend, duplicate input_datetime. Create an automation on change of first input_datetime, set value of second one. But, man, this is over complicated.

Time trigger allows a sensor of device_class: timestamp to be used as the at: input.

So set up a template sensor using the logic above, something like this (untested, assumes two input_datetime helpers are set up, and probably needs fiddling to get it recognised as a timestamp sensor):

template:
  - sensor:
      - name: "Shower time"
        device_class: timestamp
        state: >
          {{ iif(now().weekday() in (0, 1, 2, 3, 6),
             states('input_datetime.shower_weekday'),
             states('input_datetime.shower_weekend')) }}

then:

  - platform: time
    at: sensor.shower_time
1 Like

I’ll try and get back with what worked.

Another option is to use a template trigger. The following assumes the Input datetime is a time-only type.

trigger:
  - platform: template
    value_template:
      {% if now().isoweekday() in [5, 6] %}
      {% set offset = 1 %}{% endif %}
      {{ now() >= (today_at(states('input_datetime.shower')) 
      + timedelta(hours = offset|default(0)))  }}
1 Like

Both solutions are working, but I want to wait for a week-end.
I know, I could change the automation but, I’m leazy and prefer to wait for the real triggering

Thanks to @Didgeridrew and @Troon for the help.
I selected Drew’s solution at the end to save me an additional input_datetime and add the automation to set the second based on the first.

1 Like