Programming a smart home with a fluent, domain-specific language
Programming a smart home with a fluent, domain-specific language
In response to a question I received recently, here is an example of the
fluent extensions to C# that my home automation system uses to define
how it should behave. In effect this is a domain specific language for
modeling home automation behavior as a finite state machine. Note how
you can use either a purely declarative sequence or you can use
procedural code within a Do() block.
Living.FloorSensor
.Then(Entrance.HallBeam, 30)
.Provided(Time.Bedtime)
.ProvidedNot(Home.DinnerGuests)
.PulseStretch(10 * 60) // don't announce it too often - every 10 minutes
.Do("Garage doors warning", () =>
{
if (Garage.GarageDoors.AreAnyOpen)
{
FirstFloor.Kitchen.AllMediaZones.AnnounceString("Excuse me, I think you may want to close the garage doors.");
}
});
In many ways this is similar to the Reactive Framework from Microsoft
except my work on this predates the availability of Reactive Framework
and unlike the Reactive Framework my 'sequential logic blocks' include
persistence logic so the entire state can be saved to disk (as it is
every second). This is important because some transitions in the state
machine might be several hours or even days long and you need to be able
to restart the system and carry on exactly where you left off.
One key benefit of the declarative approach over the procedural approach
is that the declarative approach can explain itself. So, the log entry
for a light going on can show that the light was turned on because 'it
was dark' and 'we had visitors' and 'someone came into the room'.
Compared to traditional home automation systems where you either have no
logging at all or you have a log of what happened, this kind of logging
is invaluable for figuring out what went wrong when the logic gets
complicated. So in this example I should have moved the test for
Garage.GarageDoors.AreAnyOpen out to a Provided clause which would
allow it to be part of the reverse-chain logic explanation.
Partial results can be captured at any point in these logic chains and
then reused in other chains because each partial result is itself a
Sensor device that implements the full fluent interface.
Ultimately I plan to hook the logging for what happened back up to the
NLP engine which will allow users to ask the home 'Why did you put the
driveway light on last night around 9pm?' and ultimately I plan to allow
the logic itself to be defined using natural language.
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".
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.
One way to reduce the volume of sensor data is to remove redundant points. In a system with timestamped data recorded on an irregular interval we can achieve this by removing co-linear points.
Home automation systems need to respond to events in the real world. Sometimes it's an analog value, sometimes it's binary, rarely is it clean and not susceptible to problems. Let's discuss some of the ways to convert these inputs into actions.
Another super useful function for handling sensor data and converting to probabilities is the logistic function 1/(1+e^-x). Using this you can easily map values onto a 0.0-1.0 probability range.
In a home automation system we often want to convert a measurement into a probability. The ATAN curve is one of my favorite curves for this as it's easy to map overything onto a 0.0-1.0 range.
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.