Many thanks for your help everyone!
Now I know how to avoid this in the future
Yes, of biblical proportions!
In the current version with all the suggestions from this thread it runs in about 30-60 ms, quite the improvement from the minutes before. So based on my very rough timing, 2500-5000 times faster ^^
Thank you for suggesting Numbers!
I use this with floatValue, it introduces no noticable delay and results in quite clean code. Thank you!
setPoint = Math::round(<my decimal value>.floatValue)
This is kind of the key for me, I need to calculate with decimals but have an integer number to send to my device. I thought using floats everywhere was the way to go, but obviously not
Here is the current code, for reference.
Notice! This is not a finished rule, itβs simply the state in which it no longer have performance issues.
val logID = "math.rules"
val Number Y2 = 17
val Number Y1 = 1
val Number X2 = 100
val Number X1 = 0
var Number Xtrigger = 3.4
var Number oldAqi = -100
var Number setPoint = 300
// --------------- air quality changed ---------------
rule "air quality changed"
when
// Item aqiHome changed
Item Test_switch_4 received command ON
then
val newAqi = aqiHome.state as Number
if (Math::abs((newAqi-oldAqi).floatValue) >= Xtrigger) {
logInfo(logID, "aqi changed more than: " + Xtrigger)
setPoint = Math::round((((Y2-Y1)/(X2-X1)) * newAqi + Y2 - ((Y2-Y1)/(X2-X1)) * X2).floatValue)
// purifier_1_favoritelevel.sendCommand(setPoint)
logInfo(logID, "purifier setPoint: " + setPoint.toString)
}
end
Log
2019-01-11 13:00:56.245 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:7c49ebb3ac4c:158d0002c5e942:button triggered SHORT_PRESSED
2019-01-11 13:00:56.259 [ome.event.ItemCommandEvent] - Item 'Test_switch_4' received command ON
==> /var/log/openhab2/openhab.log <==
2019-01-11 13:00:56.273 [INFO ] [se.smarthome.model.script.math.rules] - aqi changed more than: 3.4
2019-01-11 13:00:56.290 [INFO ] [se.smarthome.model.script.math.rules] - purifier setPoint: 1
==> /var/log/openhab2/events.log <==
2019-01-11 13:01:07.928 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:7c49ebb3ac4c:158d0002c5e942:button triggered SHORT_PRESSED
2019-01-11 13:01:07.950 [ome.event.ItemCommandEvent] - Item 'Test_switch_4' received command ON
==> /var/log/openhab2/openhab.log <==
2019-01-11 13:01:07.978 [INFO ] [se.smarthome.model.script.math.rules] - aqi changed more than: 3.4
2019-01-11 13:01:08.005 [INFO ] [se.smarthome.model.script.math.rules] - purifier setPoint: 1