Use multiple motion sensors to detect direction

Hi All,

Im fairly new to HA and was hoping to find out if an idea I had is possible and if so, how i would go about making it happen.

I recently bought a couple ESP8266s and hc-sr501s. My thought is that when sensor 1 is triggered first followed by sensor 2 second (Im walking to the bedroom), turn lights on in the bedroom. Then if sensor 2 is triggered first followed by sensor 1 second (leaving the bedroom), turn the bedroom lights off.

It sounds simple enough, but I havent found any examples codes of people using motion sensors in this way. Is this possible to do? If so, can I get help implementing this into my automations?

Thanks everyone, Ive really enjoyed learning and customizing my HA to work for my household.

something like this is not simple. I’d wager that this would need to be a python script or component as coding this in yaml/jinja would be a PITA.

how many sensors? If only 2 it’s feasible and relatively straight forward.
basically you’d need 2 automations, one for A-> B and one for B->A
(you could probably do in one but it will be messy and hard to troubleshoot)

A-> B
you trigger on sensor 1 = on and and you check when sensor 2 is off,
If so then you’re going A-> B, other wise you’re going the other direction and this should have been picked up by the other automation

For B->A you copy your automation and you swap sensors 1 and 2 everywhere

- alias: Going to Zone 1
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_1
      to: 'on'
  condition:
    - condition: state
      entity_id: binary_sensor.pir_2
      state: 'off'
  action:
  - service: light.turn_on
    data:
      entity_id: light.zone1
      brightness: 255


- alias: Going out of Zone 1
  trigger:
    - platform: state
      entity_id: binary_sensor.pir_2
      to: 'on'
  condition:
    - condition: state
      entity_id: binary_sensor.pir_1
      state: 'off'
  action:
  - service: light.turn_off
    data:
      entity_id: light.zone2

@petro When did you stop accepting challenges? :stuck_out_tongue:

Now personally I think this is a bit overkill as you need 2 sensors for that logic to work, and I’ve done it a different way: I have a motion sensor in the room and when motion is detected, light turns on, when motion is not detected, light dims down and then switches off after 2 min of no motion

Lol, I mean, as soon as its more than 2 it’s a pita. I was thinking about the way I would handle it. I’d treat each motion sensor as a node with a list of ‘connected nodes’. And going down that path would be a bitch, but it would work 100% of the time with multiple people.

I’m probably over complicating it…

Cool thanks @lolouk44! I have 2 sensors setup atm and did basically what you described. It seems to be working but of course will be tweaking things overtime such as proper delay on the hc-sr501’s and various conditions.
I thought about the timed motion sensor but I don’t want to wave my hand at the sensor anytime I sit still for more than a few minutes. Though my current setup also has it’s problems that I need to work out.
Below is my automation for this setup in case anyone is curious or has any more suggestions. One of the downfalls that I need to figure out is, if I’m in the living room and my S.O. goes downstairs, it will turn the lights off on me. I have a condition for not to if the TV is on, but I don’t watch a lot of TV so it won’t work every time. Still trying to think of something more reliable.

- alias: 'Motion Living Room On'
  trigger:
    - platform: state
      entity_id: binary_sensor.living_room_pir
      to: 'on'
  condition:
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.stairs_pir
        state: 'on'
  action:
    service: scene.turn_on
    entity_id: scene.lights_on

- alias: 'Motion Living Room Off'
  trigger:
    - platform: state
      entity_id: binary_sensor.stairs_pir
      to: 'on'
  condition:
    - condition: and
      conditions:
        - condition: state
          entity_id: binary_sensor.living_room_pir
          state: 'off'
        - condition: state
          entity_id: media_player.samsung_tv
          state: 'off'
        - condition: state
          entity_id: group.caspiller
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.lights_off