Persistance query of a QuantityType item in DSL rule (oh3)

Hello again,

During my move to oh3 (from oh2.5) I started to use Unit of Measuremnts for my items.
Now I have some problems to query and post values inside DSL rules in oh3. The following problem:

I have two items, configuered with the ui:

Number:Energy Gaszaehler_StandkWh (Dimension in kWh) “gas counter”
Number:Energy Gaszaehler_VerbrauchkWh_1T_aktuell (kWh) “actual daily gas consumption”

and I want to use this rule (old rule from oh2.5) to calculate every 15 min my actaul daily gas consumption with the help of my persistance service (influxdb). My persistance service is running without any issues.

var Number temp2g = Gaszaehler_StandkWh.deltaSince(now.minusHours(calc1),“influxdb”) as Number
postUpdate(Gaszaehler_VerbrauchkWh_1T_aktuell, temp2g)
logInfo(“rule”, “Der Gasverbrauch des aktuellen Tages wurde berechnet und beträgt: " +String.format(”%.2f",(temp2g).floatValue())+ " kWh")

My problem is that I get a false value back. I guess that the queried number is in an other unit of Number:energy and I need kWh, but I have no idea how I have to “convert” it. The documantation section is not very helpful for persistance and UoM for rules.
I hope somebody can point me in the right direction.

Thank you!

I don’t know how persistence works with UoM so if there is something going on there I can’t really help much. But if you just log out the Item’s state without any conversions to floatValue or the like. That will tell you the default units which is very likely the units used when storing the value.

From there the question is to figure out what deltaSince returns. Looking at the JavaDocs it looks like it returns a DecimalType which, unfortunately does not include the units. So once you are armed with the units that are being stored and returned by the database, you will have to do the math to convert it yourself.

1 Like

Thank You @rlkoshak ,

I changed the gas counter item back to only number and so I don’t have to calculate inside the rules.


I use the following in the rule in order to get the power consumption of today. However, I notice that the wrong value have been read from the influxdb. I checked the value via InfluxDB Studio.

val  ZonedDateTime  zdt =
var  ZonedDateTime  start_of_day = zdt.toLocalDate().atStartOfDay(zdt.getZone())
var Number energyToday = SZ_Wirkenergie_total_Wh.deltaSince( start_of_day, "influxdb" )

I posted the values in the log

2021-01-16 02:22:10.447 [INFO ] [org.openhab.core.model.script.Strom ] - Start of day: 1325
2021-01-16 02:22:20.439 [INFO ] [org.openhab.core.model.script.Strom ] - Current value: 13817621

Start of day value should be 13816850, therefore the calculated power consumption value is not correct. Is there any conversion when reading values from InfluxDB? Someone else has the same experience?

Best Regards,