Using a single sensor to turn lights on and off

Hi all,

I’m quite new to the whole Home Assistant thing, so i’m going to try to explain my objective in accordance with the topic instructing how to ask the best questions and I would be very happy if anyone knows how to work this.

So this is the set up. I have a Phillips Hue motion sensor hanging in the dooropening to my bedroom. I have a light hanging in the middle of my bedroom. I am using the sensor at this moment (using HA, while previously using Phillips’ own automation options) to turn the light on and after 5 min to turn the light af. This between 07.00 and 23.00. I’m currently using ‘occupancy’ detection and not motion because for some reason that works best as an actual motion sensor.

So this is what i am trying to do:
Trigger: Sensor detects motion
Condition: light off
Action: turn light on

Trigger: Sensor detects motion
Condition: light on
Action: turn light off.

I have no problem getting the first one to work, but for some reaons the sensor doesnt trigger for the second one for some reason. I have taken a look at this with a neighbour far better at automation and HA but we couldn’t figure it out ourselves. It seems to have to do with the sort of restart time the sensor needs to clean out the trigger signal.

So here’s my question:

  • Can anyone figure out a solution to turn the lights both on and off based on the single motion sensor
  • Does anyone know what the time lag that is required to have the sensor eligible to have a new trigger?

The sensor will have a predefined amount of time that it stays ‘on’ after it’s triggered before it turns ‘off’ after no motion. It’s usually a few seconds, on some sensors you can reconfigure this delay but I’ve never seen a need for it. Some motion sensors will then drop to ‘off’ and then immediately turn ‘on’ again as motion continues.

Taking that into account actually makes automations easier because one can be confident in most situations that once the motion sensor is continuous ‘off’ for a short time that all requirements for the light have expired.

As such, the automation is generally simple

Trigger - when the motion sensor is on, or has been off for 30 seconds

Action - turn the light to the state of the motion sensor

Thus:

automation:
  alias: quick example of motion activated lights
  trigger:
    - platform: state 
      entity_id: binary_sensor.1
      to: 'on' 
    - platform: state 
      entity_id: binary_sensor.1
      to: off
      for:
        seconds: 30 
  action:
    service: "light.turn_{{ trigger.to_state.state }}"
    entity_id: light.1

You can make it as complicated as you want after that, for example all of my lights dim to half their current brightness for 10 seconds so that if someone is in the room but just hasn’t moved for a while, they can wave a hand to activate the motion sensor again.

Of course one can condition for times and turn on the lights at different brightness, colour etc depending on when it is triggered or by whom.

The world is your lobster once you have understood the logic.

Hope this helps.

Thank you very much for your help on the matter mf_social! I do have one ‘concern’, i am thinking this would only work when the sensor is located within the bedroom, so it can continiously monitor me being around or not. The sensor however is located at the door of the bedroom. Would that change your advice mf_social?

Depends. Tbh I find motion sensors in bedrooms to not be very effective as you can never get the right balance, it’s either not sensitive enough and the light turns off all the time, or it’s too sensitive and every time you roll over in bed the lights come on. But that’s me…

The question really is how do you see the logic working with this sensor? Is it supposed to assume that you’re in the room the first time it’s triggered and assume that you’ve left the next time? If so, how can you mitigate it going out of sync with your movements? What if you’re in there and someone else comes in? If you’re happy with all of that, how will it know you’ve gone to bed and switch off the lights?

As I say, it’s the logic behind the automations that counts, then coding it is easy.

Yes that would precisely be the logic behind the automation.

I enter the room, it can assume i stay there untill it again sees me leave. I dont plan on any other presence in the house that’s going to the bedroom so that should be an issue for the forseeable future.

When the light needs to go out when i go to sleep, that is part of a routine thats GA supported (and influences other stuff as well)

In that case it’s even easier.

automation:
  alias: quick example of motion activated lights
  trigger:
    platform: state 
    entity_id: binary_sensor.1
    to: 'on' 
  action:
    service: light.toggle
    entity_id: light.1

Also means that if it does go out of sync for some reason, you just have to reactivate the sensor to bring it back in to sync with where you are.

And this would also turn the light off when the sensor is triggered again?

‘Toggle’ does exactly ‘what it says on the tin’.

1 Like