Setting up basic motion activated Lighting using Home Assistant Configuration

This post explains how to set up motion activated lighting that stays on for as long as there is activity in a room. I wanted to share my automations that integrate the dockerised RF gateway I wrote about before with Home Assistant automations.

We will assume that you have set up your motion sensors as binary_sensor in your Home Assistant configuration and that sense sensors accurately track the state of your motion sensors. Please check out the linked article if you need instructions on how to do this. We will assume that you have set up your motion sensors as binary_sensor in your Home Assistant configuration and that sense sensors accurately track the state of your motion sensors. Please check out the linked article if you need instructions on how to do this.

While this post helped me understand the requirements and get my head around the ideas, I highly recommend you check out my article A complete Guide to Motion Lighting in Smart Homes - Introducing AppDaemon instead, as it presents a better solution using Python objects (no messy YAML configuration). If you want to use YAML (or you do not plan on adding more motion sensors in the future), then read on and be warned.

Creating motion activated lights

Let us start using our motion sensors by adding some automations for room-based, motion-activated lighting!

I defined a group which contains all lights I want to turn on when motion is detected in the living room. These groups are not used in the traditional way. Rather, we will use them internally in automations. Once you get your setup working it’s a good idea to hide these ‘meta’ groups from your front end using hide_entity: true.

### MOTION PRESENCE GROUPS ########################
lr_presence:
  name: LR Presence
  entities:
    - light.living_room_floor_lamp
    - light.kitchen_lamp # due to the layout of my house, it makes sense to include a kitchen light.

Home Assistant v0.65 introduced the idea of Group Lights — a group that is designed to be part of the light component (and other components in the future). Of course, I finished implementing my configuration before finding out about this new Group Light component. It’s the perfect use case.

- alias: Motion Sensor Lighting
  hide_entity: yes
  trigger:
    platform: state
    entity_id: binary_sensor.living_room_motion
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: or  # 'when dark'
        conditions:
          - condition: sun
            after: sunset
          - condition: sun
            before: sunrise
      - condition: state
        entity_id: group.lr_presence
        state: 'off'
  action:
    - service: notify.pushbullet
      data:
        message: Motion detected !
    - service: homeassistant.turn_on
      entity_id: group.lr_presence

- alias: No motion - turn off lighting
  hide_entity: yes
  trigger:
    platform: state
    entity_id: binary_sensor.living_room_motion
    to: 'off'
    for:
      minutes: 10
  action:
    - service: homeassistant.turn_off
      entity_id: group.lr_presence


I highly recommend you take this implementation with a grain of salt. I have since learned about a [better way] to code motion lighting. Please check out my article titled A complete Guide to Motion Lighting in Smart Homes - Introducing AppDaemon.

The first automation above is triggered by the motion sensor. I added a condition block with the following effect:

The automation will only execute when it is dark outside and none of the devices in the respective group is switched on. That second condition is to prevent motion sensors from turning on the entire group when one or more lights within in the group is already switched on.

If you switched on some lamps manually, that means the problem of lighting has already been taken care of. Therefore no need to activate motion based lighting.

Problem: Motion lights turn off manually controlled lights after time out period expires

This actually creates a design nightmare for us… How do we stop our motion automations from turning off our lamps in cases we do not want this to happen?

Say you switched on your lights manually because you are chilling in the lounge with friends. You get up to grab a drink and your motion sensor is triggered. Motion Sensor Lighting will not execute because of its carefully designed conditions. However, the binary_sensor.living_room_motion entity is now in the on state regardless. This means that the second automation will turn off all your manually controlled lights after 10 minutes of inactivity despite the fact the first automation ignored this event initially.

I hope it’s making sense.

How do we prevent this problem from occuring? The issue is made worse when we consider that this problem is duplicated across all rooms and each motion sensor you set up. Any automations that attempt to solve this issue would have to be duplicated for each motion sensor in your configuration!

I suppose it comes down to your preference:

  • Do you want to enforce a strict “lighting for as long as there is motion” policy? (in other words “no motion - no lighting”?)
  • or do you want to have manual control over lighting.

Both attempts are appealing. Your garage and lounge room fit into those categories.

Regarding Motion Sensor Placement

Depending on where you place your motion sensor, you can achieve “no motion - no lighting” anywhere in your house! The No motion - turn off lighting automation will not trigger unless there has been no motion for a period of time. The timer will be reset as soon as motion is detected. That’s why the positioning of your motion sensor is important. Make sure it captures the motion activity Home Assistant needs to know about. Kinda obvious, but you see where I’m coming from.

Conclusion

This is a long one. We covered a lot. See ya.