Example: Digital Twins
9 min
end to end walkthrough build a pump digital twin model, attach static and dynamic attributes, create an instance, and query live values setup from litmussdk import digital twins from litmussdk devicehub import devices, tags assumes the default connection is configured; see managing connections docid\ vwtfo9sxcmrbfx9cafenw 1\ create a model model = digital twins create model( name="pump", description="centrifugal pump generic template", ) model id = model id print(f"created model {model id}") 2\ attach static attributes static attributes don't change at runtime manufacturer info, location, etc digital twins create static attributes( model id=model id, attributes=\[ {"key" "manufacturer", "value" "acme pumps"}, {"key" "maxflowrate", "value" "200 l/min"}, ], ) 3\ attach dynamic attributes dynamic attributes pull live values from tags or topics first find a relevant tag from your device all devices = devices list devices() pump device = next(d for d in all devices if d name == "plantfloor pump 01") device tags = tags list registers from single device(pump device) pressure tag = next(t for t in device tags if t tag name == "pressure") then create a dynamic attribute that subscribes to that tag's topic digital twins create dynamic attribute( model id=model id, name="pressure", data type="number", unit="kpa", topic=pressure tag topics\[0] topic, ) 4\ create an instance the model is a template; instances are concrete twins of real devices instance = digital twins create instance( model id=model id, name="plantfloor pump 01", ) \# set running so the instance starts subscribing to its dynamic attribute topics digital twins set instance state(instance id, state="running") 5\ query live values \# all instances of this model all pumps = digital twins get instance by model(model id) for inst in all pumps print(f"{inst name} state={inst state}") 6\ add a transformation (optional) digital twins create transformations( model id=model id, transformations=\[{ "name" "pressurecategory", "formula" 'if(pressure > 150, "high", "ok")', }], ) 7\ tear down digital twins delete instance(instance id) digital twins delete model(model id) see also digital twins docid\ rvi0 uxy2vvgeazetbnlu full reference managing connections docid\ vwtfo9sxcmrbfx9cafenw le vs lem