Preventing false positives on PIR

I’ve got a load of Digoo 433MHz PIR sensors that work really well - and I’ve made some of my own plug-in multisensors (light/temp/humidity/pressure/microwave/pir).

Problem is, the PIR I used is prone to the occasional false positive (once or twice a day). I’ve been looking round and there’s a number of possible solutions (eg. combine the microwave with PIR to activate) - but I wanted to see if it were possible to make the PIR only determine if it were on with a few activation’s in a fixed window.

I managed to figure it out,

globals:
  - id: pir_trigger_count
    type: int
    restore_value: no
    initial_value: '0'

binary_sensor:
  - platform: template
    name: "Landing PIR 1"
    device_class: motion
    id: landing_pir_1

  - platform: gpio
    pin: D3
    device_class: motion    
    id: landing_pir_sensor_trigger
    internal: true
    on_multi_click:
    - timing:
      - ON for 0.1s to 5s
      then:
       - lambda: |-
          id(pir_trigger_count) += 1;
          if (id(pir_trigger_count) >= 2) {
            id(landing_pir_1).publish_state(true);
          }             
    - timing:
      - OFF for at least 3s
      then:
       - lambda: |-
          id(pir_trigger_count) = 0;
          id(landing_pir_1).publish_state(false);

So the code above says that if there are 2 PIR activation’s within a 3 second window - then that qualifies as it being a real activation.

I’ll be able to fine tune it - but this should be enough to stop a single PIR activation from triggering.

I thought I’d just share this with others.

5 Likes

What’s false triggering it, the sun?

I have a few outdoor PIRs that have pulse count settings of 1 to 5. Even on 5 they false trigger on partly cloudy days. Or bright sunny but windy days (moving shadows). I tried a combined PIR and microwave sensor. It was slightly better but it too would false trigger. Apparently warm air currents can false trigger the microwave sensor. Still searching for a good outdoor movement sensor that can cover an area (i.e. not a break-beam sensor).

1 Like

Have you looked at the various ultrasonic sensors, or the ‘radar’ ones or, but at a higher cost, LIDAR units? It depends in part if you mean off the shelf or a custom job built by yourself.
Some cheap cameras have motion detection, but … I set one up to capture a visiting badger and it triggered on the movement of leaves in the garden!

Microwave and radar are the same thing. I’ve tried an expensive Bosch PIR + microwave and a low cost PCB microwave radar module. The latter was hopelessly inconsistent. The Bosch was the best I’ve found so far but it does still false trigger outside (it’s meant to be an indoor sensor).

I’ll have a look for LIDAR and ultrasonic. Thanks for the suggestion.

Using this code, did it work for you to reduce the false readings? I’m currently trying to do the same thing but with node red.

Was looking into the pir sensors and someone mentioned that using resistors helped stop false readings as it reduce noise/spikes between the sensors and chip.

Maybe good to inform you about truth’s PIR sensors that help to eliminate the problems that are commonly known with these small units.

To start, the PIR unit basically consist of two critical elements: the piezo electric sensor that senses infrared energy and second electronics circruitry that enables differential detection of infrared energy changes.

The sensitivity of the sensor is such that only just microvolts of amplitude variation will be passed to the circuitry that is designed to judge “movement detection” or not. If you look at the module from pure technical point of view, it isn’t detecting movement but it detects “changes in the infrared light spectre”. Sudden changes are usually caused by persons that dissipate “heat” from body temperature which is noted when passing a sensor due to the fact that it causes a sudden change in this infrared energy in the detection area.

However, due to the technology, not only persons will be detected. In the case where sudden temperature changes (1 tot 2 degrees will do already) happen, a sensor circuitry may already give so named positives. The “intelligence” of the differential evaluation often is a prime selection criterium for the right sensor in your application. In all cases, it is advised to position the sensor such that no sudden temperature changes are to be expected such as a window or drafty position.

Another point to give serious attention is EM field sensitivity. As written above, the sensor element reports changes in the infrared spectrum with utmost small voltage variations that are in the microvolt range of which the evaluation circuitry needs to decide whether it is a detection or not. Many (cheap) PIR units are positioned close to RF radiating elements (such as wifi) or use long power / data leads that easily pickup electromagnetic energy that cause small voltage variations near the sensor element or circuitry as well. If sudden changes here are notified by the PIR unit (such as a wifi databurst), it may cause false detection. In PIR technology, often “noise” is named as cause for bad detection quality. This noise is not only caused by limited filtering of the evaluation circuit, but also EM disturbances. Actually, there are quite some aspects that influence the total behavior of a PIR unit of course. A further cause of noise is often a microcontroller, causing small switching voltages in IO lines that may already influence a PIR and last but not least the power supply noise may be of influence.

I have studied several PIR units and learned that almost none of them take care of EM filtering in an adequate manner. This is not a big problem when no EM field disturbances are to be expected. When the PIR units is meters away from the (in this case ESP) RF unit, it will work reasonably well. When this is not possible, measures have to be taken. For example, the HC-SR505 PIR unit is great due to small size, but it brings not only very simple evaluation circuitry, but lacks any kind of EM filtering. The 8-pin chip, for example, outputs the detection line via the output pin only via a 1kOhm resistor. There is no filtering whatever that eliminates EM radiation. In cicruitry that evaluates micovolt variations, this is killing.

A third thing to be aware of is light on/off switching. Since an artificial light also radiates infrared energy, the area will undergo a sudden change when light is switched. This is normal and cannot be prevented. Only very advanced PIR units with DSP / asic evaluation circuitry may “recognise” the pattern that light switching causes in the infrared field, which basically differs from a person moving. Simple circuits that we use can normally not differentiate between various types of changes in infrared energy, so don’t waste precious time in trying to eliminate that, you have to take that for granted.

EM radiation can be eliminated very well, but knowledge of RF technology is of importance. Moreover, EM filtering potentially requires additional circuitry which is contrary to the desire to keep things small.

Usually the test setup in your ESPhome isn’t ideal as the unit is subject to all kinds of sudden events that cause false triggering. Main items to keep in mind will be temp variations (just blow to the circuit to test it) and EM susceptibility. Now, what to do to have a reasonably fair PIR element:

  1. Limit temp variations

As said before, position it in a place where sudden temperature variations (within a couple of seconds) are not to be expected. Slow variations are fine as the circuit measures differential voltages in a window comparator or digital equivalent of that.

  1. Minimize EM field sensitivity:
  • position ferrites on the three wires to do a basic RF filtering
  • You may create RF shielding arount the PIR unit by encapsulating it into a metal box. Most important here is that the grounding of the metal box with the ground of the PCB is handled adequate. If done wrong, the metal case may act as antenna rather than RF block.
  • You may further filter the power supply and detect output wires with a so calles Lowpass (Pi) filter as close to the circuit as possible, consisting of at least a capacitor of a couple of Pf’s between the power supply / detection output lead and GND as well as an inductor in series with it. I would start to use 100pF and 220nH as this has a cut-off frequency of about 80MHz, which should eliminate problems enough.
  1. minimize voltage ripple

Minimize voltage / data ripple on the leads. This can be done in its simplest form by eventually adding a ~100 of 220uF electrolytic capacitor over the voltage line and GND, again as close to the circuit as possible.

If handled well, the measures of above will significantly improve the performance. I suggest to test these suggestions one by one, starting with the first. Most of times, you don’t need to implement all to have a reasonable behavior.

Good luck!

2 Likes

Hello @blessani!

Thank you for this! I was woken up constantly last night by false triggers from a PIR sensor in my home and I was happy to find this - it is exactly what I need. I have two problems though. The first is the internal: true needed to delay publishing to Home Assistant before the filtering is done; it actually prevents EspHome and Home Assistant from registering the sensors altogether. They just show up as ‘unavailable’. Removing that comment seems to thwart the filtering completely and I still get false triggers. Attached is a screenshot with two sensors having the internal: true tag:

The second issue is an error message I get in the EspHome logs while uploading. I don’t know if it is a problem or not with any functionality, but I imagine they are as they are highlighted.

Online research suggests adding a -Wcomment flag preceding the offending command line, but I can’t figure out where that goes.

Any help is appreciated.

Not sure if you have solved this since.

I have a pretty massive outdoor DIY surveillance grid made of a multitude of sensors (don’t ask, it’s mostly for fun and proof of concept more than actual use). I use industrial grade PIRs, radar sensors, AI video analysis and LIDAR. The best combination I found was a field detection system (PIR or microwave, or both) combined with line crossing LIDAR sensors enclosing the area’s ingress and egress points. When doing smart sensor fusion using all that data and its spatial and temporal relationships, the false positive rate is practically zero and it works even in very adverse weather conditions.

One thing I am considering is the new Reolink IP cameras with onboard person and vehicle detection. The only issue is that there is not yet a way to get these event triggers into Home Assistant. Though I am lead to believe their engineers are working on an API to allow this.

The new Hikvision ones have a pretty decent onboard human / vehicle detection and there’s a direct HA integration too. Not perfectly false alarm free, but at least it isn’t triggered by tree shadows and random cats / foxes / etc.

They don’t even need to be new dahua/hikvision. This is a 5yo analog dahua system. The dvr has ivs, I can set multiple rules per camera. The box will notify abandoned objects, anything left more than 30 seconds. The line trips my presence detection scan.

The dvr’s use the same api as the ip versions. I use dahua to mqtt and every rule comes through on a separate topic. Plus your typical general motion for each channel.

My exterior lights, I use general motion to slightly raise the brightness, then full brightness/flash if you cross an ivs trigger. The ivs triggers allow setting the bounding box, I rarely get false triggers. It’s usually a few raccoons following each other when I do.

This is true for almost any hikvision/dahua system these days even the entry level / analog systems.

Yes, but the difference is that the newer models run every detection through an AI model to determine what caused it, before sending a trigger out, for example, if the detected line crossing is from a human. Bounding box sizes can only go that far, especially if your view angle has lots of perspective. I have both older Hikvisions with line crossing and adjustable bounding boxes and newer ones with AI. The newer ones are much better at avoiding false positives, especially outside with moving trees and sun shadows, wildlife and such.

I’ve found a post on the web to use Ferrites on the vcc & data for pir sensor. No more false positive.

2 Likes

I know this is a bit of an old post now, and just wondering if anyone managed to refine this code, how slow does it make the sensor?

I have added some ferrite cores to the PIR as above and it definitely reduced the false positives, for a day, then they came back, somehow, and moving the power cable away from an Alexa helped (maybe)

These things are just so fickle lol

I have a LD1115H sensor setup for my kitchen area along with an AM312 sensor separate from the LD1115H as well in the same area. The LD1115 would have false triggers every so often. Not sure if it is due to the location (next to the microwave) it was placed.

My workaround to prevent the lights coming on due to false positive is to set a condition to only turn on the light if the AM312 has been triggered less than 3 minutes ago. Seems to work, but if the AM312 would fail at any time, then the lights will never turn on when triggered.

  condition:
    - condition: template # Prevent false triggers. remove if not needed
      value_template: >-
        {%- set seconds = (now() - states.binary_sensor.motion_kitchen_occupancy.last_changed).seconds -%}
        {{ seconds <= 180 }}

Thanks for that :slight_smile:

But its that kind of cool down that I moved from a battery sensor to avoid HAHA.

Its so frustrating, I’ve added ferrites, changed PSU, moved the sensor.

This thing would be great… If it didnt suck so much LOL

So the ferrites didn’t help with the issue? Thanks

Not really no. I ended up moving the PIR off to the side and that completely fixed it.

If you use

If you have a look here, Jukka has these awesome cases that are parametrised so you can expand them as you want.

I am using the " HC-SR505" module with a ESP8266, and was consistently getting a false trigger every 30-60 seconds.

Initially I’ve read on some post to use 2 separate power supplies, which is fine for a quick once-off project, which solved my problem.

BUT I wanted to get a more permanent and proper solution:

1x100uf electrolytic capacitor
1x100nf ceramic capacitor
Connect them between ground and the signal coming from the PIR.

Since then no false positives.

2 Likes