Motion Detection with trigger reset before light turn off

Hi,

I have Tuya component switches and smart bulb and I would like to implement to turn ‘on’ light when motion detection is activated with PIR sensor. Since I have implemented already the case when motion is detected and smart bulb is turned ‘on’ for specific interval and then it goes ‘off’ after specified time. This case is not good since the room is dark and light goes ‘off’ and motion needs to detect again to switch light ‘on’, but I was thinking how to implement the case that motion detect and reset counter (time) so that light never goes ‘off’. (it will goes ‘off’ when I left the room and counter comes to end (in a meanwhile motion did not detect anything and therefore the counter were not reset.).

Thanks for your help!

You would need two automations:

One to turn the light on if there is motion after a specified time:
One to turn the light off if the motion detector detects NO motion for a specified time. The below automation is an example of the second automation, where you turn off the lights after 5 minutes if the motion sensor goes to “no motion”:

- alias: Turn off Master Bathroom Light in 5 minutes With no Motion
  trigger:
    platform: state
    entity_id: sensor.master_bathroom_multisensor_burglar_128_10
    to: '0'
    for:
      minutes: 5
  condition:
    condition: state
    entity_id: light.master_bathroom_vanity_lights
    state: 'on'
  action:
    - service: light.turn_off
      entity_id: light.master_bathroom_vanity_lights
1 Like

If I understand what you’re trying to do (i.e., light goes on immediately when motion is detected, and goes off X minutes after motion is no longer detected), you should be able to implement that in one automation. Or you can do it with two separate automations as @BendedArrow suggests. It’s really a matter of preference.

If you could post your existing automation, then I could suggest how to change it to turn the light on and off based on motion. (And please make sure to follow the directions at the top of the page to properly format the code you post.)

1 Like

Hi,

Thanks for your help. Yes, I would like to implement automatic turn ‘off’ and turn ‘on’ light when motion is detected. And I did not have a chance to test solution suggested by @BendedArrow but difference is that I have tuya moton sensor that is not recognized in home assistant (I have requested change request). and if/when it’s supported I would be able to control motion detection on sensor itself. Currently I have only android application where I can set trigger for light to turn on, and then Home Assistant detect when light is turned ‘on’ and count e.g. 300 seconds before it turn ‘off’ Disadvantage of this solution is that light turn ‘off’ even though I’m still in the room. So I was thinking since sensor is all the time triggering motion, it can be used so every time motion detector send ‘1’ it should reset counter so it never turn ‘off’ light while someone is in the room.

My current Configuration looks like this:

  - alias: 'Automatic light turn off in 300 seconds'
trigger:
    entity_id: light.bathroom
    platform: state
    to: 'on'
    for:
      seconds: '300'
action:
  - condition: state 
    entity_id: light.bathroom
    state: 'on'
  - entity_id: light.bathroom
    service: homeassistant.turn_off

a different question: if I want to change this number (300) from front-end, what would I need to use and how to put it in the automation? Thanks

Something like the below is possible?

seconds: ‘{{ states (“input_number.motion_living_room_on_for”) }}’

input_number:
  motion_living_room_on_for:
    name: Motion living room
    initial: 5
    min: 0
    max: 10
    step: 1

That would be possible if HA could see this happening. But if I understand you correctly, the only thing HA can see is the light turning on. It can’t see the sensor continually turning the light on (which happens outside of HA, right?) unless the light goes off then back on, which of course, is too late.

If there is something that the motion sensor can do to directly tell HA it has detected motion (and it either continually does this, or can tell HA when it has stopped detecting motion), then it should be possible to do what you want. E.g., is there anyway for the motion sensor to send an event to HA, say via the REST API?

The state trigger does not accept a template for the for: parameter. The way you usually do this is to use a timer. If you’d like more details about how to do this, you should start a new topic, and provide a more complete description of what you’re trying to accomplish.

will do. Briefly, I want to be able to change the time, in front end, after which the light will turn off (when no motion is detected)