I have been lately playing more with expressions and here is one finding which may be useful to others.
There is an example where data is pushed to an array and then calculations can be done with the data:
https://community.getvera.com/t/how-to-time-series/208506?u=vpow
In this example there is three main parts
-create interval condition which is then triggering expression
-push data to array
-do something with array values
So something like this
isint
getstate( "Reactor Sensor 13", "urn:toggledbits-com:serviceId:ReactorGroup", "GroupStatus_root" )
series
if( isint=="1", push( series, tonumber( getstate( "BR_Temperature", "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature" ) ), 5), series)
range
max(series)-min(series)
The issue is that variables get initial value of null
. So if now the interval driving isint
is set for example to one hour, during the first hour series
is null, range
tries to get min and max out of null and that will result to coercion error. After the first push to array happens then everything works ok.
To fix this, an initial value can be used:
isint
getstate( "Reactor Sensor 13", "urn:toggledbits-com:serviceId:ReactorGroup", "GroupStatus_root" )
temperature1
tonumber(getstate("BR_Temperature", "urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature"))
series
if(series==null, list(temperature1), if( isint=="1", push( series, temperature1, 5), series))
range
max(series)-min(series)
Now if the series
is null
, it will be initiated as an array with a value and range
calculation works from the beginning.
Above issue is not limited to only arrays, but all variables. For example take this simple expression which tries to store minimum value:
mintemp
if(temperature1 < mintemp, temperature1)
Since the initial value of mintemp
is null
, the result is only error message “[luaxp]Invalid comparison (number<table) at 15”. The same fix can be applied:
if(mintemp==null, temperature1, if(temperature1 < mintemp, temperature1))
An improvement idea might be a box next to expression/variable where initial value could be given (number, string, array)
One thing the expressions could be used is filtering. I have some data sources where data has some noise and needs to be filtered. Here couple examples I have been working on.
First collect data to an array:
temps
if(temps==null, list(outtemp,outtemp,outtemp),if(every5min=="1", push(temps, outtemp, 3) , temps))
low-pass filtering:
LPtemp
if(LPtemp==null, temps[1], if(every5min=="1",0.6*temps[1]+0.4*temps[2],LPtemp))
median filtering:
medtemp
if( (temps[1]<=temps[2])&&(temps[1]<=temps[3]), if(temps[2]<=temps[3],temps[2],temps[3]), if( (temps[2]<=temps[1])&&(temps[2]<=temps[3]), if(temps[1]<=temps[3],temps[1],temps[3]) , if(temps[1]<=temps[2],temps[1],temps[2])))
If the LuaXP had median function this would be much easier