How to change unit of quantity types

Hello,
I am preparing on my Raspberry 4 (Openhabian 3.4.5) the update from 3.4.5 to 4.x and have unfortunately set some of the number items to “kWh” instead of “Wh” in the *.items files. As a result, some values were calculated incorrectly.

But now I have corrected the unit - nevertheless the unit remains in “kWh”.

How can I force the new unit in the system?
A restart of the openhab.service did not solve the problem.

old / wrong

    Number:Energy               IBBM_Consumed                         (IBB_MeterSmartM,IBB_MeterSmartM_Pers)              { unit="kWh",  channel="fronius:meter:eb46843650:39ac6a2986:energyrealsumconsumed" }
    Number:Energy               IBBM_NetReceive_DayBase           { unit="kWh" } 

current/correct

    Number:Energy               IBBM_Consumed                         (IBB_MeterSmartM,IBB_MeterSmartM_Pers)              { unit="Wh",  channel="fronius:meter:eb46843650:39ac6a2986:energyrealsumconsumed" }
    Number:Energy               IBBM_NetReceive_DayBase           { unit="Wh" }

result:

The values are persisted in a InfluxDB.

From the release blogpost, I read:

unit metadata that fixes the unit for a given Item. This unit is assumed if a value with no unit is sent to the Item and the same unit is used when data is persisted.

From that I understand your issue comes from wherever you get the power value from is a unitless source, and you tell it to assign Wh to this number, even if it is something else. So, rather you should assign the correct unit (probably kWh), and handle the conversion to Wh downstream, as display unit, and in the rule via something like .state.toUnit("Wh").doubleValue or so (not sure that casting syntax is correct, I am guessing from my understanding of the docs)

Unfortunately not.
The source provides unitless, but definitely represents Wh.
But I defined these values as kWh out of carelessness.

So 5000 Wh are interpreted as 5000 kWh !

Now I want to go back so that 5000 Wh are actually interpreted as 5 kWh.

If the data is coming from fronius binding, it provides the unit already.

@J-N-K is the summary at the top of this github issue still applies to the current state of the codebase? If that’s the case then it should be perfectly fine to define an item like this:

Number:Energy MyEnergy { channel="a_source_that_sends_Wh", unit="kWh" }

The value is still going to be correct, just converted to the specified unit, e.g. the source sends 5000 Wh, the item will hold the value 5 kWh and not 5000 kWh because the incoming unit is not stripped.

This would affect persistence though, right?

  • If the source sends 5000 Wh and no unit has been specified → persist the value 5000
  • If the source sends 5000 Wh and unit=“kWh” → persist the value 5.0

Ok, even if the binding brings the unit, the unit is currently wrong for the value.

Wh are interpreted as kWh.

How do I solve this ?

Edit:
For now, I am back to my previously created backup. Here you can see that the channel “Real Energy Consumed” does not provide a unit in the “smart meter” although the channels in the “inverter” have units:

Have you checked the state description in the MainUI?

Look to my previous answer.

It is as I have said:
The channel of the item “IBBM_Consumed” does not have a unit. I accidentally assigned the wrong unit in my configuration (kWh) and I can’t get back (Wh).

For example here is a channel from the inverter with a delivered unit

The correct documentation can be found here. The default unit for energy is kWh, so without setting unit a value of 5000 Wh is converted to 5 kWh for the item’s internal state and that is persisted (as 5). If you want to persist a value of 5000, change the item’s unit to Wh.

I understand that. But my problem is that openhab does not seem to change the unit once it has been set incorrectly.

kW is not a valid unit for energy, it is a unit for power. Valid units for energy are Wh, kWh, J and some more. You can’t convert an energy to kW.

Sorry, my mistake - of course I mean “Wh” and “kWh” - not “kW”

Did you restart? after the change?

yep, I did

… btw… I restarted the openhab service - not the whole raspi, but this should be enough (?)

EDIT:
But ok, as I said, I’ve restored the backup and let the system continue to run with it. I will correct all the units that do not correspond to the standard and then re-install the changes tomorrow.

Since IBBM_NetReceive_DayBase is not linked to a channel, it must be updated by some rule. I guess that’s where it goes wrong.