Turn on light for X minutes, but not off when manually turned on

Hi, I’m using Aqara motion sensor on our nappy change table (sorry, I’m not English native).

The idea was to automatically turn on lamp for a couple of minutes when it’s dark. I have the automation for ON, but what I’m looking for is a kind of “run for 3 minutes” action parameter.

I did have the separate automation to turn the switch to “off” when no motion was detected BUT that had the unfortunate effect of also turning off the light when It was manually turned it on before. So this approach doesn’t work in this case.

Any tips?

I’d wrap the switch in a template switch that also flips an input boolean so you could tell if it was turned on by home assistant. Maybe an automation that turns the boolean after a few seconds. just leave it on long enough so whatever automations can run.

Then set up things to either use the original switch or the template switch that flips the boolean.

Can’t really write YAML because I’m only on my phone.

1 Like

The automation @SteveDinn is referring to would look something like this:

# configuration.yaml
input_boolean:
  nappy_table_light_auto:

# automations.yaml
- alias: "nappy table light on with motion"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion
      to: "on"
  condition:
    - condition: state
      entity_id: light.nappy_table
      state: "off"
  action:
    - service: light.turn_on
      entity_id: light.nappy_table
    - service: input_boolean.turn_on
      entity_id: input_boolean.nappy_table_light_auto

- alias: "nappy table light off with no motion"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion
      to: "off"
  condition:
    - condition: state
      entity_id: input_boolean.nappy_table_light_auto
      state: "on"
  action:
    - service: light.turn_off
      entity_id: light.nappy_table
    - service: input_boolean.turn_off
      entity_id: input_boolean.nappy_table_light_auto

You may have issues with this, though.

Depending on how you turn the light on manually, if doing so requires you to be in the view of the motion sensor, it will already be turned on automatically. Which will defeat this setup. If that is the case for you, the best solution is to have a button near the nappy table that you press to mean “leave the light on until I press the button again”, which, of course, requires different automations.

It’s a problem that seems so easy, right? I would presume there’s a simple FOR: XX TIME parameter. Thanks for your inputs.

Here’s what I was originally proposing. First we have an input_boolean to keep track of whether or not the light is on because it was triggered by your motion sensor

input_boolean:
  light_triggered_by_motion:
    name: Light triggered by motion

Now we have your original light and the template wrapper light that also turns on the input_boolean. Your motion automation would turn on the template light instead of the original light.

light:
  - platform: whatevs
    name: original_light

  - platform: template
    switches:
      triggered_light:
        value_template: >
          {{ is_state('light.original_light', 'on') }}
        turn_on:
          - service: homeassistant.turn_on
            data:
              entity_id:
                - light.original_light
                - input_boolean.light_triggered_by_motion
        turn_off:
          - service: homeassistant.turn_off
            data:
              entity_id:
                - light.original_light

Lastly, we have a couple of automations that turns off input_boolean.light_triggered_by_motion whenever light.original_light is turned off (we need this because the template light could be triggered to turn it on, but then the switch used to turn it off) and to turn off the triggered light after 3 minutes.

automation:
  - alias: turn_off_boolean_with_original_light
    trigger:
      - platform: state
        entity_id: light.original_light
        from: 'on'
        to: 'off'
    action:
      - service: input_boolean.turn_off
        data:
          entity_id:
            - input_boolean.light_triggered_by_motion
  - alias: turn_off_triggered_light_after_time
    trigger:
      - platform: state
        entity_id: light.original_light (light.triggered_light would also work here, it doesn't really matter)
        from: 'off'
        to: 'on'
        for: '00:03:00'
    condition:
      - condition: state
        entity_id: input_boolean.light_triggered_by_motion
        state: 'on'
    action:
      - service: homeassistant.turn_off
        data:
          entity_id:
            - light.original_light

Now you’re able to reference input_boolean.light_triggered_by_motion to see if your light is on because somebody switched it on using light.original_light or it was triggered by something that used light.triggered_light.

2 Likes

If nothing else is ever going to trigger the light, your automation for turning it on could just set the input_boolean to ‘on’ and skip the whole template switch part.

You guys are crazy, thanks! :slightly_smiling_face:

I’ve found an easy solution that also seems to work:

- id: '1573030009223'
  alias: Aqara motion light ON
  description: ''
  trigger:
  - device_id: f6cc3037fbff4755acecd446a3d127bc
    domain: binary_sensor
    entity_id: binary_sensor.aqara_body_sensor_occupancy
    for:
      hours: 0
      minutes: 0
      seconds: 3
    platform: device
    type: motion
  condition:
  - after: '18:00'
    before: '7:00'
    condition: time
  action:
  - data:
      brightness_pct: 100
      entity_id: light.bedroom_light
      kelvin: 3000
    service: light.turn_on
  - delay: 00:02:30
  - data:
      entity_id: light.bedroom_light
    service: light.turn_off

It doesn’t turn off the light when it was not switched on by the motion sensor because the if it wasn’t, the whole automation was not called.

Does that motion sensor ever get tripped when the light had already been turned on manually?

Not often if ever. I’ve set it to only engage when motion detected for longer than 3 seconds. I will use your logic for other stuff though, it’s very useful.

I hope you don’t mind I tag along on this post :slight_smile:

I’m facing a situation at home that somewhat resembles the post you commented on up here?

Using the above suggestions I set u an automation for my living room based on motion when the sun is below the horizon and the light group ‘living room’ is off (This condition is there so it doesn’t trigger when I turned on any light manually in the living room).

When there’s no motion and the input boolean is set to ON (switched to ON by motion automation), the group living room is turned off; this was to make it easy, but I can change this to only the lights that got turned on by the motion automation.

The living room has got a bunch of lights (some in groups because there are for example 4 Philips Hue bulbs in one lamp). I mostly control them using scenes. In every scene évery light has a state ON or OFF (not only the lights that are turned on by the scene), so that when I switch from ‘read lighting’ to ‘mood lighting’ only the lights come on that I want and others are turned off.

Before when the sun would set quite late I’d ask google to start the read scene and because this turned the group ‘living room’ to ON the motion automation would not trigger when the sun dropped below the horizon.

Now that it’s getting dark earlier I’ve ran into a problem… When I walk into the living room and the sun is below the horizon the motion automation triggers; that’s oke. Right after I ask google to switch the read scene on; that works too. BUT… the ‘no motion automation’ still triggers and makes all the lights turn off. I can change this to only turn off the lights that got triggered by the motion automation, but some of them are part of the read scene…

Does anyone know how I can set this up so that the motion automation triggers when all lights are off (group living room = off) and the sun is below the horizon and, when no other lights are turned on, the ‘no motion automation’ turns the lights off BUT when I turn on lights right after the motion automation is triggered the ‘no motion automation’ leaves the lights triggered by the scene on?

Long story, hope you guys understand and can give me a hand :slight_smile:

Thank you!

These are the automations:

- alias: PIR woonkamer nachtlamp ON (nachtlamp = night light)
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_03_woonkamer_presence
    to: 'on'
  condition:
  - condition: state
    entity_id: light.woonkamer (woonkamer = living room)
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: light.turn_on
    data:
      entity_id: light.licht_hoek
      brightness_pct: 45
      kelvin: 2237
      transition: 5
  - service: light.turn_on
    data:
      entity_id: light.licht_eetkamer_2
      brightness_pct: 25
      kelvin: 2237
      transition: 5
  - service: light.turn_on
    data:
      entity_id: light.licht_eetkamer_3
      brightness_pct: 25
      kelvin: 2237
      transition: 5
  - service: input_boolean.turn_on
    entity_id: input_boolean.pir_woonkamer_nachtlamp
  id: 0cc7092f8b3140e4aea28e7262cf04f2`

- alias: PIR woonkamer nachtlamp OFF
  trigger:
  - platform: state
    entity_id: binary_sensor.pir_03_woonkamer_presence
    to: 'off'
  condition:
  - condition: state
    entity_id: input_boolean.pir_woonkamer_nachtlamp
    state: 'on'
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - service: light.turn_off
    data:
      entity_id: light.woonkamer (light group with all living room lights)
      transition: 5
  - service: input_boolean.turn_off
    entity_id: input_boolean.pir_woonkamer_nachtlamp
  id: e1a3bad1d4404f198e34a3258424e144