I’m having issues getting my script working on my new openhab installation on my RaspberryPi 3+.
Below script works fine on my 2.3 installation running on a win laptop.
import org.openhab.core.library.types
import org.openhab.model.script.actions
import java.lang.Math
import java.text.SimpleDateFormat
import java.lang.String
import java.util
//import java.lang.Object
//import java.util.Calendar
//import java.util.TimeZone
rule "Day high and low"
when
Item Ute_Temperature received update
then
var Number Min
var Number Max
var String tmp
var SimpleDateFormat df = new SimpleDateFormat( "HH:mm" )
if (Ute_Temperature.state instanceof DecimalType) {
Min = (Ute_Temperature.minimumSince(now.toDateMidnight() , "jdbc").state as DecimalType)
tmp = (Math::round(Min.floatValue*10.0)/10.0) + " °C (" + df.format(Ute_Temperature.minimumSince(now.toDateMidnight() , "jdbc").timestamp) + " )"
postUpdate(DayTempLow, tmp)
Max = Ute_Temperature.maximumSince(now.toDateMidnight() , "jdbc").state as DecimalType
//df = new SimpleDateFormat( "HH:mm" )
tmp = (Math::round(Max.floatValue*10.0)/10.0) + " °C (" + df.format(Ute_Temperature.maximumSince(now.toDateMidnight() , "jdbc").timestamp) + ")"
postUpdate(DayTempHigh, tmp)
}
end
when I run smarthome:status DayTempHigh I get the response
> openhab> smarthome:status DayTempHigh
> 2.6 °C (13:19)
On my Rasberry Pi on the other hand, the same script doenst work for some reason (OpenHab 2.5.2)
openhab> smarthome:status DayTempHigh
NULL
When I check the event log, it says it’s been updated.
$ cat /var/log/openhab2/events.log | grep WeekTempHigh
2020-03-22 23:27:25.769 [ome.event.ItemUpdatedEvent] - Item 'WeekTempHigh' has been updated.
The item “WeekTempHigh” is configured in the paper UI and is of type “String”
You should not import anything from org.openhab in .rules files. This has been the case since OH 2.0 came out which implies this code is OH 1.x Rules code.
Per the docs, you should use DayTempHigh.postUpdate(... as opposed to postUpdate(DayTempHigh,....
WeekTempHigh does not appear in that Rule. And you’ve looked at the state of DayTempHigh, not WeekTempHigh.
And in a case like this you want to look in openhab.log for errors. You need to add logging to this Rule to see when/if it runs and what important values are set to when it runs.
It’s impossible to say what is wrong with this Rule without a lot more information but if I had to guess, given the minimal information provided, I’d say that Ute_Temperature’s state is NULL and therefore not an instance of DecimalType so the Rule does nothing. It’s also possible that minimumSince and maximumSince are returning null because there isn’t anything in the database. But without the errors from openhab.log, it’s all guessing.
Thanks for your help! I’m not a programmer so code debugging isn’t my core
However, with your input I got it to work by changing “instanceof DecimalType” to “instanceof QuantityType”.
When I used:
DayTempHigh.postUpdate(DayTempHigh, tmp)
i got the error message
2020-04-02 22:26:19.949 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Day high and low': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(org.eclipse.smarthome.core.items.Item,org.eclipse.smarthome
.core.types.State) on instance: null
so I changed back to the original syntax without the DayTempHigh-prefix and it worked fine. Can you please provide the full syntax of your proposal and briefly explain why that is better?