eclipse-archived / smarthome

Eclipse SmartHome™ project
https://www.eclipse.org/smarthome/
Eclipse Public License 2.0
862 stars 783 forks source link

[ArithmeticGroupFunction] Avg does not properly calculate Number:Angle Items #6792

Open 5iver opened 5 years ago

5iver commented 5 years ago

I came across this while setting up groups to calculate daily forecasts for OWM using a free API key, so @cweitkamp may be enticed to look into this one.

The Avg group function calculates the average of angles incorrectly. This Wikipedia article explains, and has a formula that could be used.

https://github.com/eclipse/smarthome/blob/master/bundles/core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/library/types/ArithmeticGroupFunction.java#L216

cweitkamp commented 5 years ago

Thanks for reporting. Why don't you submit a PR on your own? That will be appreciated.

5iver commented 5 years ago

My hands are a bit full ATM, but I plan to get one together if nobody gets it in before I free up.

5iver commented 5 years ago

As a workaround, I removed the group function and I'm manually updating the group state using the following algorithm:

windDirectionItemStates = map(lambda item: item.state.intValue(), ir.getItem("gForecast_WindDirection_" + str(index)).getMembers())
windDirectionAvg = reduce(lambda x, y: (((x + y) / 2) if y - x < 180 else (x + y + 360) / 2) % 360, windDirectionItemStates)
events.postUpdate("gForecast_WindDirection_" + str(index), str(windDirectionAvg))