Dynamically setting delays and transitions

Hi all.

I am making an esphome based, motion sensor driven RGB light.
I managed to have the basic behavior working, but now I am trying to get the different durations (fade-in, stay on and fade-out) settable by potentiometers.

I tried using lambdas to drive the transition_length and delayed_off parameters but they are not templatable.
It seems something that would be quite common, but my searches and experiments could not bring anything.

Any tip on how to proceed ?

My current yaml for reference:

light:
  - platform: rgb
    name: "Light"
    id: "light_strip"
    red: red_component
    green: green_component
    blue: blue_component

binary_sensor:
  - platform: gpio
    pin: 27
    id: movement
    name: "Movement Sensor"
    device_class: motion
    on_press:
      then:
        - light.turn_on:
            id: light_strip
            transition_length: 3s
  - platform: template
    name: "Movement Sensor Delayed"
    lambda: |-
      return id(movement).state;
    filters:
      - delayed_off: 60s
    on_release:
      then:
        - light.turn_off:
            id: light_strip
            transition_length: 30s

output:
  - platform: ledc
    id: red_component
    pin: 18
  - platform: ledc
    id: green_component
    pin: 13
  - platform: ledc
    id: blue_component
    pin: 26

From the specs:

  • transition_length (Optional, Time, templatable): The length of the transition if the light supports it.

Perhaps you should post some code of your template code you tried with.
Have a look at the examplecode at

in the section on_json_message Trigger … which does use lamba with transition_length

Thank you, I was trying too hard.

I was trying to do it like this:

            transition_length:
              seconds: !lambda |-
                return 3;

But it works without the seconds:, just with:

            transition_length: !lambda |-
              return 3000;

Any way to do it with delayed_off or to achieve an effect similar to it ?

When I try this:

      - delayed_off:  !lambda |-
          return 30000;

I get this error:

  filters: 
    - 
      This option is not templatable!.
      delayed_off: !lambda |-
        return 30;

@justone pointed me in the right direction for transition_length.
For delayed_off, there is no direct way to do this. But it can be achieved with a script and a delay step.

Here is the approach to take for anyone passing here:

script:
  - id: light_cycle
    mode: restart
    then:
      - light.turn_on:
          id: light_strip
          transition_length: !lambda |-
            return id(on_duration).state;
      - delay: !lambda |-
          return id(stay_duration).state;
      - light.turn_off:
          id: light_strip
          transition_length: !lambda |-
            return id(off_duration).state;
binary_sensor:

  - platform: gpio

    name: ${disp_name} Presence Sensor

    id: esp32_2_motion

    pin:

      number: GPIO27

      mode: INPUT_PULLDOWN

    on_press:

      then:

        - light.turn_on:

            id: backlight

            transition_length: 3s

            red: 1.0

            green: 0.2

            blue: 0.1

    on_release:

      then:

        - light.turn_on:

            id: backlight

            transition_length: 5s

            red: 0.0

            green: 1.0

            blue: 0.0

        - delay: 3s

        - light.turn_off:

            id: backlight

            transition_length: 3s

    device_class: motion

    filters:

      - delayed_on: 10ms

      - delayed_off: 2s