# How to calculate with Unit?

Hello dear community,
I would like to add an offset with 30.1 kWh to a power energy item in the rule. How do I do this?
Here is my attempt:

``````logInfo("elmeter.rules SolarProd",SolarProd.toString)
SolarProd = SolarProd + 30.1|kWh
``````

``````2024-07-29 20:32:19.561 [INFO ] [model.script.elmeter.rules SolarProd] - 14618.594 kWh
2024-07-29 20:32:19.567 [INFO ] [model.script.elmeter.rules SolarProd] - 52735298400
``````

But after the calculation, the value is extremely high.
I’m on OH4.2

What is SolarProd? How was it defined as a variable and how was it populated?

The docs for Rules DSL indicate that the units sometimes need to be put in quotes: `30.1|"kWh"`.

The new value has no units so it’s really unclear what’s going on here. We need more details.

Here more Details
in rules:

``````val Number SolarProd = (SE_day_production.sumSince(now.withYear(2020).withMonth(5).withDayOfMonth(16).withHour(0).withMinute(0).withSecond(0)) as Number)
SolarProd = SolarProd + 30.1|kWh
``````

And the item:

``````Number:Energy           SE_day_production    "Tages Produktion [%.2f kWh]"                                  {channel="solaredge:generic:c6c9f513:aggregate_day#production"}
``````

That’s your problem. You are telling Rules DSL that it’s a `Number`, not a `QuantityType`. So when you do the calculation Rules DSL converts the result to a `Number`.

It’s yet another reason why forcing the type of variables in Rules DSL is not a good idea. Strange things happen when you do that.

So it works better if you don’t force it.

Also, you’ve defined it as a `val` which means it’s a constant. You can’t reassign it later. You need to use `var`.

``````var SolarProd =  SE_day_production.sumSince(now.withYear(2020).withMonth(5).withDayOfMonth(16).withHour(0).withMinute(0).withSecond(0))
SolarProd = SolarProd + 30.1|'kWh"
``````

This crappy type system is one of the many reasons I recommend against doing new development of rules using Rules DSL. Any of the other options are going to be just as easy to code in but avoid all the spooky magic that Rules DSL introduces with it’s pseudo-typeless system which only works part of the time.

In JS Scripting the code above would be:

``````var SolarProd = items.SE_day_production.persistence.sumSince("2020-05-16T00:00:00").quantityState;
console.info(SolarProd);
console.info(SolarProd);
``````

In Blockly it would be

I got this:

``````failed: The name 'items' cannot be resolved to an item or type;
``````

There are three examples in my reply in three separate rules languages. The first is Rules DSL. The second is JS Scripting and the third is Blockly. JS Scripting and Blockly require the JS Scripting add-on to be installed and both have a significantly different syntax from Rules DSL (they don’t go in .rules files for one).

See JavaScript Scripting - Automation | openHAB for everything you need to know to create a rule using JS Scripting either through the UI or in a .js file.

See Rules Blockly | openHAB for everything you need to know about Blockly. Blockly can only be use in the UI.

Or use my first example which is in Rules DSL same as your original code.

Maybe it’s time to retire rules DSL?
I stopped using it when I went to oh3.

And in JRuby

``````solar_prod = SE_day_production.sum_since(Time.parse("2020-05-16"))
logger.info(solar_prod)
solar_prod += 30.1 | "kWh"
logger.info(solar_prod)
``````

Far too many people still use it. Eventually I do see it becoming deprecated but I don’t see it going away until something breaks.