# Weird calculated values in rule

Hello, I have rule to calculate daily monthly and yearly energy consumtion.
I read value once a day from my energy meter.

Everything is ok but when I calculate difference between two values it gives me strange numbers.

here is rule:

``````rule "zuzycie energa"
when
then
var Number licznik_energa_dzis1
var Number licznik_energa_miesiac1
var Number licznik_energa_rok1

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1).withDayOfYear(1)) === null) { var licznik_energa_rok1 = 0 } else {
licznik_energa_rok1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfYear(1)).state as Number }

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)) === null) { var licznik_energa_miesiac1 = 0 } else {
licznik_energa_miesiac1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)).state as Number }

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)) === null) { var licznik_energa_dzis1 = 0 } else {
licznik_energa_dzis1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)).state as Number }

val licznik_energa_dzis3 = licznik_energa_obecnie1 - licznik_energa_dzis1
val licznik_energa_miesiac3 = licznik_energa_obecnie1 - licznik_energa_miesiac1
val licznik_energa_rok3 = licznik_energa_obecnie1 - licznik_energa_rok1

postUpdate(licznik_energa_dzis, licznik_energa_dzis3)
postUpdate(licznik_energa_miesiac, licznik_energa_miesiac3)
postUpdate(licznik_energa_rok, licznik_energa_rok3)

end
``````

and here are values that I get:

Im reading this data since march 2022 so usage should be about 1000kWh and it gives me 52 859 599 299 ???

I don’t think that’s a Number, it’s a Quantity (with units).
See

That looks like a problem too; here a new variable named `licznik_energa_rok1` is declared and created (regardless of if any other variable already exists with that name), it is set to zero, but then discarded at the end of the code block, the closing }.
So it doesn’t do anything useful at all.

ok, so I changed my rule to:

``````rule "zuzycie energa"
when
then

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1).withDayOfYear(1)) === null) { val licznik_energa_rok1 = 0 } else {
licznik_energa_rok1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfYear(1)).state as QuantityType<Number> }

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)) === null) { val licznik_energa_miesiac1 = 0 } else {
licznik_energa_miesiac1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)).state as QuantityType<Number> }

if (LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)) === null) { val licznik_energa_dzis1 = 0 } else {
licznik_energa_dzis1 = LicznikEnerga_EnergaZuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)).state as QuantityType<Number> }

val licznik_energa_obecnie1 = LicznikEnerga_EnergaZuzycie.state as QuantityType<Number>
val licznik_energa_dzis3 = licznik_energa_obecnie1 - licznik_energa_dzis1
val licznik_energa_miesiac3 = licznik_energa_obecnie1 - licznik_energa_miesiac1
val licznik_energa_rok3 = licznik_energa_obecnie1 - licznik_energa_rok1

postUpdate(licznik_energa_obecnie, licznik_energa_obecnie1)
postUpdate(licznik_energa_dzis, licznik_energa_dzis3)
postUpdate(licznik_energa_miesiac, licznik_energa_miesiac3)
postUpdate(licznik_energa_rok, licznik_energa_rok3)

end
``````

and now I got error:

2022-10-23 15:07:52.657 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘energa-1’ failed: Could not cast 14300.87 to org.openhab.core.library.types.QuantityType; line 10, column 27, length 134 in energa

I don’t think I’ve ever seen that usage.
If the state is a Quantity, it shouldn’t need to be cast as a Quantity.

That’s still a do-nothing, the variable being created inside the { } block, and discarded immediately.