Variable & Temperature Sensor Value Averaging

Has anyone implemented a variable in Reactor to read a sensor value and average over time? I am OK using a rolling average using the formula ‘temp_avg = temp_avg + (current_temp - temp_avg) * alpha’. Thinking of using .50 for alpha to lag/smooth it out. Real question would be, how can I get such a variable to repoll / re-evaluate in Vera at a certain frequency, for example, every 20 minutes?

So making some headway here…

  1. Created an ‘Outside Temperature’ Group with ‘interval’ condition to run every 20 minutes.
  2. Setup a few expressions:
    current_temperature=getstate( 38, “urn:upnp-org:serviceId:TemperatureSensor1”, “CurrentTemperature” )
    alpha=0.5
    average_temperature
  3. Attached activity ‘SetVariable’ with average_temperature={average_temperature} + ({current_temperature} - {average_temperature}) * {alpha}

Looks like 3) doesnt work as intended. I also need to figure out how to set the default value of this to ‘current_temperature’ if null initially.

Modified items for 2) to be the following:

I am exporting ‘temperature_outside_ma’ and in my action set to run every 20 minutes, coping the value from ‘average_temperature’ into ‘temperature_outside_ma’. Appears to work… will let it run for a few days and start using that for my Hot Tub ranges. Have a Tuya-based floating temp sensor which I cant get into Vera (at least yet), so using the average temperature in that app to set a temp program based on average outside ambient temperature.

Catching up a bit late… your Set Variable action value should have had the entire expression inside one set of { } curly braces, like this: { average_temperature + ( current_temperature - average_temperature ) * alpha }. But what you’ve got now is good to go.

2 Likes

Late, but there nonetheless. I actually like the function better in the action as I have better control over when it gets evaluated. When performing this in the expressions section, I would presume it would get re-evaluated each time a referenced variable changed. So, I have it now using an alpha of 0.5 and re-evaluating every 2 hours. So a moving average based upon that. Should smooth out the values decently (I hope) to have a less fluctuating temperature range during the day. I of course didnt think to encapsulate the expresession in curly braces, and was instead doing something like this… {variable1} - {variable2} instead of {variable1 - variable2}. Makes sense though. I have seen similar implementation with network appliances where you need to encapsulate TCL in the same manner, otherwise the data is treated as a string. Presume if you really want to use a { somewhere in a string, you would have to escape it? Would \ { work for instance? or a double {{ ?

@rigpapa

I am thinking of further advancing my idea of keeping track of outside temperature. Stumbled upon the following thread: HOW-TO: Time Series - #22 by LibraSun, but alas it is locked. I see one can create an array object using push() and set the 3rd parameter to control its length and shorten it ‘from the left’, taking out the oldest value. Now… is there a way to reference a particular element and extract the value? Clearly, I tried the object[0] method with index as 0 but that didnt exactly work.

1 Like

Wow, I had forgotten about that old thread, but glad you found it.

If your series expression is a non-empty array, then certainly series[0] ought to return the value of the first element. Not sure what’s tripping you up on that.

1 Like

SOB! Apparently the implementation used here uses an index starting at 1, doh! So, if there is only one element in an object created by variable = push(variable, “somevalue”), the way to access this would be variable[1]. Doesnt start at 0.

Oh, that’s right. I’ve been using MSR (which has zero-based arrays) for so long now that I forgot some of the protocols in old Reactor (which uses 1-based lists).

1 Like