stateTransformation depending on another channel

Hi,

I’m requesting state from an external device via an HTTP JSON API and use the HTTP binding.

One channel looks like this:
Type number : chargingPower "chargingPower" [ stateExtension="state", stateTransformation="JSONPATH:$.charging_power", unit="W", mode="READONLY" ]

The device’s API has an issue though which is that the value is not always updated when charging stops. So while no charging is performed the charging_power still shows the last positive value which is highly confusing and breaks my metrics dashboard.

There I’m checking the options I have to “correct” the value and trying to understand if it’s already possible on the Thing level somehow.
The most logical would be to combine the channel with the information from another one:

Type string : state "state" [ stateExtension="state", stateTransformation="JSONPATH:$.state", mode="READONLY" ]

If the above is not “CHARGING” the channel should always provide 0 as a value and only use the API value when state matches CHARGING.
Is there any way to achieve this?

Fallback plan would be to create a (virtual) additional Item which is updated via rule according to the above logic but I’m wondering if there is a solution closer to the origin?

This is an interesting topic. So the api after it finished charging it send the last value without any error or timeouts nothing ?

Yes, that is the behavior I see. And I actually consider it a bug in the device but the likeliness of it being fixed by the vendor is low so I’m looking for a workaround.
It doesn’t happen always. Could be a timing issue on the device side but it happens quite often.

Then from my knowledge you have the proxy item and rule or don’t use the http binding and use JavaScript to get status process do the logic and then spit out the result based on the workaround you found.

Can you post what device are you talking about? Maybe someone on the forums has already a workaround.

It’s a dezony IQ wallbox and since that vendor is new and small I somewhat guess nobody else has experience (yet).

Do you have a pointer on how to implement a Thing via Javascript?
No experience with that so most likely I’ll be doing the proxy Item.

Just as a reference this is how my current OH integration looks like with the wallbox:
(only german though)
https://forum.dezony.de/t/openhab-integration/34

And here my (german) “bugreport” about the API issue:
https://forum.dezony.de/t/api-charging-power/39

I had a similar challenge with my Solar Inverter (Growatt, connected via “grott” on MQTT). When the sun goes down it stops reporting and always shows around 6 Watt still being delivered whereas it actually consumes power from the grid to keep itself running.
My solution was to add modBus energy meter showing the actual import/export.
In the end i switch of the inverter when it does not deliver any power and switch it on again in the morning based on the astro binding.
I put a separate energy meter in front of my BEV Charger, so I have an independent way to record the consumption.
Works like a charm and you can see the power consumption / delivery without OH as well.

This can be easily done using profile.

Example here using Ruby transformation profile, but you can use JS if preferred. Note: I haven’t tested this.

String ChargingState { channel="xxxxx:state" }

Number:Power ChargingPower { channel="xxxxxxx:chargingPower" [ profile="transform:RB", toItemScript="| ChargingState.state == 'CHARGING' ? input : 0" ] }
1 Like

That proposal seems to work fine for me. Thanks.

I have to correct myself. It’s not totally working apparently.

My item looks like this now
Number:Power wallbox_current_actual "Ladeleistung [%.1f W]" (Energy, gDezony) ["Measurement", "Power"] { channel="http:url:dezonyIQ:chargingPower" [ profile="transform:RB", toItemScript="| wallbox_state.state == 'CHARGING' ? input : 0" ] }

Here is the event log which shows that the items are still updated (maybe in a certain condition):

2023-09-15 07:15:21.064 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-15 07:15:31.058 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 0 W
2023-09-15 07:15:31.064 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 0 W
2023-09-15 07:15:31.068 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-15 07:15:41.057 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 0 W
2023-09-15 07:15:41.062 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 0 W
2023-09-15 07:15:41.066 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-15 07:15:51.060 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 5 W
2023-09-15 07:15:51.061 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_current_actual' changed from 0 W to 5 W
2023-09-15 07:15:51.068 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 5 W
2023-09-15 07:15:51.072 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING
2023-09-15 07:15:51.074 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_state' changed from CHARGING_FINISHED to CHARGING
2023-09-15 07:16:01.061 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1650 W
2023-09-15 07:16:01.065 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_current_actual' changed from 5 W to 1650 W
2023-09-15 07:16:01.068 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1650 W
2023-09-15 07:16:01.072 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING
2023-09-15 07:16:11.065 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 932 W
2023-09-15 07:16:11.067 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_current_actual' changed from 1650 W to 932 W
2023-09-15 07:16:11.071 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 932 W
2023-09-15 07:16:11.074 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING
2023-09-15 07:16:21.061 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1182 W
2023-09-15 07:16:21.064 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_current_actual' changed from 932 W to 1182 W
2023-09-15 07:16:21.068 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1182 W
2023-09-15 07:16:21.071 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to IDLE
2023-09-15 07:16:21.074 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'wallbox_state' changed from CHARGING to IDLE
2023-09-15 07:16:31.063 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1182 W
2023-09-15 07:16:31.070 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1182 W
2023-09-15 07:16:31.073 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to IDLE

OK to make the item not updated at all, change your item to:

Number:Power wallbox_current_actual "Ladeleistung [%.1f W]" (Energy, gDezony) ["Measurement", "Power"] { channel="http:url:dezonyIQ:chargingPower" [ profile="transform:RB", toItemScript="| input if wallbox_state.state == 'CHARGING'" ] }

This will return java null (not to be confused with NULL from UnDefType) when wallbox_state is not CHARGING, which will make the state not passed on to the item, as if there was no update.

Tried this latest proposal and it makes sense to me. But the previous version also made sense to me and the only difference according to your explanation would be 0 vs null. But the 0 didn’t work and the null also does not work apparently. After changing it I still get the following events on Thing updates:

2023-09-16 09:02:19.300 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:02:29.278 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:29.282 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:29.285 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:02:39.283 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:39.289 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:39.293 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:02:49.289 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:49.295 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:49.299 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:02:59.286 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:59.294 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:02:59.296 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:03:09.286 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:09.292 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:09.297 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:03:19.294 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:19.302 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:19.305 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:03:29.287 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:29.296 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:29.299 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED
2023-09-16 09:03:39.290 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:39.295 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_current_actual' updated to 1751 W
2023-09-16 09:03:39.298 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'wallbox_state' updated to CHARGING_FINISHED

Any further ideas?

No, when the transform script returns null, there will be no state update at all, not even to NULL / 0. Nothing.

But according to your log, it is still updating, so try either:

  • Comment out the item, save the .items file (if there are other items in the same file), then uncomment it, and save again.
  • If that doesn’t work, try restarting openhab.

Aha, removing/adding the item didn’t change anything but restarting openhab did:
No ProfileFactory found which supports profile 'transform:RB' for link 'wallbox_current_actual -> http:url:dezonyIQ:chargingPower'

As I didn’t use scripting like that so far I didn’t know I have to install some plugin but I assume it’s the JRuby Scripting?

Yes you do need to install jruby scripting automation addon. I guess it never worked all this time if you hadn’t installed it.

Yes, that was the problem from the beginning then. Sorry for the confusion.
I went back to your first proposal because the other would likely lead to the same issue because once the state switches away from CHARGING it would not update the wrong value to 0 compared to fetching the wrong value from the API. In the end the data would still be wrong.
I will watch out if there still is a problem but I’m pretty confident that it works now.