Guidance, please, on setting up motion-based lighting

Please note - I am a total noob with HA. I have a brand new hassbian install on a Raspberry Pi but I have yet to add a single component or make it do anything useful. I just login and see the welcome message. I’m struggling to get beyond that,

One of my goals is to have motion detection combined with light level detection that automatically turns on my kitchen lights. If it’s evening and we’re watching a movie/TV, then turn on the LED striplights under the cabinets. If we’re not watching TV, turn on the can lights in the ceiling. If it’s late at night (and I’m up to get a drink of water), then turn on the striplights. But if it’s morning or afternoon and it happens to be below a certain lux level (because it’s overcast outside or whatever), then turn on the can lights.

In case that’s as clear as mud, here’s some pseudocode…

If motion (detected in kitchen) {
   check light level
   if light_level = low {
      check TimeOfDay
      If TimeOfDay = evening {
         check IsTVon
         if TVon {
            turn on LEDstripLights
         } else {
            turn on CanLights
         }
      } ElseIf TimeOfDay = latenight {
         turn on LEDstripLights
      } else {
         turn on CanLights
      }
   } else {
      ; do nothing
   }
}

I have three Bosch (Zigbee) motion sensors, some LED strip lighting that’s not yet installed, and a Sonoff switch (that I put in the wall box behind the preexisting switch) that controls 6 can lights in the kitchen ceiling. My questions are…

  1. Can anyone recommend an inexpensive, battery-powered light sensor? (Ideally, a DIY project?)
  2. Assuming I start the movie/TV with an HA command, can I set a global “WatchingTV” variable that other scripts can read and act on?
  3. Is there a similar script maybe out there already that I could use as a template?
  4. Someone must be doing something like this now. Any pointers would be appreciated!

Well, I’ll start with the bad news…

If your Bosch motion sensors are model ISW-ZPR1-WP13 then (as far as I know) they aren’t supported yet in HA. I bought 4 of them about 9 months ago because they were so cheap only to find out that I can’t get them to work in HA. They pair but then in about 30 seconds they disappear again from the system and never show back up. Lots of people here are in the same boat and the devs aren’t very forthcoming on if they will ever be supported.

If they aren’t that model then never mind…

or if you figure out how to get them to work let everyone know.

As far as the motion controls. Here is a snippet of my motion controls section of the automations.yaml file:

Computer Room Light Motion Control

- alias: Turn on computer room lights when there is movement
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.comp_rm_motion_template
      to: 'on'
    - platform: state
      entity_id: input_boolean.bool_1
      to: 'on'
  action:
    - service: homeassistant.turn_on
      entity_id: light.sengled_e11g13_03071df9_1

- alias: Turn off computer room lights 3 minutes after last movement
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.comp_rm_motion_template
      to: 'off'
      for:
        minutes: 3
    - platform: state
      entity_id: input_boolean.bool_1
      to: 'off'
  condition:
    condition: state
    entity_id: input_boolean.bool_1
    state: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: light.sengled_e11g13_03071df9_1

Along with the above I also have the ability built-in to override the 3 minute timer to keep the light from going off if I’m in front of the computer and not moving much by using the following automations, script and an input_boolean (a fake switch):

Here are the automations for that:

- alias: Computer Room Light Control
  trigger:
    - platform: state
      entity_id: input_boolean.bool_1
      to: 'on'
    - platform: state
      entity_id: input_boolean.bool_2
      to: 'on'
  action:
    - service: script.turn_off
      entity_id: script.comp_light_control
    - delay: '00:00:02'
    - service: script.turn_on
      entity_id: script.comp_light_control
      
- alias: Computer Room Light Control from Motion
  trigger:
    - platform: state
      entity_id: binary_sensor.comp_rm_motion_template
      to: 'on'
    - platform: state
      entity_id: binary_sensor.gs5_webcam_motion_template
      to: 'on'
  condition:
    condition: state
    entity_id: input_boolean.bool_1
    state: 'on'
  action:
    - service: script.turn_off
      entity_id: script.comp_light_control
    - delay: '00:00:02'
    - service: script.turn_on
      entity_id: script.comp_light_control
     
- alias: Stop Computer Room Light Script
  trigger:
    platform: state
    entity_id: input_boolean.bool_1
    to: 'off'
  action:
    service: script.turn_off
    entity_id: script.comp_light_control

And here is the script:

comp_light_control:
    alias: Computer Room Light Control Script
    sequence:
      - delay: '00:25:00'
      - service: light.turn_off
        data:
          entity_id: light.sengled_e11g13_03071df9_1
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.sengled_e11g13_03071df9_1
      - delay: '00:00:01'
      - service: light.turn_off
        data:
          entity_id: light.sengled_e11g13_03071df9_1
      - delay: '00:00:01'
      - service: light.turn_on
        data:
          entity_id: light.sengled_e11g13_03071df9_1
      - delay: '00:05:00'
      - service: homeassistant.turn_off
        data:
          entity_id: input_boolean.bool_1

basically it waits 25 minutes after the last movement and then flashes the light a couple of times to give me a five minute warning. If i want it to stay on I hit the reset switch and it starts everything over again. If I’m not in the room any longer it will turn off automatically after the five minutes is up.

For the basics just start with the first set of automations. I posted the other stuff just to kind of give you an idea of whats possible and other examples of working logic. I’m not saying it’s the best way to do it but it works for me.

Hope it helps…

Thanks again, finity! Yep, those are the Bosch motion detectors I bought. I read one of the Amazon reviews that gave a step-by-step procedure for getting them working in SmartThings, and since I had already seen something on these forums about interfacing with a ST hub, I figured I could get them working that way but I think I’ll return them.

I’m hoping to cobble together the motion and light sensors from here only make it battery powered using ESP01 microcontrollers as seen here. Not long ago, I bought an Arduino development kit but I haven’t had a chance to get started. If/when I’m able to get this working, I’ll post detailed instructions and a video.

For motion lighting I highly suggest you look into AppDaemon. I spent a lot of time tinkering with motion sensors and getting lights to do what I want them to do. Check out the blog post

Edit since late 2018: Please check out my Entity Controller component. It is specifically built to handle motion lighting use cases and offers a ton of configuration options.

@frits1980 thanks for commenting

Or you could look at “entity controller” in HACS.