Avoid sensor states overlapping

Hi all.
I have installed Aqara motion sensors on every room in my home. All those sensors are hardware hacked, so they restart motion detection after 5 seconds with the proper configuration.
I also have set a helper with many options in a dropdown wich works as a “room presence”, with an automation choosing the correct option when every sensor detects movemement. So, if i walk into the kitchen, sensor detects motion - helper goes to “kitchen”… And so every time i walk into a room, or even the hallway. I’m using this to start (or end) other automations, light actions, etc…
Generally speaking, is working as expected. But, in some cases what i’m getting is not the desired result. Example: when i left my living room to go to the bathroom, i have to walk through the hallway. What i must see on the HA states and history of the “presence” helper has to be “livingroom - hallway - bathroom”… But what i see is “livingroom - hallway - livingroom - hallway - bathroom - hallway”. This is (very often) ruinning some other automation and light triggers.
On the automation used to “change” the “status” of the presence helper i’ve already set a motion detection trigger for minimum time by 5 secs. (The time set for the sensors to reset), but It seems that they’re overlapping states or something similar.
So, any help with this, please ??
Thanks.

PS: Apologies for the large text.

Post the automation responsible for setting the helper’s state.

This:

alias: Room presence
description: Detector de presencia
trigger:
  - type: motion
    platform: device
    device_id: 23673fd08c5c1a195d5f77214d3940a1
    entity_id: binary_sensor.sensor_bano
    domain: binary_sensor
    id: Baño
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: 23510008d6f2a0ff6ad6b24020c8757f
    entity_id: binary_sensor.sensor_vestidor
    domain: binary_sensor
    id: Vestidor
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: 88661cdd4c976cabc1a665477d56bc24
    entity_id: binary_sensor.sensor_pasillo
    domain: binary_sensor
    id: Pasillo
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: e9fba6c56d8013e7776678afde58274b
    entity_id: binary_sensor.sensor_salon_a
    domain: binary_sensor
    id: SalonA
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: ff5738180ac338fccfbdcfae300c6129
    entity_id: binary_sensor.sensor_cocina
    domain: binary_sensor
    id: Cocina
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: 7ba11ef0cf5b6fe3b6954a729a01c530
    entity_id: binary_sensor.sensor_dormitorio
    domain: binary_sensor
    id: Dormitorio
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - type: motion
    platform: device
    device_id: 7dd8b397df261e74271b82a68736b342
    entity_id: binary_sensor.sensor_habitacion_f
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: HabF
  - type: motion
    platform: device
    device_id: 0b08e1535bf8f275d8708d0ac4a9e060
    entity_id: binary_sensor.sensor_salon_b
    domain: binary_sensor
    id: salonB
    for:
      hours: 0
      minutes: 0
      seconds: 5
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Baño
        sequence:
          - service: input_select.select_option
            data:
              option: baño
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: Cocina
        sequence:
          - service: input_select.select_option
            data:
              option: cocina
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: Pasillo
        sequence:
          - service: input_select.select_option
            data:
              option: pasillo
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: SalonA
        sequence:
          - service: input_select.select_option
            data:
              option: salon
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: Vestidor
        sequence:
          - service: input_select.select_option
            data:
              option: vestidor
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: Dormitorio
        sequence:
          - service: input_select.select_option
            data:
              option: dormitorio
            target:
              entity_id: input_select.room_presence
      - conditions:
          - condition: trigger
            id: HabF
        sequence:
          - service: input_select.select_option
            data:
              option: habf
            target:
              entity_id: input_select.room_presence
    default: []
mode: restart

Although this is unrelated to the issue you reported, I noticed your automation has triggers for binary_sensor.sensor_salon_a (SalonA) and binary_sensor.sensor_salon_b (salonB) but choose only handles SalonA not salonB.

What is the Input Select’s option for salonB?

If you name the rooms in input_select.room_presence to this:

bano
vestidor
pasillo
salona
cocina
dormitorio
habitacionf
salonb

then you can use this simpler automation:

alias: Room presence
description: Detector de presencia
trigger:
  - platform: state
    entity_id:
      - binary_sensor.sensor_bano
      - binary_sensor.sensor_vestidor
      - binary_sensor.sensor_pasillo
      - binary_sensor.sensor_salon_a
      - binary_sensor.sensor_cocina
      - binary_sensor.sensor_dormitorio
      - binary_sensor.sensor_habitacion_f
      - binary_sensor.sensor_salon_b
    from: 'off'
    to: 'on'
    for:
      seconds: 5
condition: []
action:
  - variables:
      room: "{{ trigger.to_state.object_id[7:].replace('_', '') }}"
  - condition: template
    value_template: "{{ room in state_attr('input_select.room_presence', 'options') }}"
  - service: input_select.select_option
    data:
      option: '{{ room }}'
    target:
      entity_id: input_select.room_presence
mode: queued

There’s no input for salonb. I don’t need that for now, still thinking options for that sensor.
I’m not at home right now, will test your suggestion as soon as I can.
I’ll tell you the results.
Thanks in advance !

Hi.
Tested your suggestion: Same results as my original setup.

Then it’s not an issue with the automation (you have tested two different versions) but the behavior of the motion detectors.

This may be a silly question, but have you tried repositioning the motion sensors - particularly the one in the hallway?

So, I assume there’s nothing I can do.

Not tried, but thought about it many times for sure. And ended thinking that it wouldn’t make a difference: The bath is on the half of the hallway and the distance between doors are short. The hallway sensor right now is on the most far spot I have.

You can try masking the motion sensor to constrain its field of view.

For example, I have a wall-mounted motion detector in a bathroom. Its location allows it to activate the moment someone crosses the threshold of the bathroom door. For my purposes, that’s too soon. There are times when you need to enter the bathroom to close the window blind before the motion detector turns on the light. I used tape, to cover part of the IR sensor, to narrow its field of view.

Perhaps it’s possible for you to do something similar to prevent the motion sensor in one room (livingroom) from detecting motion in another (hallway) and/or try relocating it.

I know that, and i have some of my sensors “half blinded”, but i think there’s no “ghost / false” positive here. Before positioning the sensors I’ve checked and tried in search of possible issues and so.
It may be something related to the way the information is passed or managed by HA when sensors “match” detecting motion or so.
And of course, maybe the way I realized this “room presence” can work accurately I’ve imagined its not the best solution.
I’ll keep trying and report if I can find the way.

Thanks so much again for your responses and help.
Much appreciated.

Look at each sensor’s history when you move from livingroom - hallway - bathroom.

That appears to be correct to me:
Motion is set to OFF after 5 seconds is set to ON (just as I have set it)

Or at least, that’s is what report the HA logs (look for the times in the picture)

That’s only the hallway sensor, but for the others it’s the same behavior. Of course, there’s some coincidence sometimes when two sensors are detecting movement at the same moment for 1 or 2 seconds (That’s why I’m using the 5 seconds state’s to ON ON the automation).

I can’t form any conclusions based on the single history chart you have presented.

I recommend you examine the automation’s traces that are recorded when you move from livingroom - hallway - bathroom.

I wonder if it’s a good idea to use the state of the motion sensors directly - the fact that they turn off may be making things unnecessarily complicated.

I have a sensor last_movement which is updated with the room name every time motion is detected and retains its value until the next movement. (This allows me to give Alexa commands like “lights off” without specifying the room - works well as long as there’s only one person in the house.) Something like that might simplify things.

@Stiltjack what you say is very similar to what I have done with my helper.

@123 checked the traces and seems correct: It chooses the right option every time.

I think I have it working as expected for now:
I’ve changed the detection states from 5 to 6 seconds in order to trigger the automation. Also changed the mode from “restart” to “single”.
I need to check the behaviour the next days but the tests at this moment is working OK.

Thanks for your help !! :+1:t2: