Automation/Blueprint for a PIR + MMW sensor (combined hybrid sensor for occupancy/presence detection)

So… I have PIR sensors and MMW sensors in few places. I want to create some automations based on actual occupancy of the room using both the PIR and MMW (using both of their pros and cons).

First the pros and cons for each sensor:

  • PIR: Quick to detect motion when entering a room and VERY reliable. but often turns off when you idle in a room. Very unlikely to have a false positive (specially with a good PIR).
  • MMW: slightly slow to initially detect when entering. many many false positives. I have several MMW sensors that even gets stuck on ON state for days. but extremely good at detecting when idling or sitting around.

The definition of occupancy for me is:

  • Occupancy only activates if PIR sensor triggers [activates immediately].
  • Occupancy only deactivates either if
    • PIR remains off for set amount of time (say 30 mins [configurable]). [this to prevent if MMW sensor is triggering false positives]
    • or if MMW had started triggering while the PIR was active and then eventually MMW has turned off (+ some set amount of time for extra leeway like 1 min [configurable]).
  • Allow for an input boolean to turn this off/on (like: kitchen_lights_automation: on/off)

Is some logic like this achievable using HA ? Is there anyone that can help me with some automation or a blueprint that allows for this or atleast point me into some directions?

lastly, is there an easy way to use this logic to create a proxy sensor that defines the “Occupancy” for a room (like kitchen_occupancy, study_occupancy etc)? I can then use this proxy sensor into much simpler automations (or even existing blueprints) to control lights or turn on/off AC etc. If this is possible its ideal, so i can define what Occupancy is in one place for each room, and dont have to duplicate that for every automation i want to trigger. Thoughts/ideas?

I can actually achieve most of this with nodered. Ive actually been using a nodered flow to generate the above logic to create proxy sensor into home assistant, and i can then use the sensor for automations. But nodered seems to messy for this, and due to not being able to properly reuse the logic, it seems like a big duplicated mess. and i notice sometimes the nodered triggers are not 100% reliable (the proxy sensor once in a while doesnt update although it absolutely should have). im just hoping that there is a better solution from within HA. I cant possibly be the only one who wants to use occupancy detection based on both PIR and MMW.

Any help is appreciated :slight_smile:

Bump! Is there anyone that has any ideas or thoughts on this?

You would need to use a Trigger-based Template binary sensor to get the behavior you have described. Template Blueprints are currently (2025.01) limited to state-based sensor types.

As I understand it, there was recently a PR submitted that might address the issue, allowing Blueprints of trigger-based sensors… but, it tends to take at least a few months for those kind of things to get from submission to being merged.

Thank you for the suggestion! I wasn’t familiar with trigger based templates and was eyeopening. Although itll take me a while to fully grasp them, i can already see quite few ways to use them to make some of my logic/states lot more transparent, clean and reliable.

That said, i did have a good look through the docs, but i cant exactly see how trigger based templates can be used for this sort of hybrid sensor. Because the hybrid sensor i want need to 1: turn ON with the PIR sensor; 2: turn off with the mmw sensor; 3: Turn off IF PIR sensor doesnt turn on for certain amount of time (to prevent mmw doing false positives);

I dont see how i can get to that level of complexity with template sensors. Is there anyway someone can give me some potentially complex example(s) that would give me some hints on how i can build this? All the examples in the docs are fairly basic.

This is a quick and dirty example…

template:
  - trigger:
      - trigger: state
        to: "on"
        entity_id: binary_sensor.PIR
        id: "on"
      - trigger: state
        to: "off"
        entity_id: binary_sensor.PIR
        for: "00:30:00"
        id: "off"
      - trigger: state
        to: "off"
        entity_id: binary_sensor.MMW
        for: "00:01:00"
        id: "off"
    condition:
      - condition: state
        state: "on"
        entity_id: input_boolean.kitchen_lights_automation        
      - or:
          - condition: trigger
            id: "on"
          - alias: Conditions to switch to Clear
            and:
            - condition: state
              state: "off"
              entity_id: binary_sensor.PIR
              for: "00:05:00"
            - or:
                - alias: MMW is "off"
                  condition: state
                  state: "off"
                  entity_id: binary_sensor.MMW
                - alias: MMW has been stuck "on" for a given time
                  condition: state
                  state: "on"
                  entity_id: binary_sensor.MMW
                  for: "05:00:00"
    binary_sensor:
      - name: Kitchen Lights Occupancy   
        state:  "{{ trigger.id | default('off', true) }}"

You should probably spend some time figuring out why the MMW gets “stuck” or reports incorrect data. As long as your MMW sensor is unreliable, any sensor or automation that relies on it will also be unreliable and it will lead to frustration.