How to turn lights of after they started with an automation

After a lot of hours of work to set up Home Assistant on RPI3, I now can control my Hue lights, my Toon thermostat and I have my first automations working!

What I’m trying to create now: I would like to turn the lights on when its dark at daytime. Some rules I created so far:

  • Hue motion sensor detects movement (so someone is at home)
  • Light level measured bij hue motion sensor is below its setpoint (I’m not sure if this part is working, still need some testing)
  • Lights are not turned on yet

Automation.yaml:

- id: '1575622289987'
  alias: licht aan bij beweging
  description: ''
  trigger:
  - device_id: 7a27b17bb7424c7cb4f54a947e030e3c
    domain: binary_sensor
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    platform: device
    type: motion
  condition:
  - condition: state
    entity_id: light.beneden
    state: 'off'
  - below: 500
    condition: device
    device_id: 7a27b17bb7424c7cb4f54a947e030e3c
    domain: sensor
    entity_id: sensor.hue_motion_sensor_1_light_level
    type: is_illuminance
  action:
  - scene: scene.helder

Now this means that the lights will stay on for the rest of the day, or untill the next setting is activated. So I would like to get the lights turned off 30 minutes after this automation started.
I cannot get it to work when I add a delay in the automation. I’m also not sure if I can arrange this with an automation or a scrip.

Setup another automation that triggers after the light has been on for 30 minutes - if you really want it to just turn off after 30 minutes.

Or look at entity controller which is an add on that manages most of this for you :wink:

Ok, tanks. I will have a look at entity controller later. I tried shortly, but didn’t manage to install it yet.
But first I prefer to get it working with some own made automations to learn and understand a bit more

What exactly does this mean?

Do you get an error?

So I have an automation that turns on a light after sunset, then turns off a certain amount of time later:

- alias: Front Lights
  initial_state: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -1.0
  action:
  - service: light.turn_on
    entity_id: light.front_garden_lights
  - delay: 01:30:00
  - service: light.turn_off
    entity_id: light.front_garden_lights

I would have thought yours would look something like this:

- id: '1575622289987'
  alias: licht aan bij beweging
  description: ''
  trigger:
  - device_id: 7a27b17bb7424c7cb4f54a947e030e3c
    domain: binary_sensor
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    platform: device
    type: motion
  condition:
  - condition: state
    entity_id: light.beneden
    state: 'off'
  - below: 500
    condition: device
    device_id: 7a27b17bb7424c7cb4f54a947e030e3c
    domain: sensor
    entity_id: sensor.hue_motion_sensor_1_light_level
    type: is_illuminance
  action:
  - service: scene.turn_on
    entity_id: scene.helder
  - delay: 00:30:00
  - service: scene.turn_off
    entity_id: scene.helder

I assume you tried to turn off your lights witch turn_off scene.helder, right? The problem is that you can’t turn off scenes. Think about it, say you have a scene where the bulb in the livingroom is turned on at 20% and the bulb in the bedroom is turned off, how would you want to turn off this scene or what behaviour would you expect.

Either create a turned_off scene and turn on this one after the delay or try adding a delay and turn off the lights instead of turning off the scene.

action:
  - service: scene.turn_on
    entity_id: scene.helder
  - delay: 00:30:00
  - service: lights.turn_off
    entity_id: light.yabbayabba

Indeed I want to turn the lights off, not the scene.

action:
  - service: scene.turn_on
    entity_id: scene.helder
  - delay: 00:30:00
  - service: lights.turn_off
    entity_id: light.yabbayabba

So this works fine to turn the lights off after 30 minutes.
Next thing to add is, I want them only to turn off is no motion is detected for 30 minutes…
(lights can be turned off if no one is at home or upstairs)