Case study: one heating script, four versions
These four scripts ran, one after another, on an X-300 heating the ground floor of a small residential building. Each change was tested in this simulator before it was uploaded. They're shown here as they ran, apart from the building's name. Each version opens in the playground with sensible starting values.
The building's needs were ordinary: a comfortable day temperature, less at night, less during weekday working hours, no heating on mild afternoons, and none in summer.
Version 1: a thermostat with a night setback
let a = 1
do while a = 1
let c = temp1
if c < 150 then
if c > 20 then
if time < 12600 then
let b = extvar2
else
if time > 79200 then
let b = extvar2
else
let b = extvar0
end if
end if
if relay1 = 0 then
if c <= b - extvar1 then
Let relay1 = 1
end if
end if
if relay1 = 1 then
if c > b then
Let Relay1 = 0
end if
end if
end if
end if
loop
end
The original. Day setpoint extvar0, night setpoint extvar2 from 22:00 to 03:30, swing extvar1. Times are written as raw seconds since midnight (12600 is 03:30, 79200 is 22:00), which works but is hard to read; 03:30:00 and 22:00:00 mean the same thing.
The do while a = 1 loop is a common way to write a loop that never ends (a is set to 1 and never changed); a plain DO does the same.
Two quirks worth noticing: the sensor sanity check (c > 20, c < 150) is on separate lines at odd indentation, and Let and Relay1 are capitalised on a couple of lines. Both run fine; the unit treats statement words and its own variable names without regard to case.
Version 2: a weekday temperature
let a = 1
do while a = 1
let c = temp1
let i = date + 3
let j = i % 7
let h = extvar3
if h < 0.3 then
let h = 0.3
end if
if h > 5 then
let h = 0.3
end if
let b = extvar0
if j < 5 then
if time > 28799 then
if time < 57600 then
if extvar1 > 49 then
let b = extvar1
end if
end if
end if
end if
if time < 12600 then
let b = extvar2
end if
if time > 79200 then
let b = extvar2
end if
let g = 0
if c < 150 then
if c > 20 then
let g = 1
end if
end if
if g = 1 then
if relay1 = 0 then
if c <= b - h then
let relay1 = 1
end if
else
if c > b then
let relay1 = 0
end if
end if
end if
loop
end
Two additions:
- Weekday daytime setpoint. From 08:00 to 16:00, Monday to Friday, the target drops to
extvar1. The day of the week comes from(date + 3) % 7, as in lesson 4. Settingextvar1to 49 or below switches the feature off without touching the script (if extvar1 > 49 then), which is handy for the odd week when the usual schedule doesn't apply. - Anticipation moved to
extvar3and protected. The swing (here called anticipation) is clamped: anything below 0.3 or above 5 is treated as 0.3. A slip on the control page (typing 30 instead of 3.0) can't make the heat cycle wildly.
The night window moved to the end of the "pick the target" section, so night always wins over the weekday rule.
Version 3: no heat on mild afternoons
let a = 1
do while a = 1
let c = temp1
let i = date + 3
let j = i % 7
let h = extvar3
if h < 0.3 then
let h = 0.3
end if
if h > 5 then
let h = 0.3
end if
if extvar4 <> f then
let f = extvar4
let t1 = 36000
end if
let d = 0
let b = extvar0
if j < 5 then
if time > 28799 then
if time < 57600 then
if extvar1 > 49 then
let b = extvar1
end if
end if
end if
end if
if time < 12600 then
let d = 1
end if
if time > 79200 then
let d = 1
end if
if d = 1 then
let b = extvar2
end if
let e = 1
if d = 0 then
if t1 > 0 then
if extvar4 > 55 then
let e = 0
end if
end if
end if
if e = 0 then
let relay1 = 0
end if
let g = 0
if c < 150 then
if c > 20 then
let g = e
end if
end if
if g = 1 then
if relay1 = 0 then
if c <= b - h then
let relay1 = 1
end if
else
if c > b then
let relay1 = 0
end if
end if
end if
loop
end
A Raspberry Pi on the same network fetches the outside temperature and writes it into extvar4 every 10 minutes, alternating the last digit so every push is a new value (lesson 6). The script adds:
d: 1 during the night window. The outside rule never applies at night; the building always heats to the night setpoint.e: 0 means "heat must be off". During the day, while the reading is fresh (t1 > 0) and above 55°,egoes to 0 and relay 1 is forced off.- If the Pi stops sending for an hour,
t1runs out and the script is exactly version 2 again.
The simulator's tests run versions 2 and 3 over the same days and check that they behave identically whenever the feed is missing.
Version 4: a heating season and a test button
let a = 1
do while a = 1
let c = temp1
let i = date + 3
let j = i % 7
let h = extvar3
if h < 0.3 then
let h = 0.3
end if
if h > 5 then
let h = 0.3
end if
if extvar4 <> f then
let f = extvar4
let t1 = 36000
end if
let d = 0
let b = extvar0
if j < 5 then
if time > 28799 then
if time < 57600 then
if extvar1 > 49 then
let b = extvar1
end if
end if
end if
end if
if time < 12600 then
let d = 1
end if
if time > 79200 then
let d = 1
end if
if d = 1 then
let b = extvar2
end if
let e = 1
let i = date - 10957
let i = i % 1461
if i < 366 then
if i > 59 then
let i = i - 1
end if
else
let i = i - 366
let i = i % 365
end if
if i > 150 then
if i < 273 then
if relay2 = 0 then
let e = 0
end if
end if
end if
if d = 0 then
if t1 > 0 then
if extvar4 > 55 then
let e = 0
end if
end if
end if
if relay3 = 0 then
let t3 = 0
else
if t3 = 0 then
let t2 = 18000
let t3 = 36000
end if
if t2 = 0 then
let relay3 = 0
end if
if c > 80 then
let relay3 = 0
end if
end if
if relay3 = 1 then
let e = 2
end if
if e = 0 then
let relay1 = 0
end if
if e = 2 then
let relay1 = 1
end if
let g = 0
if c < 150 then
if c > 20 then
let g = e
end if
end if
if g = 1 then
if relay1 = 0 then
if c <= b - h then
let relay1 = 1
end if
else
if c > b then
let relay1 = 0
end if
end if
end if
loop
end
Two of the unit's three relays weren't wired to anything, so they became switches on the control page:
- Relay 2, "Season ON". From June to September the heat is off, unless this is switched on. The day of the year is worked out as in lesson 7. In season the switch does nothing.
- Relay 3, "Test heat". Pressing it forces the heat on for at most 30 minutes (
t2 = 18000) or until the room passes 80°, then the script switches relay 3 back off itself.t3just remembers that the 30-minute timer has already been started for this press, so it isn't restarted on every pass. It's for checking from a distance that the boiler actually fires, without changing any setpoints.
e = 2 is a third state, "force on", checked after the season and outside rules so that Test heat beats both.
The whole script is 1,629 bytes on disk and 1,732 as the unit counts them (each line ending counts as two bytes), under the 1,900-byte limit. All four versions touch the nesting limit of 5 in places (the loop plus four IFs), which is one reason v4 uses the flags d, e and g instead of nesting the rules inside each other.
What carried through all four versions
- Decide, then act. Every version first works out a target (
b) and whether heat is allowed (e), then runs the same short thermostat block. New rules only touch the first half. - Every input has a safe fallback. A bad sensor reading, a silly control-page value, a dead weather feed: each one falls back to plain thermostat behaviour instead of leaving the heat on or off forever.
- Test before upload. Every version was run in this simulator over winter days, warm days, weekends, a dead feed and power cuts before it went near the unit. The current one was then uploaded, checked to say "Running", and tested once with the Test heat button.