Actually, I think the issue is in the numbers you are receiving from the device. There is nothing in your code that would cause a negative number. So the negative number has to be coming from the sensor. Look in events.log to see what it was updated to when the result of the calculation goes down.
Log out each part of the calculation as well and make sure they make sense. But this is a super simple formula. There is nothing there that would cause a negative number.
That’s rather my point, the timing is outside of openHABs control. This does not seem like a good plan for a time sensitive application - integrating the lux over time. Don’t use it as the “tick” to drive your clockwork, just use it as a sensor.
Well, I went back to the basics and did some more research and tried a few things and found this to be just as simple as can be. Well, at least this is working…
Item
Number DLI_Daily_Counter "DLI Daily Counter [%.2f]"
Rule
rule "Count of the DLI Total"
when
Item racksensor1_DLI received update
then
DLI_Daily_Counter.postUpdate((DLI_Daily_Counter.state as Number) + (racksensor1_DLI.state as Number / 12))
end
and that’s all it took. Probably what you guys were trying to tell me but was just not registering. Some turkey and suddenly it made sense…
So, presumably this should work then? val Number DLI_Daily = 0
…
DLI_Daily = DLI_Daily + (racksensor1_DLI.state as Number / 12)
I am asking because I am struggling with a simlar issue, where I define a Number at top of the rule and run through loops incresing the value along the way.
That makes sense and would have been my guess.
However with my code below, it does not work with Food:
rule "analyze expenditure"
when
Item Fin_Chk received command OFF
then
logInfo("+++ FINANCE", "$$$ - Fin Check triggered.")
var Number Food = new DecimalType()
val results = executeCommandLine("cat /etc/openhab2/html/tmp/text.csv",5000)
val array1 = results.toString.split("\n")
for(var i=7; i<array1.length; i++) {
var String client = ""
var String money = ""
var Number amount = new DecimalType()
val myList = array1.get(i).toString.split(";")
// extract client and transaction amount
client = myList.get(3)
money = myList.get(7)
if(client.contains("xxx")) {
Food = Food + DecimalType.valueOf(money) //
logInfo("+++ FINANCE", "Food costs updated to: " + Food + " EUR")
}
}
}
logInfo("+++ FINANCE", "Cost items have been updated")
end