Any good room presence detection sensors out there?

Being quite new to MQTT and HA, I am trying to get my head around the plumbing from when you’ve got you processed data from the sensor (i.e. you have the state you want to send) and how that flows to HA.
I’ve found a few examples so I give them a go. :slight_smile:

I suppose any more details about your algorythm would be handy.
Do you just have a min temp threshold? or minimum number of pixels over a certain temp?
do you do noise filtering?

The best advise I can give you is to just get started :slight_smile: No need to go full machine learning in one go. Try to read the 64 values of the sensor (like in the adafruit link above) and then go from there! If all you need is a binary “someone is in the room or not” then I had great success with an average value + threshold

I’d have to check the pi for the code, but the gist of it was like this:

  1. Make sure no one is in the room when starting the script :stuck_out_tongue:
  2. Run a calibration routine (poll the sensor a couple of times and average each pixel so you get a calibration_list of 64 values)
  3. Substract the calibration_list from the measurement, sum it up and define 2 thresholds, above threshold1 I fire a MQTT message to HA for “on” and below threshold2 I fire a MQTT message to HA for “off”
  4. Recalibrate regularly if no one is in the room

Going by temp directly was difficult, without any external temperature sensor for ambient measurements. But then I decided that all I needed was the change of temperature from a setpoint and not the absolute temperature, hence the calibration routine.

Some stuff to improve it:

  1. Use another temperature sensor and build it into the calibration routine
  2. As I’ve seen many people do, extrapolate using a cubic spline and try some CV on that - then you can go from binary to a count of people in the room and filter small heatsources
  3. Include more sensors, even from different rooms. I had great success checking the number of people in my apartment with a wifi device tracker (unifi) and tracking motion sensors in different rooms to cross check when to calibrate again
1 Like

Nice. That makes sense. I did a similar algorithm for a crude image duplicate checker once :slight_smile:

I get how for the basic (ie without the improvements) you could do all of that on a small SBC and publish the result.
But when doing the more complicated multi sensor ‘wholistic’ stuff, where would be the most appropriate place to do the computing for that?
Did you have a separate machine doing that processing and then sending collated data to HA? If so does that machine receive all the sensor data, and HA doesn’t see the sensors directly?

Was this reply for me? I don’t have any experience with the thermal sensors but Room-Assistant also doesn’t care whether you sit still or not. I also leverage motion sensors, TVs etc to make the room presence detection more sophisticated.

1 Like

Yes and no,
the ESP is sending a MQTT json with all the sensordata on a topic with a node-ID, like the mqtt discovery, so HA is getting the data as per discovery

my HA is running in a docker on an intel NUC, the room presence mash is a simple python script which I’ve dockerized on the same NUC, subscribed to the same topic(s) and sends the results via mqtt to HA

1 Like

Hi
How you connect it to HA?
Via Esphome?

No, simply with MQTT.

I have moved away from the pi already as you have guessed by mentioning ESPHome.

My current setup is a ESP32 which has a bunch of other sensors as well, but the room presence part is still done by the AMG8833.
For now I am calculating the standard deviation of the 8x8 array and if it is more than some value I claim room presence and send it with a simple MQTT message to HA.

As a side note, I am also sending the raw data and train a machine learning model, but that’s still in the experimental phase

1 Like

Would you mind sharing code and more details? @komadori1
I did train GBM model when i used to use ble…it work great until i moved to multilevel house…

Sure,
I am using an esp32 mini with the following sensors:

  • PIR for motion
  • BME280 for temperature, humidity and “feels like” temperature calculation using the dewpint
  • BH1750 for light intensity
  • SGP30 for airquality with TVOC, eCO2, “raw H2” and “raw ethanol”
  • INMP441 for noise level measurement and fft to detect specific sounds as seen here
  • and the AMG8833 for room presence

Everything gets sent to HA with MQTT and are included in the recorder.

I am using the <Adafruit_AMG88xx.h> library, so the usage is as in their examples.

The roompresence part hereby is:

unsigned int readAMG8833(float temperature)
{
  unsigned int presence = 0u;


  /* read amg8833 */
  amg.readPixels(pixels);


  /*calculate mean and std */
  float sum = 0.0;
  float amg_mean = 0.0;
  float temp = 0.0;

  for(int i = 0; i<64; ++i)
  {
    sum += pixels[i];
  }
  amg_mean = sum/64;
  for(int i = 0; i<64; ++i)
  {
    temp = pixels[i] - amg_mean;
    amg_STD += temp*temp;
  }
  amg_STD = sqrt(amg_STD/64);

  /* check for presence */
  if( amg_STD > amg_STD_THD )
  {
    presence = 1u;
  }
  else if( amg_mean > (temperature + temperature_offset) )
  {
    presence = 1u;
  }
  else
  {
    presence = 0u;
  }

  return presence;
}

Basically if either the standard deviation is higher than a threshold, or the mean is higher than the temperature reading + some offset (imagine being really close to the amg8833), I claim room presence.

I am polling the sensor every second and wait for a number of consecutive presence detections before I really claim room presence.

Data is sent every minute with MQTT.

The MQTT part is done with <PubSubClient.h> and MQTT Discovery

So far it seems to have been working stable for a month, but I am still testing and experimenting :slight_smile:


I am still playing a lot with the machine learning part, so I do not want to share the code yet before I have some real testing done, but I opted for DL to see if I can trim it down with int8/int16 quantization and L1 regularization to compute the inference on the ESP, but just an overview of what I am doing using python

  • I poll the sql database of HA and filter for all the sensors in the living room where the sensornode is up, this also includes e.g. windowcontacts that I get with zigbee
  • I preprocess the data, interpolating timestamps, scaling and so on
  • For the amg8833 I train it with a CNN, do a globalmaxpool before it joins the other sensors
  • The other sensors get your ordinary LSTM and ANN with the binary classification at the end

I have discovered that the SGP30 has great potential for detecting room presence as well (taking into account the open windows), so does the INMP441, but it’s still too early to draw conclusions

hope this was helpful!

3 Likes

Hi,

The Code you posted it just what I am looking for unfortenetly I can’t seem to get it to work. Can you please post the whole code so I can see where it goes inside the loop.

Thanks.

OK so I think I am getting places but still have some issues.
As I understand I have two variables I need to set: Temperture and the STD thershold. But how do I know what value to give them? it of course depends on the empty room temp at the moment of testing which may change if I turn on the AC or somethinge like that.

Thanks

Is there a way for the ESP to tell HASS direction of movement with the AMG8833?
My plan is to put one on top of each door so I can track direction of a person with a counter. So anytime the counter reads above 1, it will have the lights on on the particular room.
Seems simple, expensive for whole house but most importantly, reliable. If it’s done of course.
Any ideas?

I think I have what you’re looking for. It was driving me nuts not being able to do this and I created this. Working on moving from the prototype to getting it manufactured. It’s a dream!

HTTPS://Smarthomeoccupancy.com

1 Like

Interesting idea. I imagine this is based on keeping a very accurate count of enters and exits. My first thought is, what if it has a false positive or the count or room state gets off somehow?

What sensors are you using? Would this be able to handle pets? How can it tell if you’re going in or out?

Correct. Each time you enter or exit the room it keeps track. If there is a false positive you have three options: 1) use the app to adjust the count 2) use a home assistant “Alexa, I just left the office” or “Alexa, set the office count to x”, or 3) wave your hand in front of the sensor to trigger an event. This doesn’t happen often.

It’s your choice if you want pets to trigger it. The sensor basically does multiple break beams (that’s how I know if somebody is entering or exiting a room). I keep mine mounted at waist level so animals don’t trigger it.

Everything is custom made, but I’m using a photoelectric sensor so you don’t have to mount anything on the other wall. Xbee to communicate back to hub.

Hope this will be something that solves what you were looking for!!!

This is interesting!
Can this be used to detect a chime/sound that is a few seconds long?
I’m thinking it could be used to listen for sounds that indicate it’s commercial break on TV

@Clint Have more details about your system to share with us? What sensors are you using? Ultrasonic?

I’m using ir photoelectric sensors. Ultrasonic had too much lag when I tested a few of them.

1 Like

Hi @Clint

I’d very much like to buy a couple of these, for myself and some clients.
Are they ready to connect to HA?

Shipped to the UK.

Thanks,
Seth

I’m curious if you’ve made any progress on this! It’s an interesting idea.

What do you do about rooms that have multiple entrances? Seems like that can get complicated quickly!