historicState() gives back "wrong" value

You’ve thrown away the Quantity property, effectively discarding the units.
So your result has no units either when you try to update an Item that demands units.

Work with the units, not against them. If you log out the values of your intermediate variables you will get a better idea of what’s going on.

var accTotal1 = Shelly_EM3_accTotal.historicState(firstDayPrevMonth).state as QuantityType<Energy>

That will give you accTotal1 as a Quantity type with units.
It might be in joules or kWh or Wh, who cares - so long as you sum it with another Quantity, openHAB will take care of matching/scaling the units. So you can add kWh to Wh without thinking about it.
The result will be a Quantity as well.

More exploration here

1 Like