Not able to use QuantityType<Time> in Rules?

Hi, looks like I cannot define a constant in type QuantityType in rules, looks like only affects Time, I can create QuantityType w/o problem. Below is the code:

val QuantityType<Time> continuousUsageThreshold = 60|min

And the error:

2023-08-13 16:00:37.983 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'WaterMeter.rules' has errors, therefore ignoring it: [1,8]: no viable alternative at input 'Time'
[5,5]: no viable alternative at input 'QuantityType'

Any suggestion?

You have to import Time. But that’s kind of a bother so I recommend using ? any time you must cast the state of an Item to a QuantityType (e.g. as QuantityType<?>). And if you don’t need to, in Rules DSL never force the type of an Item.

val continuousUsageThreshold = 60|min

You only cause problems for yourself and increase the time it takes for the Rules DSL parser to load and parse your rules when you force the type like that.

1 Like

Thank you! This works great so I don’t need to define those timers as integers!
I’m calculating continous water usage every 3 minutes and would like to ask if there is a way to convert a Number to QuatityType based on persistence service:

var lastUsage = current_value.deltaSince(now.minusMinutes(3)) as Number

current_value.state is a QuantityType, however I can only cast the result of deltaSince to Number (it returns DecimalType I think). What do I need to convert this Number to QuantityType? I have tried to append |L to the variable and it doesn’t like that, so I assume |unit is only for constants.
Many thanks in advance!

With the changes to UoM handling in OH 4, it’s now feasible for the persistence actions to return values with units where it’s warranted. Perhaps in the near future that will be the case. But for now you’ll have to create a QuantityType manually from the number and you’ll just have to know what the unit is ahead of time.

How to convert from a Number or DecimalType to a QuantityType in rules? Would you mind sharing a line of code that does the conversion? Many thanks in advance!
I saw the API below, however not sure what to send in as Unit:

public QuantityType(Number value, javax.measure.Unit<T> unit)

I don’t really use Rules DSL any more. It’s way easier in the other languages.

But that’s a constructor so, as with all constructors:

val myWatts = new QuantityType(<some number>, 'W');

Anything in the UoM table showing the units symbols. The same values you use for the unit metadata.

Thank you! But looks like the constructor wants a Unit object:

2023-08-14 20:01:00.412 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '_Test-1' failed: An error occurred during the script execution: Could not invoke constructor: org.openhab.core.library.types.QuantityType.QuantityType(java.lang.Number,javax.measure.Unit) in _Test

Just out of curiosity, what language do you use for rules?

Oh, right, use the one that takes a String.

new QuantityType(<some number> + ' W')

JS Scripting in the UI.

1 Like