Valid use of for as part of action to control a light?

Is the following a valid use of ‘for’ as part of an automation action, or do I need to use a timer instead?

The config checker parses this as valid but want to make sure (will the light go off after 5 min?).

  alias: Rainy
  trigger:
  - entity_id: group.family
    platform: state
    to: home
  condition:
  - condition: and
    conditions:
      condition: state
      entity_id: light.porch_light
      state: 'off'
      condition: state
      entity_id: weather.dark_sky
      state: 'rainy'
  action:
  - service: light.turn_on
    data:
      entity_id: light.porch_light
      for:
        hours: 0
        minutes: 5
        seconds: 0

I don’t think it will work, because you do not have anything that tells it to go off

hmmm, that’s what I suspected. I’ll go for a timer then together with another automation that gets called after 5 min to turn the light off

The config checker failed to report the error; you can’t use for in that manner. The proof is when you execute the automation it will produce an error message.

I used this automation:

- alias: 'test for in action'
  trigger:
    platform: state
    entity_id: input_boolean.flash
    to: 'on'
  action:
    service: light.turn_on
    data:
      entity_id: light.family
      for: '00:00:10'

and executing it produced this error message in the system log:

2019-07-12 09:10:29 ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.test_for_in_action. Invalid data for call_service at pos 1: extra keys not allowed @ data[‘for’]

You can use a 5-minute delay in the action: turn on the light, delay for 5 minutes, turn off the light. However, while the delay is counting down from 5 minutes, the automation is considered ‘in use’ and cannot be executed again until the countdown is finished. Depending on your application, this may or may not be an issue.

  action:
  - service: light.turn_on
    entity_id: light.porch_light
  - delay: '00:05:00'
  - service: light.turn_off
    entity_id: light.porch_light

Thanks @123 that’s good to know, for now I’ve just finished implementing a timer and working out all the kinks in my yaml so I’ll see if that works.

The advantage of using a timer is that, compared to delay, it doesn’t ‘block’ the automation.

Example of ‘non-blocking’ operation:

  • The automation is triggered, its action is executed, and the timer is started.
  • While the timer is counting down, let’s say the automation is triggered again.
  • It executes the action (it can because there’s no delay to block it).
  • What will happen (or should happen) is that the timer is simply restarted.