Simple Light automation to turn light ON and OFF according to motion in a room

This is a straight forward light automation.
It turns lights on when a motion is detected. It also turns lights OFF when there is no motion for a certain number of minutes (the number of minutes range from 1 to 10, and this is decided by the user).

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: Turn Light ON and OFF
  description: Turn light on when motion is detected and off when motion is not detected.
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: Choose the motion sensor to control the lights.
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    target_light:
      name: Target Light
      description: Choose the lights that you want to control.
      selector:
        target:
          entity:
            domain: light
    delay_time:
      name: Number of mintes 
      description: Number of minutes, afterwhich the lights will turn off.
      selector:
        number:
          min: 0
          max: 10
          step: 1
          unit_of_measurement: "minutes"

trigger:
  - platform: state
    entity_id: !input motion_sensor

condition: []

action:
  - choose:
    - conditions:
      - condition: state
        entity_id: !input motion_sensor
        state: 'off'
        for:
          hours: 0
          minutes: 0
          seconds: 0
      sequence:
      - delay:
          hours: 0
          minutes: !input delay_time
          seconds: 0
          milliseconds: 0
      - service: light.turn_off
        data:
          transition: 10
        target: !input target_light
    - conditions:
      - condition: state
        entity_id: !input motion_sensor
        state: 'on'
      sequence:
      - service: light.turn_on
        data: {}
        target: !input target_light
    default: []
mode: restart
4 Likes

Can you explain how it does that?

  1. At the moment the motion sensor turns off the State Trigger is triggered.
  2. It skips the first conditions in choose because that’s for when the motion sensor turns on.
  3. The second conditions in choose is for when the motion sensor turns off. However, it must have been off for at least X minutes (delay_time). Except I don’t see how it can meet that criteria because it just turned off now, not X minutes ago (and it won’t wait in conditions until X minutes pass).

So how exactly does it ever satisfy the second conditions in choose with that single State Trigger?

FYI, I took the time to test the blueprint and it failed to turn off the light after the specified delay_time.

The blueprint doesn’t work for the reason I mentioned in my previous post. A State Condition with a for option doesn’t wait for the specified period to occur (that’s how a State Trigger with a for works but not a State Condition).

If you’re interested, the following blueprint does work. It uses two State Triggers:

  1. Detect when motion sensor turns on
  2. Detect when motion sensor turns off and remains off for the duration specified by delay_time.

It employs choose to determine which of the two State Triggers triggered and then performs the associated action.

blueprint:
  name: Turn Light ON and OFF
  description: Turn light on when motion is detected, and turn it off when motion is not detected.
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: Choose the motion sensor to control the lights.
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    target_light:
      name: Target Light
      description: Choose the lights that you want to control.
      selector:
        target:
          entity:
            domain: light
    delay_time:
      name: Number of minutes 
      description: Number of minutes, after which the lights will turn off.
      selector:
        number:
          min: 0
          max: 10
          step: 1
          unit_of_measurement: "minutes"

trigger:
  - platform: state
    entity_id: !input motion_sensor
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: !input motion_sensor
    from: 'on'
    to: 'off'
    for:
      minutes: !input delay_time
condition: []
action:
  - choose:
    - conditions: "{{ trigger.to_state.state == 'on' }}"
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target: !input target_light
    default:
      - service: light.turn_off
        data: {}
        target: !input target_light
mode: single

A small enhancement would be to add a State Condition to prevent turning on a light if it’s already on.

1 Like

Thank you for your feed back, I reviewed it and you were right, it was not working. I amended it little bit. It should be working now. Have a look at it and tell me what you think. Cheers.

It won’t work correctly. Inserting delay there will change the entire behavior (and not in a good way).

You are very right. I amended the code.
Now, any change in the state of the motion sensor will tigger the automation.
Option 1, if there is motion, turn light on.
Option 2, if motion stops, wait for 5 min and turn lights off.
This works better Thanks for the feedback.

I have been using it since the morning, and seems to be working ok with me. So far so good. I will keep an eye on it and update.

Check your log for any warnings about executing an automation that’s already running.

  • Let’s say the motion sensor has just turned off.
  • This triggers the automation and it executes the delay statement (before turning off the light).
  • While the delay is counting down, let’s say the motion sensor detects motion and turns on again.
  • This should trigger the automation again but it can’t because the automation is running in single mode and is still busy executing the delay.
  • The incident is reported in the log as a warning.
  • The light gets turned off after the delay expires (even though there was motion detected while it was counting down the delay).

You should know that there are several techniques to implement an auto-off light and most are already available as blueprints. The one I posted is just one of the ways to do it (successfully). You’re basically trying to reinvent the wheel using techniques that have already been proven to not to work.

So, I think if I change it to ‘Restart’ mode, that should solve the issue. Is my understanding right?

I do appreciate your help. It is just I am still learning :grinning:

You’re welcome but, for future reference, one doesn’t normally post a blueprint to Blueprint Exchange that simply doesn’t work.

If you have questions about the development of blueprints, post it in the Blueprints section (which is different from Blueprint Exchange).

Brilliant, thanks :grinning:

Or you can use the version I suggested which doesn’t need delay, works fine in single mode, and is half the size and complexity. :man_shrugging:

Thank you so much for your help. That’s brilliant.

Being totally new to Home Assistant and facing a steep learning curve, how would you do this

Add a State Condition to the conditions statement in choose.

Thank you for that, and I think I had concluded the same, the trouble is it is my complete nativity with YAML that is the problem, and despite having a go I cannot get my head around the coding for this.

Any help would be greatly appreciated.

Post what you tried but failed to work.

action:

  • choose:
    • conditions: “{{ trigger.to_state.state == ‘on’ }} & {{ target_light.state == ‘off’ }}”

I strongly recommend that you review the documentation links posted below because that’s so profoundly wrong that anything I provide is unlikely to make sense to you.

Conditions

Templating