Senzorv1.0.1

Generators

The six value generators, their parameters and constraints, and the noise option.

Every field of a template has a generator that produces the field's value on each tick. A generator is a table with a generator key and that generator's parameters:

fields.co2 = { generator = "random_walk", start = 600, min = 400, max = 2000, step = 15 }

The same table can also give the field a unit and a name; see Fields.

GeneratorProduces
constantThe same value on every tick.
random_walkA number that drifts up and down by a random step, within bounds.
sineA number that follows a sine wave over time.
uniformA new random number in a range on every tick.
sequenceThe values of a list, one per tick.
scriptWhatever a Rhai script computes.

Every generator also accepts noise. All numbers must be finite: TOML allows nan and inf, Senzor does not. An unknown parameter, a missing required parameter or an unknown generator name is an error.

Random draws come from the instance's own random number generator. With a [simulation] seed, they repeat from run to run; see Reproducible runs. For how values, pins and runs relate, see Values and generators.

constant

Publishes value on every tick.

ParameterTypeDefaultRules
valuestring, number or booleanrequired
noisenumbernoneOnly when value is a number.
fields.firmware = { generator = "constant", value = "1.4.2" }

random_walk

The first tick publishes start. Every later tick adds a random amount between -step and +step and keeps the result between min and max.

ParameterTypeDefaultRules
startnumberrequiredBetween min and max, inclusive.
minnumberrequiredLess than max.
maxnumberrequired
stepnumberrequiredGreater than 0. The largest change per tick.
noisenumbernone
fields.humidity = { generator = "random_walk", start = 45, min = 30, max = 70, step = 0.5 }

sine

Follows center + amplitude × sin(2π × (t + phase) / period), where t is the time since the instance started. The value moves between center - amplitude and center + amplitude and repeats every period.

ParameterTypeDefaultRules
centernumberrequired
amplitudenumberrequired
perioddurationrequiredGreater than 0.
phaseduration"0s"Shifts the wave. A quarter of period starts at the peak.
noisenumbernone
fields.temperature = { generator = "sine", center = 21.5, amplitude = 1.5, period = "24h" }

The wave starts when the instance starts, not at a time of day. With period = "24h" and no phase, the peak comes 6 hours after the start.

uniform

Draws a new random number on every tick, at least min and less than max. Consecutive values are unrelated; use random_walk for a value that changes gradually.

ParameterTypeDefaultRules
minnumberrequiredLess than max.
maxnumberrequired
noisenumbernone
fields.rssi = { generator = "uniform", min = -90, max = -40 }

sequence

Publishes the values of values in order, one per tick.

ParameterTypeDefaultRules
valuesarray of strings, numbers or booleansrequiredNot empty. The types can be mixed.
loopbooleantruetrue starts again from the first value after the last one. false keeps publishing the last value.
noisenumbernoneOnly when every value is a number.
fields.state = { generator = "sequence", values = ["open", "closed"] }

script

Runs a Rhai script on every tick; the value of its last expression is the field's value. Scripts and commands describes what a script sees and what it can do.

ParameterTypeDefaultRules
scriptstringrequiredMust compile, and must not be empty.
startstring, number or boolean0The script's value on the first tick. A number must be finite.
noisenumbernoneAdded when the script returns a number.
[templates.fields.temperature]
generator = "script"
start = 20.0
script = '''
if state.heating == true { value + 0.2 } else { value - 0.1 }
'''

A TOML string between ''' keeps the script as written, over several lines and with " inside. The script must return a number, true or false, or a string; anything else is an error of the instance on that tick, and the field keeps its latest value.

noise

Any generator accepts noise. On every tick it adds a random amount between -noise and +noise to the generator's value.

  • noise must be greater than 0.
  • On constant and sequence, it needs numeric values. On script, it applies when the script returns a number.
  • Noise does not change the generator's own state: a random_walk continues from its value without noise.
  • The result is not limited again, so a random_walk with noise can go up to noise beyond min and max.
fields.temperature = { generator = "sine", center = 21.5, amplitude = 1.5, period = "24h", noise = 0.1 }

Constraint messages

When a generator breaks a rule, validation reports one of these messages, as field `<name>`: <message> for a template field or field `<name>` override: <message> for a device's field override.

MessageCause
numbers must be finiteA parameter or value is nan, inf or -inf.
noise must be greater than 0noise is 0 or negative.
noise requires numeric valuesnoise on a constant or sequence with a non-numeric value.
min must be less than maxrandom_walk or uniform with min equal to or greater than max.
start must be between min and maxrandom_walk with start outside the bounds.
step must be greater than 0random_walk with step 0 or negative.
period must be greater than 0sine with period = "0s".
values must not be emptysequence with values = [].

Senzor checks the rules in the order of this table and reports only the first one each field breaks. A script field can have a problem with its script on top: script is empty, or script does not compile: <reason>, where the reason names the line and position. For all validation messages, see Validation errors.

On this page