Logic Format for Presence Sensor FP2 Automations

I am new to HA and enjoying the awesome things it can do, but I am struggling to figure out the right way to setup presence logic for zones. I have the Aqara FP2. Here’s the simple task I am trying to accomplish. Let’s say a room has 3 zones, “A”, “B”, and “C”. If presence is detected in “A”, light “1” should be on. If presence is detected in “B”, lights “1”, “2” and “3” should be on. Nothing for C.

Where this is failing me is when walking from “B” to “A”, all 3 lights turn off because that split second of transit time it appears HA sees both “B” and “A” as unoccupied before “A” is recognized as presence.

Is there a clever way to use HA and presence sensors when one light spans multiple zones? I can’t figure out the right way to catch all the conditions to get consistent results without the lights turning off for 1 second and turning back on when you cross a zone boundary. It’s rather inconsistent. Sometimes, it does work without blinking the lights.

My current setup:
Trigger: When Zone A presence becomes occupied:
Action: Turn on Light 1.

Trigger: When Zone B presence becomes occupied:
Action: Turn on Light 1, Turn on Light 2, Turn on Light 3.

Trigger: When Zone A presence becomes not occupied
Condition: Presence not detected in B.
Action: Turn off Light 1.

Trigger: When Zone B presence becomes not occupied.
Action: Turn off light 2, turn off light 3.

Trigger: When Zone B presence becomes not occupied.
Condition: Presence not detected in A.
Action: Turn off Light 1.

Results: Transiting between B and A yields inconsistent results. A to B doesn’t seem to misfire. Light 1 always stays on transiting from A to B.

Other questions:
Is there a delay parameter somewhere to wait 2 seconds, then check the conditions and execute?
Can someone explain how the duration field is intended to be used?
What is the difference between between setting the trigger as device–>“Presence detected in zone” and setting the trigger as state–>“zone change from clear to detected”? Is there any reason why you should use devices or states for triggers? I tried both and they work identically as far as I can tell.

Hi, Could you share your automation yaml code ?

@Anaconda

Hello, I installed Studio code server and was able to find this. Sorry for the delay but I am not getting email notifications. I typed some english labels in the code for you. Obviously, the yaml doesn’t contain the labels/comments.

  • id: ‘1697438840828’
    alias: Hall Occupied
    description: ‘If dining or hall zone detects presence, turn on dining light’
    trigger:
    • type: occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: dcc86dce53240a45b6c540d6326b7f5b (FP2 Hall Zone)
      domain: binary_sensor
    • type: occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: 43d3b35cf4d8659789d5f66cd6ef16d3 (FP2 Dining Zone)
      domain: binary_sensor
      condition:
      action:
    • type: turn_on
      device_id: d9a453bb76033e7077f8c0198851166c (Ecobee Switch+)
      entity_id: 4d3266fa2c1fa460a48fb06dc4705820 (Dining Light)
      domain: switch
      mode: single
  • id: ‘1697438750155’
    alias: Dining Room - On from Hall
    description: ‘if hall detects presence, turn on dining light. This seems to be a duplicate of above’
    trigger:
    • type: occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: dcc86dce53240a45b6c540d6326b7f5b (FP2 Hall Zone)
      domain: binary_sensor
      condition:
      action:
    • type: turn_on
      device_id: d9a453bb76033e7077f8c0198851166c (Ecobee Switch+)
      entity_id: 4d3266fa2c1fa460a48fb06dc4705820 (Dining Light)
      domain: switch
      mode: single
  • id: ‘1697439114140’
    alias:Hall Off
    description: '(If hall or dining becomes not occupied), AND (dining room is clear AND hall is clear), then turn off dining room ’
    trigger:
    • type: not_occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: dcc86dce53240a45b6c540d6326b7f5b (FP2 Hall Zone)
      domain: binary_sensor
    • type: not_occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: 43d3b35cf4d8659789d5f66cd6ef16d3 (FP2 Dining Zone)
      domain: binary_sensor
      condition:
    • condition: and
      conditions:
      • condition: state
        entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_5 (Dining zone is clear)
        state: ‘off’
        for:
        hours: 0
        minutes: 0
        seconds: 0
      • condition: state
        entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_4 (Hall Zone is clear)
        state: ‘off’
        for:
        hours: 0
        minutes: 0
        seconds: 0
        action:
    • type: turn_off
      device_id: d9a453bb76033e7077f8c0198851166c (Ecobee Switch+)
      entity_id: 4d3266fa2c1fa460a48fb06dc4705820 (Dining Light)
      domain: switch
      mode: single
  • id: ‘1697489250139’
    alias: Living Room - On
    description: ‘When presence detected in living room, turn on light’
    trigger:
    • platform: state
      entity_id:
      • binary_sensor.presence_sensor_fp2_a21c_presence_sensor_6 (Living Room Zone)
        from: ‘off’
        to: ‘on’
        condition:
        action:
    • type: turn_on
      device_id: 194bbfaaa9e7f2750a87c91c3cbded9c (kasa plug)
      entity_id: dd31d65a58ab3edd3d9ed9010d4f39dd (living room light)
      domain: switch
      mode: single
  • id: ‘1697489931561’
  • id: ‘1697439371240’
    alias: Living Room - Off
    description: '(If living room became not occupied or couch became not occupied), AND (living room is clear and couch is clear and piano is clear), then turn off living room light ’
    trigger:
    • type: not_occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: b6be571a9d32cd133906ffae688b6906 (FP2 Living Room Zone)
      domain: binary_sensor
    • type: not_occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: ac0ba721dacb6eb29c8d618f43653736 (FP2 Couch Zone)
      domain: binary_sensor
      condition:
    • condition: state
      entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_2 (FP2 Couch Zone)
      state: ‘off’
    • condition: state
      entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_6 (FP2 Living Room Zone)
      state: ‘off’
    • condition: state
      entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_3 (FP2 Piano Zone)
      state: ‘off’
      action:
    • type: turn_off
      device_id: 194bbfaaa9e7f2750a87c91c3cbded9c (Kasa Plug)
      entity_id: dd31d65a58ab3edd3d9ed9010d4f39dd (Living Room Light)
      domain: switch
      mode: single
  • id: ‘1697432799497’
    alias: Piano Occupied
    description: ‘turn on piano lights if piano zone becomes occupied’
    trigger:
    • type: occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: 5987d6bdbd2328ca93f6a0bde1d6d141 (Presence-Piano Zone)
      domain: binary_sensor
      condition:
      action:
    • type: turn_on
      device_id: 59408bbe1c95ae584eacdc707eeea14e (Kasa dual plug)
      entity_id: 3e280d88df50afbcce1782d185a90776 (Piano Light)
      domain: switch
    • type: turn_on
      device_id: 59408bbe1c95ae584eacdc707eeea14e (Kasa dual plug)
      entity_id: fd8a2592b80a90082258cfb820d7b0eb (Music Light)
      domain: switch
    • type: turn_on
      device_id: 194bbfaaa9e7f2750a87c91c3cbded9c (Kasa plug)
      entity_id: dd31d65a58ab3edd3d9ed9010d4f39dd (Livingroom Light)
      domain: switch
      mode: single
  • id: ‘1697433293223’
    alias: Piano Off (Piano/Music Light)
    description: Turn off piano lights if piano zone becomes unoccupied’’
    trigger:
    • type: not_occupied
      platform: device
      device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
      entity_id: 5987d6bdbd2328ca93f6a0bde1d6d141 (FP2 Piano Zone)
      domain: binary_sensor
      condition:
      action:
    • type: turn_off
      device_id: 59408bbe1c95ae584eacdc707eeea14e (Kasa dual plug)
      entity_id: fd8a2592b80a90082258cfb820d7b0eb (Music Light)
      domain: switch
    • type: turn_off
      device_id: 59408bbe1c95ae584eacdc707eeea14e (Kasa dual plug)
      entity_id: 3e280d88df50afbcce1782d185a90776 (Piano Light)
      domain: switch
      mode: single

alias: Piano Off (Livingroom)
description: ‘If leaving piano zone for hall, turn off living room lights’
trigger:

  • type: not_occupied
    platform: device
    device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
    entity_id: 5987d6bdbd2328ca93f6a0bde1d6d141 (FP2 Piano Zone)
    domain: binary_sensor
    condition:
  • condition: state
    entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_6 (FP2 Living Room Zone is clear)
    state: ‘off’
    action:
  • type: turn_off
    device_id: 194bbfaaa9e7f2750a87c91c3cbded9c (kasa plug)
    entity_id: dd31d65a58ab3edd3d9ed9010d4f39dd (Living Room Light)
    domain: switch
    mode: single

So this was my first attempt at it. Here was the idea:

Dining Room light on if presence in dining room OR hall, else off.
Livingroom light on if presence in livingroom, couch, or piano, else off.
Piano light on if presence in piano, else off.

Yes, i know the couch zone is not programmed.

What I am not understanding is how do you format the living room light automations for On in multiple zones and off in other zones, without the lights clicking off and back on when you move between zones which should have continuous on? (from piano to livingroom, for example.) I am not yet understanding how to cleverly structure the automations to allow for such conditions.

I think it’s pretty clear the problem is with the “off” automations. I’m not understanding how to apply the proper logic in HA. What is need is:
Living Room → Hall - Turn off
Piano → Hall - Turn off
Piano to Living Room → Stay On
Living Room to Piano → Stay On.

However, my exit to hall automations (no presence in living room or no presence in piano) are not able to accurately capture whether the person went to the hall (off) or to another zone which should stay on (on)…and so the results are rather mixed and unpredictable. Sometimes it works, sometimes it turns off then quickly back on.

Observations:
Blinking occurs moving from Hall to Dining, but NOT from Dining to Hall.
Blinking occurs from Piano to Livingroom, but NOT from Livingroom to piano.

What is the magic format to use to stop the blinking?

I was able to isolate the issue to

alias: Piano Off (Livingroom)
description: ‘If leaving piano zone for hall, turn off living room lights’
trigger:

  • type: not_occupied
    platform: device
    device_id: 4d8b688eced3fdef256736a0883efa16 (Aqara FP2)
    entity_id: 5987d6bdbd2328ca93f6a0bde1d6d141 (FP2 Piano Zone)
    domain: binary_sensor
    condition:
  • condition: state
    entity_id: binary_sensor.presence_sensor_fp2_a21c_presence_sensor_6 (FP2 Living Room Zone is clear)
    state: ‘off’
    action:
  • type: turn_off
    device_id: 194bbfaaa9e7f2750a87c91c3cbded9c (kasa plug)
    entity_id: dd31d65a58ab3edd3d9ed9010d4f39dd (Living Room Light)
    domain: switch
    mode: single

No blinking occurs if this is disabled. However, if I exit from piano zone to hall zone, then the living room light stays on forever. So, how can this automation be fixed so it triggers if I exit from piano zone to hall zone, but it does not run if i exit from piano zone to living room zone? Or is there a completely different way the logic should be structured?

For those finding this post later, here are a few modifications I made to the programming logic to help with the issue:

  1. Set a duration on the HA occupancy “turn off” triggers to 1 or 2 seconds, so the FP2 has enough time to register zone occupancy changes before HA checks your automation conditions. This stops the blinking issue, since we give the FP2 enough time to update states in HA before HA checks your zone occupancy conditions to decide whether to keep a light on or turn it off. Then, in your conditions for each automation, just setup the conditions that are necessary for that trigger. This has resolved all the issues I had with FP2 regarding lights spanning multiple zones.

  2. Another issue I discovered was “border bouncing”. If you are at or near a boundary, but fairly stationary, the FP2 will keep flipping your presence between both border zones, which causes unexpected results if both zones have conflicting commands. To solve this, I introduced a bounce counter as a HA helper. I added a condition to my “turn off” automations to only occur if the bounce counter is < 5. Each time the “turn off” automations fired, I incremented the bounce counter by 1. End result: if you have a situation where someone is on a border causing light bouncing, after 5 state changes, the “off” automations stop triggering and lights stay on.

2A. To reset your bounce counter, set a new rule that triggers when the FP2 enter/exit zone becomes not occupied. (This means someone has exited the room.) For conditions and actions, have HA do these things when the enter/exit zone becomes not occupied:

If the bounce counter is below your threshold,

  • Reset the bounce counter.

If bounce counter is at or above your threshold (so lights are not turning off), check if both problematic bordering zones are empty.

If FALSE

  • Do nothing. We will catch this when the last person leaves.

If TRUE:

  • Reset the bounce counter to 0. (Can’t reset below 0)
  • Decrement the bounce counter to -2
  • Call both OFF automations (for the two problematic bordering zones.) This will reset your automations correctly when the last person leaves the room. Since both these OFF automations have the bounce counter in them, your -2 counter will get +2 to get to 0.

You can always do your logic a little differently. How you need to do your logic depends on where you decide to place and check your bounce counters, and of course, your specific situation might need the workflow to be adjusted.