Xiaomi motion sensor integration

Dear all,

Please help me a little bit here.
I have I yeelight LED strip and 1 motion sensor.
This is my code:

- id: strip_motion_on
  alias: Turn on strip movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_xxx
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.yeelight_strip_xxx
    
- id: strip_motion_off
  alias: Turn off strip movement
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_xxx
    to: 'off'
    for:
      minutes: 1
  action:
    service: homeassistant.turn_off
    entity_id: light.yeelight_strip_xxx

I have 3 problems:

  1. The automation is working but I’d like to setup a timeframe when the automation should be ON. I don’t want to turn on the strip during the day.
  2. I’d like to be able to turn on the strip with a switch or with alexa (this is working) but the strip will turn off after the previous time frame
  3. The strip will turn off after more than 1 minute, even no motion is detected.

Can you please help me. I don’t have any programming knowlage. I crested this using code that I found on internet.

Thank you all.

For first question:

Use a time condition and set times or use the sun sensor - below/above horizon

Second question:
You should set a variable, e.g. input Boolean, whenever you turn it on via Alexa/switch and put that as a condition so that the turn off automation doesn’t run when you’ve manually turned on the light.

Third question:
There might not be any way around this using a xiaomi sensor. I think those sensors start sending less and less often when there’s a lot of motion to conserve battery. It might also be a bug/feature in the gateway firmware

- id: strip_motion_on
  alias: Turn on strip movement
  conditions:
    - condition: sun
      after: sunset
      after_offset: "-0:30:00"
    - condition: sun
      before: sunrise
      before_offset: "0:30:00"
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_xxx
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.yeelight_strip_xxx

To avoid the strip being turned off by the movement detector (after a minute), is a little trickier.
You could deactivate the turn off automation when someone turn on the light via a button/alexa:

   - service: automation.turn_off
     entity_id: automation.strip_motion_off

For the switch it’s quite easy, use the switch as a trigger, and turn on/off the automation when you are turning off/on the light.
For alexa, I’m not sure how you integrated it and how you can handle it.

I use input_boolean to indicate turning on the light by motion detection. Turning off the light automation will trigger only when input_boolean is in the right state.
Example in my config

1 Like

Thank you guys.
Seems very complicated :slight_smile:
I’ll take a look over Bieniu code beause I think that the answer is there :).