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'
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.
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)
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?