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.
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.