MQTT / JSONPath -> change read value from mWh to kWh

Hi,

I’m currently working on getting the PowerLast1 Attribute from my Shelly Plus Plug S via MQTT, because this is not (yet) supported by the shelly binding. Meanwhile I have a working thing file which delivers the correct value, but my issue is, that the value is in mWh and I would like to have it in kWh in the item. I’m not the JSONPath pro, so I searched for nearly two hours now, but could not find a solution. Hopefully somebody here is able to help me…

JSON string:

{
	"src": "shellyplusplugs",
	"dst": "shelly/tvwohnzimmerunten/events",
	"method": "NotifyStatus",
	"params": {
		"ts": 1703939820.14,
		"switch:0": {
			"id": 0,
			"aenergy": {
				"by_minute": [
					205.104,
					204.233,
					204.233
				],
				"minute_ts": 1703939819,
				"total": 2517.232
			}
		}
	}
}

Thing file:

    Thing topic tvwohnzimmerunten "TV Wohnzimmer Unten" @ "Erdgeschoss" {
    Channels:
        Type number : lastpower1 [ stateTopic="shelly/tvwohnzimmerunten/events/rpc", transformationPattern="REGEX:(.*aenergy.*)∩JSONPATH:$.params.switch:0.aenergy.by_minute[0]"
} 

lastpower1 includes values like “205.104” in mWh. I would like to habe “0.000205104” instead, because I summarize that in a allpowerusage item. I know I could change the unit also in the rule which is doing the calculation but all other lastpower1 value delivered by the shelly binding are also in kwh and I would like to have that the same.

Thanks!

When it’s openHAB later than 3.3, just use QuantityType:

Thing topic tvwohnzimmerunten "TV Wohnzimmer Unten" @ "Erdgeschoss" {
    Channels:
        Type number : lastpower1 [ stateTopic="shelly/tvwohnzimmerunten/events/rpc", transformationPattern="REGEX:(.*aenergy.*)∩JSONPATH:$.params.switch:0.aenergy.by_minute[0]", unit="mWh"
} 

Then use Number:Energy Item and set up the state Description to use kWh.
If using openHAB4.0 or neweeer, please be aware that you also have to setup the Item metadata unit to something meaningful, like “Wh” or also “kWh”. unit metadata is how the status of the Item is set, and in consequence it’s the unit which will be used in persistence (charts…). You shall not change the Item metadata unit later, because it will mess up your charts (or at least not be surprised at all…)

Hi Udo,

Thanks for that idea. I’m using the following rules to generate the overall value:

Shelly_TV_Wohnzimmer_Unten_Strom_PowerAll.postUpdate((Shelly_TV_Wohnzimmer_Unten_Strom_PowerAll.state as DecimalType).floatValue() + ((Shelly_TV_Wohnzimmer_Unten_Strom_LastPower1.state as DecimalType).floatValue()/60/1000/1000))

If doing like that, I can’t change that in future. Right? So what I get this way is a “205.104” also if I change give OH the unit as “mWh”?

The point is, if setting a unit this will influence which value is persisted. Now if reading old values from persistence, the persistence service does not know about Units of Measurent, so it simply will give the value “as is” and openHAB will chose the unit. Now if you’re changing the unit after persisting data, the persisted data will be calculated wrong. But that is also true for a calculation via rules. You can’t change the way of your calculation without losing old data.
But this is not true for the display state. You get the value as mWh, set the input unit (at the channel) to mWh. You want the value to be stored as Wh, set the item unit to Wh (but never change it after initial change)
You want to get the value displayed as kWh, set the state Description pattern to kWh. You want MWh, set it to MWh, you want Deca Watt hours, set it to daWh, you want to change that on a daily basis, just do it :slight_smile: but never change channel unit or Item unit (after once setting it to the correct unit, of course).

1 Like