[Solved] - Too stupid for item transformation

Hi all,

I am on OH 3.4.2 final build (please forgive but I had no time yet to migrate to OH4) and I have added a sensor for reading my power meter data and I receive the data via MQTT.

Everything works but I like to transform the data from Watts to KiloWatts by dividing the incoming value by 1000. I found the following way as the shortest and smartest but it does not work. I also found several other ways in similar topics in the community and I tried them all without success.

Number   EBZpow_used   "Stromzähler: Strom - verbraucht [%f]"  (gEBZ) { channel="mqtt:topic:bla:tasmota:power_used" [profile="transform:JS", toItemScript="| Quantity(input).divide(1000)"] }

But the item state is still:


and not


I also tried a javascript based script (which I have stored in the scripts folder)

(function(data) {
    var div = 1000;
    var MyData = (parseFloat(data) / parseFloat(div));
    return MyData.toString();

I even tried

(function(data) {
    var MyData = 17;
    return MyData.toString();

with the item definition:

Number   EBZpow_used   "Stromzähler: Strom - verbraucht [%.2f]"  (gEBZ) { channel="mqtt:topic:mySecureBroker:Stromzaehler:power_used" [profile="transform:JS", toItemScript="w2kw.js"] }

where w2kw.js is also stored in the scripts directory

But this did not change the item state to 17?

Can please somebody be so kind and tell me what I am doing wrong?

Thank you!


Have you installed js transform?

Yes, I have it installed via addons.cfg

Is there another way to confirm with a console command?

You can observe in Frontail when saving transormation to see if any error is reported

Don’t bother. Make the Item a Number:Power, set the “Unit of Measurement” property (near the bottom after clicking “Show Advanced”) to W. Then on your Item set the State Description Pattern to %.0f kW. OH will convert between them automatically for you. There’s no need for a transformation to convert between units.

In OH 4 use the unit metadata.

The transform will not apply until the next update.

If you’ve not set the “Unit of Measurement” property on the Channel, then input doesn’t have units and therefore is not a Quantity. I think it’s possible to create a Quantity without units but I think it will still add something. This might be a problem.

Transformations go in the transformations folder, not the scripts folder.

div us already a number. Trying to parse it is likely to fail.

Always look at the logs for errors. Most of the above should have generated error messages in the log telling you something of what is going wrong.

If you don’t want to use UoM, a JS transform solution would be

  return parseFloat(input) / 1000;

This file must go in the transformations folder, not scripts folder.

I don’t remember if OH 3 supported inline scripts but if it does

JS: | parseFloat(input)/1000

Since this is MQTT, the transformation should be on the Channel config, not as a profile.

I don’t use openHABIAN. I have my OH running as a Docker container on my NAS and hence no Frontail :frowning:

You cannot be successful in using openHAB without access to the logs somehow. If you can’t access the logs now, you need to drop everything and figure that out first.

In docker the logs are located in the userdata/logs folder where userdata is the volume you mount to /openhab/userdata in the container.

You can run Frontail in a docker container and point it at that file.

I have access to the logs folder and I know how to check :wink: but I am not using Frontail … I run while testing such things the karaf console with log:DISPLAY in parallel but it shows no errors.
Let me first copy my javascript to the transform folder and see what this does …
… and I will also test to use the units (I have removed it as I thought it only works with plain numbers) …

Switching to units did not help

Number:Power  EBZpow_used   "Stromzähler: Strom - verbraucht [%.2f %unit%]"  (gEBZ) { channel="mqtt:topic:bla:tasmota:power_used" [ profile="transform:JS", toItemScript="| Quantity(input).divide(1000)" ] }

I also run OH in docker, but used this to setup Frontail in its own Container. It is working very well.

Well, you didn’t do what I described above. Of course it doesn’t work.

  • Item type needs to be Number:Power (so far so good)
  • There should be no transformation at all, particularly not a transformation profile. But even so, this particular transform is absolutely not going to work in OH 3.4, Quantity only exists in the newer JS Scripting add-on and 3.4 uses the older Nashorn JS for transformations
  • You’ve still not shown the Thing configuration, you must set the “Unit of Measurement” property to W.
  • You need to set the State Description pattern Item metadata, to kW.

Thanks, I definitely should consider it :slight_smile:
I don’t see any log entries while testing … I am constantly running log:DISPLAY in the karaf console

I copied the javascript script now to the transform folder and changed to the direct usage of 1000 in the calculation …

(function(data) {
    var MyData = (parseFloat(data) / 1000);
    return MyData.toString();

the item definition looks like this:

Number        EBZpow_feed   "Stromzähler: Strom - eingespeist [%f]"          (gEBZ) { channel="mqtt:topic:bla:tsmota:power_feed" [ profile="transform:JS", toItemScript="w2kw.js" ] }

but also no success and no entry in the logs …

There are only these entries but I am not sure if they correlate exactly with my testing:

18:39:06.636 [WARN ] [core.io.rest.auth.internal.AuthFilter] - Unauthorized API request from Error while processing JWT token
18:39:14.643 [WARN ] [core.io.rest.auth.internal.AuthFilter] - Unauthorized API request from Error while processing JWT token

Then something else is wrong.

Note there won’t be anything in the logs until the Channel tries to update the Item, as I said above.

Forget about everything else I’ve said above. Undo everything you’ve done so far.

Set the “incoming value transformation” to on the Thing's Channel

JS: | parseFloat(input) / 1000

and make sure the “unit of measurement” property is not set to anything on the Thing's Channel.

Set the Item to:

Number EBZpow_feed "Stromzähler: Strom - eingespeist [%f] { channel="mqtt:topic:bla:tsmota:power_feed" }

No profiles. No .js files. No units.

Nothing will happen until a new value is published to the MQTT topic.

Ok, will do. May you kindly tell me where to put the line that you have provided in my thing channel definition as I have already a transformation pattern to extract the value from JSON array?

        Type number : power_feed "Stromzaehler Power provided" [ stateTopic="stromzaehler/tele/tasmota_15C3BD/SENSOR", transformationPattern="JSONPATH:$.SM.2_8_0" ]

I fear that I cannot make two transformations in the thing channel and as I primarily need the JSONPATH transformation to gather the data from a JSON array I guess that I cannot apply the javascript transformation to convert the value as well.

Am I right?

I don’t do .things files. I find far more time is wasted fighting syntax errors than any benefit, real or perceived, that comes from using .things files. I’d guess you’d want to chain the JS transform to the JSONPATH transform.

transformationPattern="JSONPATH:$.SM.2_8_0"∩JS:| parseFloat(input)/1000

No, and to kind of prove my point, when using the UI create Things it tells you so and how to do it right there on the screen.

1 Like

I changed the mqtt channel but behavior did not change and I still get the Watt value

       Type number : power_feed "Stromzaehler Power provided" [ stateTopic="stromzaehler/tele/tasmota_15C3BD/SENSOR", transformationPattern="JSONPATH:$.SM.2_8_0"∩JS:| parseFloat(input)/1000 ]

I will take your advise and remove all manual definition and create the mqtt channel via the UI to if I can get it run when I completely switch from file based definition to the UI based definition (even though I would expect that it shoudl behave the same :smiley: )

And for sure a message has been published after the change? There was also a bug in the file loader in OH 3.4 IIRC where it doesn’t always pick up changes to .things files You might need to reload the MQTT bundle from the karaf console (or restart OH) to ensure the changes got picked up.