Change brightness then turn off light after 5 sec

Hi All,

can I create a action when I turn off a input_boolean that light goes to brightness 10 and then turn off after 5 sec?

I use the following automation to turn off my lamp three minutes after I leave the room. That’s a single action. You want to perform two actions:

  1. brightness to 10
  2. turn off after 5 seconds

See if this example helps you any:

- alias: 'Turn my lamp OFF 3 minutes after room occupied'
id: '0022'
initial_state: true
trigger:
  platform: state
  entity_id: binary_sensor.myroom_occupancy
  to: 'Off'
  for:
    minutes: 3
action:
  service: homeassistant.turn_off
  entity_id: switch.my_lamp
1 Like

Thanks I will try this config.

Its so different then dzVents LUA script of Domoticz… For me it seems much more flexible then YAML… haha :slight_smile:

Try this;

  action:
    - service: light.turn_on
      entity_id: yourlight_id
      data:
        brightness: 10
    - delay: '00:00:05'
    - service: light.turn_off
      entity_id: yourlight_id
2 Likes

Thank you, @sjee. :slight_smile:
I didn’t know you could stack actions like that.
I learned something new today.

This will do it. Change the names of the light and input_boolean to whatever you’re using.

- alias: 'Turn light OFF after delay'
  trigger:
  -  platform: state
     entity_id: input_boolean.test
     to: 'off'
  -  platform: state
     entity_id: input_boolean.test
     to: 'off'
     for: '00:00:05'
  action:
    - service_template: "light.turn_{{'on' if trigger.for == None else 'off'}}"
      data:
        entity_id: light.test

The automation uses two triggers:

  • The first is when the input_boolean is set to off.
  • The second is 5 seconds after the input_boolean was set to off.

The action must determine which one of the two triggers fired in order to turn the light on or off. It does this by examining trigger.for

  • The first trigger has no for: statement. When it fires, trigger.for will be None.
  • The second trigger has a for: statement. When it fires, trigger.for will be a timedelta object with a value of 0:00:05. In other words, it won’t be None.

The service_template uses this difference to produce light.turn_on or light.turn_off depending on which trigger occurred.

2 Likes

Thanks for the useful example and explanation !!! this will help me to understand the way how but work!!

Interesting, but not sure I understand where the brightness to 10 is happening in this automation

That’s because it’s not happening. :slight_smile:

To set the brightness to 10 requires a modification that will only work if the light’s state can be controlled by brightness alone. The action becomes this:

  action:
    - service: light.turn_on
      data_template:
        brightness: "{{10 if trigger.for == None else 0}}"
        entity_id: light.mantle

light.turn_on with brightness: 0 will set the light to off (but only if the light is capable of being controlled this way). My MQTT Light entities have on_command_type set to brightness so they can be controlled exclusively by brightness. YMMV.

In case you’re wondering why I didn’t do this:

  # Do not do this ...
  action:
    - service_template: "light.turn_{{'on' if trigger.for == None else 'off'}}"
      data_template:
        brightness: "{{10 if trigger.for == None else 0}}"
        entity_id: light.test

Home Assistant complained when it encountered light.turn_off with brightness: 0. It did not want to see a brightness option used with light.turn_off

I question the choice of 10 for brightness. Maybe poudenes isn’t aware that it means 10/255 and is very dim (some dimmable LED lights might not even turn on).

Thanks for the explanation, always nice to see there are multiple ways to do things.

Your solution to use delay is probably the most straightforward for this application.

In this case it is brief (5 seconds) but I’ve seen examples where it is set to many minutes or longer. I’m not sure everyone realizes delay blocks execution.

Whether delay is used in an automation’s action or in a script, while delay is counting down to expiration, that action or script cannot be executed again. If used in an action, there’s no easy way of resetting delay.

In contrast, for acts like a timer (like a hidden timer). It doesn’t block execution and you can reset it. For example:

  -  platform: state
     entity_id: binary_sensor.test
     to: 'off'
     for: '00:02:00'

If the binary_sensor changes state during the 2-minute period defined by for then the countdown is restarted. This is different from delay and may, or may not, be desirable behavior for one’s application.

My personal preference is to use timers to create delays. They have start and finish events and can be restarted and cancelled. They don’t block execution. However, they do involve more configuration so sometimes a short delay is the simplest, cleanest solution.

1 Like