Sensor averages value - best practices

By way of example:
I would like to NOT turn on the lights when a cloud covers the sun for a few seconds and it dims for a while and only if it is dark for the last 3 minutes.

Can you share your experience on how to implement this best?

How to measure average values from the sensor, for example, from the last 5 minutes?

Is it possible to read historical data from the sensor, the ones that are presented in the graphs in the UI?

What is your experience and how to do it properly according to the art and idea of OH?

Do you use Rules in your openHAB setup? If so, what sort of rules?

Yes, if your Items are being persisted (and it sounds like they are because you can see historical charts), then there are inbuilt functions in many of the rule engines which can calculate the average value of that Item over the last n-minutes.

Yes, I do, many rules in my configuration.

If yes, how to use them to read historical values?

I know this is just an example, but in this particular scenario I would:

  • trigger a rule when there’s a sudden, significant drop in the sensor reading
  • wait three minutes, during which the rule would be cancelled if the sensor goes back above a certain threshold
  • turn on the light if three minutes are reached without cancellation

I don’t see a need to average the values, but I might be missing something.

You could also have the rule fire only in certain weather conditions.

This is not quite a good solution if the checking period is fixed (example, 3 minutes), because after this time you can shoot yourself in a state of exemplary cloudiness, and moment later will be sunny (inmy exaple) again.

I thought to add an item and there record the previous time and compare with current time, but if the change of the value from sensor of the state occurs in a short period of time, it will also not reflect the actual conditions, so in my opinion, only a few samples from a certain time interval would be the best approach…

Well yes, but you said three minutes, so I worked with three minutes. You will never know how long a random cloud will block the sun, so any fixed length of time is a guess.

What you actually care about in this scenario is if/when the cloud will stop blocking the sun, but data can’t be used to predict that. The next best thing is to choose a threshold after which openHAB should assume that the sun isn’t coming back soon (e.g. three minutes), which is what it sounded like you’re doing.

The weakness is that you’ll be sitting around in darkness for a period of time, because openHAB is waiting to see if it should turn on the lights.

I don’t do any of this. If it’s cloudy and the light level falls below a certain threshold, my lights turn on…and stay on. If the clouds go away, I turn them off manually. This ensures that I’m never sitting around in darkness, and that my lights aren’t constantly toggling on and off.

Again, I’m only speaking to your example, not to your larger question about averaging values.

Example Blockly snippet:

if ((items.getItem('luminance_item').persistence.averageSince(time.ZonedDateTime.now().minusMinutes(3), 'jdbc')?.numericState) < 123) {
  items.getItem('switch_item').sendCommand('ON');
}
1 Like

Wow! Thank you very much @hafniumzinc I will try to do this like in your example today. Have a nice Sunday.

Hello. Tests done. Positive of course. It works perfectly! Thank you so much once again!

1 Like