The Blog of Ian Mercer.

Digital Twins are never identical

An initial assumption for a digital twin might be that it's a direct copy of the sensor and actuator values from a device in the real world. When the real-world object changes value the twin changes value and when the twin value is changed by software the real-world device turns a light or pump on or off to match.

This is unfortunately a gross simplification for what really needs to happen. The ideal digital twin is far more complex and the unreliable network between them complicates matters further. Based on my experience building digial twins (which goes back a long way before they were given that name) here are the main things you need to consider:

Separate 'logical' and 'physical' device twins

Early on in my home automation system evolution I decided to separate logical devices from physical devices. For example, the 'Hall Light' is a logical device, it will always be called 'hall light' or one of its alternate names, it will always be in the hall and it will always have a brightness from 0.0 to 1.0. Early on there was an X10 device 'A8' that was the switch for the hall light. It had 256 brightness levels 0x00-0xff. Later it was replaced with a Lutron Caseta light switch with levels from 0 to 100. Because I modeled the logical and the physical devices separately I have been able to maintain many years of data for each light switch with a consistent name and a canonical brightness value even though the physical device has changed.

Separate Desired state and Actual state

You may want to turn a light on from software but whether or not that light turns on is another matter. Tracking the logical device state 'hall light on' and the physical device state 'relay=0' is essential for understanding (i) whether the device is functioning and (ii) whether a change in the physical device state was triggered by the system or by a human pressing the light switch. The latter indicates that there is someone on the room right now pushing the switch and asking for manual control over the light, the former means the system is still in control of the light. Separating the logical device and the physical device is one way to achive this separation of desired vs actual state.

Timing and reconciliation

A naive implementation would see the logical device change state and send a request to the device, just once, to change state. There are many things that can go wrong in between and even on the most reliable light switches, occasionally they miss a transmission. A better approach is to continually ensure that the physical light matches the logical light sending commands to update it as necessary, or polling it to check that it has the desired state. Not all devices can do this, some are 'fire-and-forget', for these you may choose to repeat an on/off command some time later to ensure that the light really is on/off.

Values need to be mapped

Most devices quantize the values sent to them into discrete stepped values. A light might only have 100 brightness levels, a multizone amplifier may have only 40 levels and a thermostat might internally have 0.1 degrees resolution in farenheit. Meanwhile our logical device wants to track a consistent, canonical 0.0 - 1.0 for all brightness and volume levels and it wants to use Celcius throughout for temperatures. This is a problem because when we set the brightness to 0.123 that might translate to 12 on the device itself and would come back as 0.12 when we convert it back. Our system would see that as a change in brightness and would assume, incorrectly, that someone pushed the switch. An essential part of the ideal digital twin is a mapping function that maps logical values to and from physical values and can determine when they are the same. This could be as simple as rounding the value to an int or it could involve a curve or other functional mapping and a quantization step. Mapping color values to and from Phillips Hue and RGB LED strips is another example of value mapping.

Sensor data needs filtering

Devices typically do not present smoothly changing sensor values. Most have low-amplitude noise on them and sometimes they suffer large random glitches that need to be filtered out. A digital twin will need to implement filtering including rejecting spurious values and perhaps a Kalman filter. Often you will do this at the edge in the device itself as it reduces the bandwith needed to the server (no need to transmit the noisy values). Even when it does happen at the edge you may want to change the parameters for that filtering to the device through its digital twin.

Sensor data needs rate limiting

In addition to filtering the sensor values you may wish to limit how much data is sent to the server over time, for example, only send changes when the temperature changes by more than 0.1 degree celcius or limit changes to no more often than every five minutes. But you will also need to make sure the device sends some data regularly to indicate that it is still alive even when the value is not changing. A delta, min interval and max interval setting are useful in these cases but even smarter data compression is possible.

Time of observation

This is a hard one because the sensor or gateway may or may not have a reliable clock or we may be buffering data on the device or storing it offline for later sending to the server when a connection is made. Should we use the time of arrival of the message, or the time it was captured by the device? A Raspberry Pi-W without an RTC Module will drift by several seconds during a day between connections to an NTP server so we can't rely on the device time being accurate. One approach is to send both the recorded time and the local time (now) with each message to the server, the server can adjust the recorded times based on the difference in time between the two values and its own more reliable clock. There is no easy answer to this problem but you should consider it in the design of your digital twin.

Metadata

In addition to the actual sensor values you may want to track data about the gateway or node that manages the sensors, for example: available disk space, internal temperature, wifi signal strength, mesh connections, ... It's important that your digital twin system is able to handle the data about gateways as well as individual sensors and to understand the relationships between them. A graph model of the connectivity is useful for this purpose and can be essential for diagnosing problems, e.g. the temperature sensor isn't updating because the gateway has an intermittent WiFi connection.

You may need a state machine

You may need a state machine on the input side and a different state machine on the output side. A refrigeration unit for example cannot be cycled on and off quickly without causing damage to it, you need to track state in order to enforce rules about how often it cycles. Some devices may have specific sequences they need to be put through to change a setting. Some protocols require a state machine to handle the messages: even a simple telnet protocol for example needs a state machine that handles disconnected vs connected states. State machines can also be used to condition digital inputs, like debouncing a noisy contact, or waiting for three pulses from a doppler radar sensor before concluding that it really is being triggered. I use my C# hierarchical state machine for these situations.

The digital twin may exist across several components

Azure IoT provides some limited digital twin capabilities and can reliably pass messages back and forth between devices and the cloud. You might use those capabilities and then build a richer digital twin with the techniques above using Azure functions triggered from Service Bus messages received by the IoT hub.

Digital Twin Full

Summary

As you can see there's quite a bit of extra work to do beyond the naïve assumption that a digital twin is just a mirror of some values on a device with the same values in the cloud.

Related Stories

Home Automation

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".

Ian Mercer
Ian Mercer

Bluetooth Tracking Project

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.

Ian Mercer
Ian Mercer

Home Automation Sensors

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.

Ian Mercer
Ian Mercer

Why smarthomes are hard

Why automated learning is hard for a smart home. The perils of over-fitting, under-fitting and how the general unpredictable nature of life makes it hard to build a system that learns your behavior.

Ian Mercer
Ian Mercer

3d Printed ESP32 Brick

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.

Ian Mercer
Ian Mercer

Bluetooth Sensing for Home Automation

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.

Ian Mercer
Ian Mercer

Microwave Doppler Sensors (RCWL-0516)

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.

Ian Mercer
Ian Mercer

Optical-beam sensors

Optical-beam sensors are reliable and can cover a long-distance such as across a garage or aisle-way. When they include multiple-beams they have good false-trigger rejection.

Ian Mercer
Ian Mercer

PIR Sensors for Home Automation

PIR sensors are cheap and easy to use but they suffer from slow response times and low repeat rates.

Ian Mercer
Ian Mercer

Strain-gauges

Strain-gauges are my top-rated sensor for home automation because they are invisible, reliable and can be tuned to detect people and ignore pets.

Ian Mercer
Ian Mercer

Logistic function - convert values to probabilities

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.

Ian Mercer
Ian Mercer

ATAN curve for probabilities

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.

Ian Mercer
Ian Mercer

Home Construction Advice

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 ...

Ian Mercer
Ian Mercer

T-Mobile home internet

I'm testing a T-Mobile Home Internet device as a backup to XFinity and a way to offload half our monthly traffic to avoid the XFinity 1.2TB cap

Ian Mercer
Ian Mercer

Bluetooth

One of my inventions recently won a $20k global competition for applications that could help in a pandemic. It uses Bluetooth to count people.

Ian Mercer
Ian Mercer

Probabilistic Home Automation

A probabilistic approach to home automation models the probability that each room is occupied and how many people are in that room.

Ian Mercer
Ian Mercer

Multiple hypothesis tracking

A statistical approach to understanding which rooms are occupied in a smart house

Ian Mercer
Ian Mercer

A state machine for lighting control

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.

Ian Mercer
Ian Mercer

iBeacon meetup in Seattle - January 2015

My notes on the iBeacon meetup in Seattle held in January 2015

Ian Mercer
Ian Mercer

Home Automation Systems as a Graph

Using nodes and links to represent a home and all the devices in it

Ian Mercer
Ian Mercer

The Internet of Hubs (and things)

Maybe it should be called the Internet of Hubs instead

Ian Mercer
Ian Mercer

Showing home status with just a single RGB LED

Multicolored LEDs can convey a lot of information in a small space

Ian Mercer
Ian Mercer

A wireless sensor network using Moteino boards

The diminutive Arduino boards include a powerful transmitter/receiver

Ian Mercer
Ian Mercer

JSON Patch - a C# implementation

Ian Mercer
Ian Mercer

A Quantified House - My Talk to the Seattle Quantified Self Meetup

My talk to the Seattle Quantified Self meetup

Ian Mercer
Ian Mercer

Integrating an Android phone into my home automation system

Some new features for my home automation using an Android phone

Ian Mercer
Ian Mercer

Before there was the web there was BeebTel

Just thought I should mention that I built a web-like system before the web existed

Ian Mercer
Ian Mercer

My first programme [sic]

At the risk of looking seriously old, here's something found on a paper tape

Ian Mercer
Ian Mercer

The Internet of Dogs

Connecting our dog into the home automation

Ian Mercer
Ian Mercer

Finally got the 1U Atom Server racked up

Ian Mercer
Ian Mercer

Timelapse video using the GoPro HD Hero

Ian Mercer
Ian Mercer

Web site crawler and link checker (free)

Ian Mercer
Ian Mercer

Smart home energy savings - update for 2010

Ian Mercer
Ian Mercer

I should have created Four Square ...

Ian Mercer
Ian Mercer

How can I tell if my house is smart?

Ian Mercer
Ian Mercer

Asian Gadgets

Ian Mercer
Ian Mercer

A great video explaining the Semantic Web

Ian Mercer
Ian Mercer

Interesting Twitter Posts March 15th-

Ian Mercer
Ian Mercer

Twitter links for Week beginning March 8th

Ian Mercer
Ian Mercer

Elliott 803 - An Early Computer

Ian Mercer
Ian Mercer

Facebook, social gaming and points

Ian Mercer
Ian Mercer

Useful Twitter links Feb 8-Feb 15 2010

Ian Mercer
Ian Mercer

Looking forward to the new year and our new datacenter

Historical note about moving my servers into a datacenter

Ian Mercer
Ian Mercer

When will people learn to backup?

A rant about RAID

Ian Mercer
Ian Mercer

Future proof your home with a new conduit system?

Running conduit can be expensive but maybe you don't need one to every room

Ian Mercer
Ian Mercer

Balloon Boy was much ado about nothing - Twitter

Some of the more witty comments on Twitter about the Balloon Boy hoax

Ian Mercer
Ian Mercer

Shortened URLs should be treated like a Codec ...

Expanding URLs would help users decide whether or not to click a link

Ian Mercer
Ian Mercer

Tagging File Systems

Isn't it time we stopped knowing which drive our file is on?

Ian Mercer
Ian Mercer

WMPnetwk.exe started using 50% of my CPU

Uninstalling Windows Media Player - the end of an era

Ian Mercer
Ian Mercer

Introducing Jigsaw menus

A novel UI for menus that combines a breadcrumb and a menu in one visual metaphor

Ian Mercer
Ian Mercer

Amazon Instance vs Dedicated Server comparison

Some benchmark performance for Amazon vs a dedicated server

Ian Mercer
Ian Mercer

Agile Software Development is Like Sailing

You cannot tack too often when sailing or you get nowhere. Agile is a bit like that.

Ian Mercer
Ian Mercer

AntiVirus Software is the Worst Software!

When your anti-virus software starts stealing your personal data, it's time to remove it!

Ian Mercer
Ian Mercer

Second Drobo Update

At this point things were looking up for my Drobo

Ian Mercer
Ian Mercer

It's all about disk speed

Why disk speed is the most critical aspect for most modern PCs and servers

Ian Mercer
Ian Mercer

Comcast woes and a new monitoring utility

Monitoring a cable modem using its HTML management interface

Ian Mercer
Ian Mercer

Core duo desktop machine runs cool

Ian Mercer
Ian Mercer

Giving up on Internet Explorer

Ian Mercer
Ian Mercer

New Home Automation Server

Ian Mercer
Ian Mercer

Dell upgrades - a pricey way to go

Ian Mercer
Ian Mercer

World's Smartest House

Over 15 years of experimentation with home automation

Ian Mercer
Ian Mercer

Pressure Sensors for Home Automation

Pressure sensors can detect HVAC system operation and could potentially detect clogged filters.

Ian Mercer
Ian Mercer