Problem with TempTrend Script/Rule

Hi there

I want to use in my openHAB 3.2.0 (on RASPI 3) environment the following Script/Rule, to display the ‘Temperature Trend’ in my Habpanel, but if i run the rule the system get’s extremly slow and things began to go offline and online as i can see in the log file.

This what i want to do:
trend

This is the Script/Rule that calculate average of last 1h - first 30 mins compared to last 30 minutes of the hour - and based on that posts an update that i use:

  • 1 if temperature is raising,
  • 0 if temperature did not change,
  • -1 if temperature is falling.
val updateDiff = [ GenericItem sourceItem, GenericItem item, double limit, String name, int offset |

  val n = 30

  var nn = 0
  var double avgPre = 0
  var double avg = 0

  var i = 2 * n + 1
  while(( i = i - 1 ) >= n) {
    val historicState = sourceItem.historicState(now.minusMinutes(i + offset))
    if (historicState !== null) {
      var y = (historicState.state as DecimalType).doubleValue
      avgPre = avgPre + y
      nn = nn + 1
    }
  }

  avgPre = avgPre / nn

  nn = 0
  i = n + 1
  while(( i = i - 1 ) >= 0) {
    val historicState = sourceItem.historicState(now.minusMinutes(i + offset))
    if (historicState !== null) {
      var y = (historicState.state as DecimalType).doubleValue
      avg = avg + y
      nn = nn + 1
    }
  }

  avg = avg / nn
  val diff = avg - avgPre

  //logInfo("TTM", "result {}: {} <- {} vs {}", name, diff, avgPre, avg)

  if (diff > limit) {
    item.postUpdate(1)
  } else if (diff >= -limit) {
    item.postUpdate(0)
  } else {
    item.postUpdate(-1)
  }

]


rule "Temperature Trend Monitor"
when
    Time cron "1 * * * * ?"
then
  updateDiff.apply(Outside_ds18b20_temperature, Outside_ds18b20_temperature_slope, 0.35, "Outdoor", 0)
  updateDiff.apply(FF_Nik_Room_temperature, FF_Nik_Room_temperature_slope, 0.10, "Nik's Room", 0)
  updateDiff.apply(FF_Bed_Room_temperature, FF_Bed_Room_temperature_slope, 0.10, "Eva's Room", 0)
end

and this is what i get in the log-file when the script is running:

as i told, the system begans to get extremley slow, so i have to disable the script/rule and reboot the system.

Is there abetter way to to do, what i want to do, or a better script/rule to do this?

Put a logInfo() at the beginning and end of your rule, you’ll be able to see how long it takes.

Maths with primitives (Integer,double) in DSL rules is pretty pants for efficiency. You’ve got multiple database accesses too. I wouldn’t like to say which is the bigger problem, but I’m not surprised running this rule every minute presents a noticeable load.

I haven’t looked in detail to see if it can be improved yet, but let’s get a benchmark first for trial and error attempts.

EDIT - you just want a binary up/down trend? I think you need just two persistence calls for that; averageSince (-1hr), averageSince(-0.5hr)… Compare and look at the sign,
Should save 170 odd database accesses per minute.

I tried your advice, but the rule slow my system down, so i cannot do everything until i’ll wait a long, long time.
So i have canceled the whole thing, i thought it is easier to implement the Script/Rule, but i think that this Script/Rule never has worked well in a system.

Usually i don’t give up so fast, but my System runs at the moment very well (without these Script/Rule), so i have canceled this.

Or you have a better Idea/Solution.

Thank you

like

1 Like

Ah,Ok I thik I understand what you mean, but don’t know exactely how can I do that.

Can you explain what I have to do to get the average and compare that