The
twin command requires the iot feature to be enabled during installation.twin command runs digital twin simulations for IoT devices with support for multiple protocols and physics strategies.
run
Start a digital twin simulation from a device definition.Path to the device definition YAML file
Path to the device library or resources directory
Path to a configuration override file
Device definition structure
A device definition file describes the twin configuration:Supported protocols
MQTT
Message Queue Telemetry Transport for lightweight pub/sub messaging. Configuration:broker: MQTT broker URLclient_id: Unique client identifiersubscriptions: Topics to subscribe to (supports wildcards)
Modbus
Modbus TCP/RTU protocol for industrial equipment. Configuration:host: Modbus server hostport: Modbus server portunit_id: Modbus unit/slave ID
Physics strategies
Physics strategies simulate sensor behavior and data generation.Sine wave
Generate smooth oscillating values.min: Minimum valuemax: Maximum value- Frequency: 0.1 Hz (default)
- Temperature cycles
- Pressure variations
- Voltage fluctuations
Noise sine
Sine wave with added randomness.In the current implementation,
noise_sine behaves identically to the sine strategy. Use the script strategy for custom noise patterns.Script (Rhai)
Custom behavior using Rhai scripting language.time: Simulation time in secondsstate: Access to other twin variables- Math functions:
sin(),cos(),sqrt(), etc.
- Complex sensor relationships
- State machines
- Conditional logic
- Custom algorithms
Replay
Replay historical data from CSV files.file: Path to CSV file (relative to device definition)column: Column name to read (optional, uses first column if omitted)loop: Restart from beginning when file ends (default: true)
Usage examples
Simple temperature sensor
Industrial pump with Modbus
Data replay simulation
Simulation loop
The twin runs in a continuous loop:- Poll adapters: Check for incoming messages from protocols
- Update state: Apply received data to twin state variables
- Tick physics: Execute all physics strategies
- Publish state: Send updated variables to protocol adapters
- Precise timing: Wait for next tick (1 second interval)
State management
The twin maintains a state object with all variables:- Be set by physics strategies
- Be updated by protocol messages
- Be read by other strategies
- Be published to protocols
Error handling
Device file not found
Invalid YAML
Unknown adapter
mqtt, modbus
Missing replay file
Performance
- Tick rate: 1 second (1 Hz)
- Timing: Precise, compensates for processing time
- Lag handling: Catches up if tick processing is slow
- Async: All I/O operations are non-blocking
Monitoring
The twin logs activity to stdout:Use cases
Testing IoT backends
Simulate hundreds of devices without physical hardware
Protocol development
Develop and test protocol handlers with realistic data
Training and demos
Demonstrate IoT systems without real devices
Data generation
Generate test data for analytics and ML models
Advanced features
Config overrides
Override device configuration at runtime:Multiple devices
Run multiple twins in parallel (requires separate processes):Dry run
Test configuration without connecting to protocols:Next steps
- Learn about device definitions
- Explore Rhai scripting
- Set up MQTT brokers
- Configure Modbus devices