In many home automation scenarios we have an observed value, maybe a sensor reading, maybe a time interval since something happened. We want to convert that value to a probability, e.g. what's the probability that someone left the room given how long it's been since any activity there.
Typically we might start using conditions:
time < 5 ? 0.2 :
time < 10 : 0.5 :
And we might throw in an abs function somewhere to deal with negative values. This piece-wise approximation is a a good way to start and it's easy to think about but there is a better way.
What we want is a function that maps any input value to the range 0.0 - 1.0. We want it to be smooth. We want to be able to adjust how quickly it rises (or falls) and how much of a 'dead zone' there is around zero. For example, for 2 minutes we want to assume nobody has left a room, after two minutes we want the probability to rise slowly and by five minutes we want it to rise quickly as we are fairly sure they have now departed.
The ATAN function
The ATAN function is an ideal function to achieve this. If we only have positive values we can use p = atan(x/k)/pi*2 and for any positive x we will get a value from 0.0 to 1.0. We can adjust how quickly it rises by adjusting k. And if we want a falling probability we can just do (1 - p).
If we want a 'dead zone' at the start we can just offset the graph and divide by pi without a multiplier, but we need to add back the new zero point to get the start point back to zero. e.g.
This produces the nice curve shown at the top and works well for converting positive values to probabilities.
One other probability trick you should know for home automation is how to combine two probabilities (a, b) in an either-or situation. Adding them would result in a value greater than 1.0 because there's also the chance that both a and b are true, and we'd be double counting. But if we subtract (a * b) which is the probability that both are true we fix that problem:
float combined = a + b - a * b;
This ensures that the combined probability never goes above 1.0. It comes from the inclusion–exclusion principle which holds when the events are independent.
I've been working on home automation for over 15 years and I'm close to achieving my goal which is a house that understands where everyone is at all times, can predict where you are going next and can control lighting, heating and other systems without you having to do or say anything. That's a true "smart home".
My year long Bluetooth project that won the $20,000 HCI and Microsoft competition during lockdown has continued to grow and now reliably tracks how many people are in the house and outside and can locate any device down to room level.
Digital Twin are an online representation of a real world object, a copy of its properties in the digital world and a way to send updated and commands to it. In effect I've been making them for years but now they have a trendy name.
An overview of the many sensors I've experimented with for home automation including my favorite under-floor strain gauge, through all the usual PIR, beam and contact sensors to some more esoteric devices like an 8x8 thermal camera.
ESP32 provides a great platform for sensors around the house but by the time you've added a USB power brick, cable and enclosure it's quite messy. I wanted a device that I could just plug in with no exposed wires and no mounting needed so I designed one in OpenSCAD.
Bluetooth sensing for home automation is a great proxy for people counting as it can detect and locate each cellphone in the house. iBeacons attached to tools, cars and pets can provide a 'find my anything' feature too.
Microwave doppler sensors can be found in some alarm sensors but there are also available very cheaply as a separate component. They offer exceptional range but suffer from false triggers requiring a probailistic approach to people sensing.
Several years ago we did a major remodel. I did all of the finish electrical myself and supervised all of the rough-in electrical. I also put in all of the electrical system and water in our barn. I have opinions ...
An if-this-then-that style rules machine is insufficient for lighting control. This state machine accomplishes 90% of the correct behavior for a light that is controlled automatically and manually in a home automation system.