Motion Light with few Condition

Hi Guys,

Am currently new to Home Assistant, am trying to move away from Samsung Smartthings, however am having issue with my motion lighting setup.

I’ve managed to get the lighting working with motion detect, however its not fully what i need.

I would like the light to go on if a motion is detected with condition of

  1. between Sunset and Sunrise
  2. If the light state is off (manual override)

Then turn it on.

But also at 23:00 (11:00pm) to Sunrise, i would like the light to reduce brightness to 20%.

If the light is switched on manual and motion is detected then do noting. This part i cant get it working, the motion overrides it and turns it off.

Also from Sunset to 22:59 10:59pm) increase the brightness to 100%.

Am also having issue with the motion timing, it doesn’t carry out the task in the time specified.

Please see my code below, i have striped it all back to just working with motion only:

 - id: '1610238984385'
  alias: Night light on motion (On)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'on'
  condition:
  - condition: state
    entity_id: light.hue_lightstrip_plus_2_2
    state: 'off'
  action:
  - service: light.turn_on
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  mode: single
- id: '1610239347729'
  alias: Night light on motion (Off)
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'off'
    for: 00:00:30
  condition: []
  action:
  - service: light.turn_off
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  mode: single

Please help!

Don’t all come to my rescue, haha.

I’ve managed to solve a part of my automation, override. So if the light is on triggered manually, the motion sensor detects movement it does nothing. If the light off and motion sensor detects movement then it will turn it on and off after 10 second.

10 second is more for testing, i will change the value to my desired value later.

My issue was, i created two sperate automation, on and off. Which the off automation overrides manually state. New code is all in one automation.

- id: '1610238984385'

  alias: Night light on motion

  description: ''

  trigger:

  - platform: state

    entity_id: binary_sensor.aeon_multipurpose_sensor_motion

    to: 'on'

  condition:

  - condition: and

    conditions:

    - condition: state

      entity_id: light.hue_lightstrip_plus_2_2

      state: 'off'

  action:

  - service: light.turn_on

    data: {}

    entity_id: light.hue_lightstrip_plus_2_2

  - delay: 00:00:10

  - service: light.turn_off

    data: {}

    entity_id: light.hue_lightstrip_plus_2_2

  mode: single

What left:

  1. How to set motion activation after sunset
  2. How to set motion deactivation after sunrise
  3. Sunset to 22:59:59 at full brightness
  4. 23:00:00 to Sunrise at 35% brightness

Is there a way to set a time to sun and sun to time i.e 23:00:00 to sunrise and sunset to 22:59:59?

I think i may have found a way with scenes, but i have to define time to time, which defeat the purpose of time tracking with sunset and sunrise.

Please help!

Untested but give this a try:

- id: '1610238984385'
  alias: Night light on motion
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.hue_lightstrip_plus_2_2
      state: 'off'
    - condition: or
      conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
  action:
  - choose:
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '22:59:59'
      sequence:
        - service: light.turn_on
          entity_id: light.hue_lightstrip_plus_2_2
          data:
            brightness_pct: 100
    - conditions:
      - condition: time
        after: '23:00:00'
      - condition: sun
        before: sunrise
      sequence:
        - service: light.turn_on
          entity_id: light.hue_lightstrip_plus_2_2
          data:
            brightness_pct: 35
  - delay: 00:00:10
  - service: light.turn_off
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  mode: single

edited to fix the sun condition, needs to be used as an or statement.

I use a timer. I have 1 automation that starts the timer and turns on the lights based on motion and any conditions. It’s mode is restart. Then a second automation on the timer expiration that turns the lights off.

Thanks Brain, I will give that a go.

Update you shortly.

Would you be able to share your code?

Thanks

Sure, here is the automation YAML for the Motion On:

alias: All Lights (Turn ON and set timer when motion detected)
description: ''
trigger:
  - type: motion
    platform: device
    device_id: xxxxxxxxxx
    entity_id: binary_sensor.1_motion_sensor_presence
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: xxxxxxxxxxx
    entity_id: binary_sensor.2_motion_sensor_presence
    domain: binary_sensor
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - scene: scene.flood_lights_100
  - scene: scene.outdoor_lights_100
  - service: timer.start
    data:
      entity_id: timer.lights_off
mode: single

and the Timer Expires:

alias: All Lights (Turn OFF and dim when timer expires)
description: ''
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.lights_off
condition: []
action:
  - scene: scene.flood_lights_off
  - scene: scene.outdoor_lights_off
mode: restart

I use scenes to turn the lights on and off. And of course you will need a timer setup, mine defaults to 5 minutes.

The worst part of all of it is the reliability of the Motion Sensors and the Kasa light switches. Other wise works great.

Thanks Rockdog,

I will add the code to mine and change where i need to for my device.

I have a samsung motion which works good for 85% of the time. My Aeotech MultiSensor 6 is a bit too slow for my liking. Maybe i need to update the code on samsung smartthing. Probably i wont as i want to connect it directly to my home assistant.

What Z-Wave and Zigbee modules are you using?

Hi Brian,

Thanks again for the code.

At first it didn’t work, so i added it all manually. Then same it wouldn’t work over motion, but the execute fine.

Looking at the code, i’ve removed sun option under condition where i had my override state.

Looking good, it works. But i yet to test the timing option and see if it reduces the brightness.

- id: '1610479739773'
  alias: New Automation
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.hue_lightstrip_plus_2_2
      state: 'off'
  action:
  - choose:
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '22:59:59'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        entity_id: light.hue_lightstrip_plus_2_2
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: '23:00:00'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 35
        entity_id: light.hue_lightstrip_plus_2_2
    default: []
  - delay: 00:00:10
  - service: light.turn_off
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  mode: single

I will let you know soon as i tested it further.

Thanks again. :slight_smile:

I use HUE motion sensors (inside and outside) and a couple IKEA TRADFRI switches connected to DECONZ/ConBee II and HUE lights on a HUE Hub. I have a bunch of Kasa wall switches and plugs (which I have problems with them not showing up on a restart of HA, if they are there they work pretty well. I have IKEA TRADFRI repeaters for the DECONZ network because I don’t have a lot of items on it. I might purchase a couple more to ensure good coverage. The biggest issue is the signal to the outdoor sensors is not great. I have considered moving DECONZ to ZHA but I asked about how to do that move without losing everything here on the forum and got no responses, so I’m holding off on that for now.

Hi Brain,

So, I’ve been playing with the code and testing it last night. I found a issue, issue was after 00:00:00 the lights would not work. When i check the brightness it was reduced to 0 every time i trigger it. I would manually change it to 100% and then trigger the motion and no luck. Light would not turn on and brightness would be 0%.

I’ve solved it by adding another line of time after 00:00:00 with all the setting i required. Works like a dream now.

Sunset and Sunrise seems to be working too. It afternoon now and am trying to trigger off the motion, but it wont trigger the lights. So, it look good.

I’ve notice another issue, my motion sensor or HA doesn’t reset the timer if i move around the room. I have a feeling it more HA side. As i will trigger the motion sensor, i made a dashboard for the motion sensor and say “detected” but the light just goes off when its timer reaches.

Would you know of a code to add?

Am doing a research in the forums for reset motion timer, but it hard for me to read their code as they add addition parameter which i don’t understand yet.

Here my latest code.

- id: '1610479739773'
  alias: Motion activated with Sun and increase/reduce brightness
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.hue_lightstrip_plus_2_2
      state: 'off'
  action:
  - choose:
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '22:59:00'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        entity_id: light.hue_lightstrip_plus_2_2
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: '23:00:00'
      sequence:
      - service: light.turn_on
        data:
          brightness: 32
        entity_id: light.hue_lightstrip_plus_2_2
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: 00:00:00
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 32
        entity_id: light.hue_lightstrip_plus_2_2
    default: []
  - delay: 00:00:02
  - service: light.turn_off
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2

Thanks

Hi Rockdog,

Sounds like a good setup. Do you think issue may be due to Ikea Tradfri repeaters not relaying back to HA after reboot? Maybe try another brand repeater?

Or maybe its Deconz itself?

Is your setup like this Kasa wall switches > Ikea Tradfri > Deconz > HA? In terms of communication.

So moving off to HA, not a bad idea. But if no one available to help then maybe break down your automation. Move one at a time. Pick a easy automation and re-create it in HA, Show and tell us what you want it to do for a single automate. Am sure then someone can help you create the code. Then learn and understand the code in HA. Keep going till everything has moved over. Remember test, test and test and modify till your happy. Take it in a small steps.

I love Philips Hue, i recently re-did my house and installed loads of philips lights from “Being Ceiling”, “Still Ceiling”, “Cher Pendant”, “Colour Spot Light” etc. I dont have a issue as they are creating their own mash and i believe my other zigbee devices are hopping on the mash. My ring alarm is using z-wave and comes with it own z-wave repeater and my Samsung motion sensor probably hopping on too.

Glad to hear that part is working.

As far as turning off the light, you’re right the delay that you have in there wont pay any attention to what’s going on with the motion sensor. While its probably possible to work this into the existing automation using wait and some timedelta stuff, the far easier solution would be to make a second automation that handles turning off the light. Something like:

alias: Light Turn Off
trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'off'
    for: "00:02:00"
condition:
  - condition: state
    entity_id: light.hue_lightstrip_plus_2_2
    state: 'on'
action:
  - service: light.turn_off
    entity_id: light.hue_lightstrip_plus_2_2

You can add any other conditions you need, I just put one in to fire only if the light was already on.

Thanks Brain,

That works as required for the motion, however if the light is manually turn on the motion is detected, it turns the light off.

Give this a try. Never tried this but in theory it should work. :smile:

alias: Motion activated with Sun and increase/reduce brightness
description: test
trigger:
- platform: state
  entity_id: binary_sensor.aeon_multipurpose_sensor_motion
  to: 'on'
condition:
- condition: and
  conditions:
  - condition: state
    entity_id: light.hue_lightstrip_plus_2_2
    state: 'off'
action:
- choose:
  - conditions:
    - condition: sun
      after: sunset
    - condition: time
      before: '22:59:00'
    sequence:
    - service: light.turn_on
      data:
        brightness_pct: 100
      entity_id: light.hue_lightstrip_plus_2_2
  - conditions:
    - condition: sun
      before: sunrise
    - condition: time
      after: '23:00:00'
    sequence:
    - service: light.turn_on
      data:
        brightness: 32
      entity_id: light.hue_lightstrip_plus_2_2
  - conditions:
    - condition: sun
      before: sunrise
    - condition: time
      after: 00:00:00
    sequence:
    - service: light.turn_on
      data:
        brightness_pct: 32
      entity_id: light.hue_lightstrip_plus_2_2
  default: []
- wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.aeon_multipurpose_sensor_motion
      from: 'on'
      to: 'off'
      for: "00:10:00"
- service: light.turn_off
  data: {}
  entity_id: light.hue_lightstrip_plus_2_2

Thank you, i was on the same wave length as you, i was just playing with the code and add triggered.
I think it worked.

- id: '1610479739773'
  alias: Motion activated with Sun and increase/reduce brightness
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.aeon_multipurpose_sensor_motion
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.hue_lightstrip_plus_2_2
      state: 'off'
  action:
  - choose:
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '22:59:00'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        entity_id: light.hue_lightstrip_plus_2_2
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: '23:00:00'
      sequence:
      - service: light.turn_on
        data:
          brightness: 32
        entity_id: light.hue_lightstrip_plus_2_2
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: 00:00:00
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 32
        entity_id: light.hue_lightstrip_plus_2_2
    default: []
  - service: light.turn_on
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.aeon_multipurpose_sensor_motion
      from: 'on'
      to: 'off'
      for: 00:00:30
  - service: light.turn_off
    data: {}
    entity_id: light.hue_lightstrip_plus_2_2
  mode: single

It didn’t need delay, but wait for trigger.

You don’t know how happy i am now. :slight_smile: :crazy_face:

I was going to abandon HA to go back to Samsung Smartthing. I guest i learnt my lesson being patient is key.

I did install Node Red, but it messed up my HA and was running slow. Luckily i could still access the interface and delete it.

Now am going to test this on my landing light.

I was testing this on my Philips light strips behind my TV which these lights don’t have a physical switch, which i though could also be the problem, after reading someone else post with manual override with a switch.

1 Like

I’ve also timed my motion sensor and it adds on another 12 second before the actual light timer kicks in.

Motion sensor 12s + light timer 30s = 42s before it turns off.

Which am fine with, as i can knock off 12s off light if needed to compensate.

Sorry for the late reply.

I was testing the automation, i found a issue with the condition that wont work at nights.

Please see below, my final version, tested for last few days and happy with the automation.

- id: '1610498201846'
  alias: Motion activated with Sun and increase/reduce brightness - Landing
  description: ''
  trigger:
  - platform: state
    to: 'on'
    entity_id: binary_sensor.landing_motion_sensor_motion
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: light.upstairs_hallway
      state: 'off'
  - condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
  action:
  - choose:
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '22:29:59'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 100
        entity_id: light.upstairs_hallway
    - conditions:
      - condition: sun
        after: sunset
      - condition: time
        after: '22:30:00'
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 32
        entity_id: light.upstairs_hallway
    - conditions:
      - condition: sun
        before: sunrise
      - condition: time
        after: 00:00:00
      sequence:
      - service: light.turn_on
        data:
          brightness_pct: 32
        entity_id: light.upstairs_hallway
    default: []
  - service: light.turn_on
    data: {}
    entity_id: light.upstairs_hallway
  - wait_for_trigger:
    - platform: state
      from: 'on'
      to: 'off'
      for: 00:00:30
      entity_id: binary_sensor.landing_motion_sensor_motion
  - service: light.turn_off
    data: {}
    entity_id: light.upstairs_hallway
  mode: single

Anyone asking what does this automation do then:

Motion activates select light or group (if you created a group), only after sunset and stop at sunrise. Keeps the brightness at 100% till “22:29:59”, then change the brightness to 32% at “22:30:00” till sunrise. Also there’s another condition to change the brightness after “00:00:00” with the brightness of 32% to keep it working throughout the night. Without this condition after “00:00:00” it doesn’t work, probably cause its a another day or something. Bonus, bypass is also added, if you switch the light on via the app or physically from a switch then the motion sensor wont activate the timer and switch off.

I may create a another code with lux once i received my Xiaomi Aqara.

Thanks