[SOLVED] Does openhab2 not support % (remainder) operator?

Trying to nicely format a time value using the % operator, but both smarthome designer as openhab2 does not appear to recognise the % operator. Openhab reports : An error occurred during the script execution: The name ’ % ’ cannot be resolved to an item or type Do I need to import a library ? if so which one.

Code extract:
var uptime = rasp_uptime.state as DecimalType
// var Number uptime = 5000
var mins = uptime % 60
var Number hours = (uptime - mins) % 60
var Number days = (uptime - mins - (60 * hours))

Seems to be working for me in a rule file.

rule modulus
when
  Time cron "5 * * * * ?"
then
  println( 65 % 60 )
end
1 Like

It may only be supported with primitives. Try calling toInt() on the DecimalType and do your math with ints instead of Numbers.

Thanks for the suggestion, but toInt() is not accepted either. However found an alternative solution (in a roundabout way):

        val Number ctime = (Rasp_uptime.state as DecimalType)
	val days = ctime / (24*60)
	val hrs = (ctime - (days.intValue()*24*60)) / 60
	val mins = (ctime - (days.intValue()*24*60) - (hrs.intValue()*60))
	logInfo("rasp","days="+days.intValue()+", hours="+hrs.intValue()+", mins="+mins)

Note that in openhab1 ‘val days = ctime /(24*60)’ would have resulted in an integer (have been caught by that on a couple of occassions) , however that is not the case in openhab 2, hence the use of intValue(). It gave me what I wanted.

A bit old, but for everyone stepping into it. I just run into it. The proposed solution only works if you know the bigger possible item. Example mins to seconds or month to day. Simpler ist following way with integer arithmetic: find remainder without using modulo or % operator

This works without knowing or taking care about the next bigger value type.

Applying intValue() to DecimalType yields an int upon which % modulo is applicable, as in:

var number = (receivedCommand as DecimalType).intValue()
var modulo = number % 10
1 Like