Example: PID thermostat with LM2
PID function
There is a PID function already added by default in Logic Machine -> Scripts -> Tools menu.

Usage
- p = PID:init(parameters)
- p:run()
Parameters
Mandatory:
- current – (object address or name) current temperature value (2 byte float or any numeric value)
- setpoint – (object address or name) temperature set point value (2 byte float or any numeric value)
Optional:
- manual – (object address or name) PID algorithm is stopped when this object value is 1
- output – (object address or name, can be a table with multiple objects) output object (1 byte scaled)
- inverted – (boolean, defaults to false) invert algorithm, can be used for cooling
- min – (number, defaults to 0) minimum output value
- max – (number, defaults to 100) maximum output value
- kp – (number, defaults to 1) proportional gain
- ki – (number, defaults to 1) integral gain
- kd – (number, defaults to 1) derivative gain
Adding Residential script
PID algorithm should be placed inside a Scripts -> Resident –> Add new script.

Script example:
- -- init pid algorithm
- if not p then
- p = PID:init({
- current = '1/1/1',
- setpoint = '1/1/2',
- output = '1/1/3'
- })
- end
-
- -- run algorithm
- p:run()
Script example with multiple output objects:
- -- init pid algorithm
- if not p then
- p = PID:init({
- current = '1/1/1',
- setpoint = '1/1/2',
- output = { 'PWM 1', 'PWM 2', '1/1/5' }
- })
- end
-
- -- run algorithm
- p:run()
Output value:
p:run() returns output value. If output parameter is not set, you can use the return value to control output objects manually in the script.




