[Solved] - Too stupid for item transformation

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

(function(input){
  return parseFloat(input) / 1000;
})(input)

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.
https://community.openhab.org/t/pointing-log-files-to-fontail-in-docker-environment/118669

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();
 })(input)

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 172.17.0.1: Error while processing JWT token
18:39:14.643 [WARN ] [core.io.rest.auth.internal.AuthFilter] - Unauthorized API request from 172.17.0.1: 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.

I got it …

I keep my file based mqtt thing and channel definition but at the item level I can add my w2kw.js and it then converts the data …
But this means that something does not work when defining the item transformation on file level …
I know that file based configuration is old-fashioned but I can see in human readable format what it does :wink:

So can I.

UID: mqtt:topic:mosquitto:cerberos_sensor_reporter
label: cerberos sensor_reporter
thingTypeUID: mqtt:topic
configuration:
  payloadNotAvailable: OFFLINE
  availabilityTopic: sensor_reporter/cerberos/status
  payloadAvailable: ONLINE
bridgeUID: mqtt:broker:broker
location: Garage
channels:
  - id: garagedoor1
    channelTypeUID: mqtt:contact
    label: Large garage door
    description: Large garage door open status
    configuration:
      stateTopic: sensor_reporter/cerberos/garagedoor1/state
      off: CLOSED
      on: OPEN
  - id: garagedoor2
    channelTypeUID: mqtt:contact
    label: Small garage door
    description: Small garage door open status
    configuration:
      stateTopic: sensor_reporter/cerberos/garagedoor2/state
      off: CLOSED
      on: OPEN
  - id: garagedoor1_opener
    channelTypeUID: mqtt:switch
    label: Large garage door opener
    description: ""
    configuration:
      commandTopic: sensor_reporter/cerberos/garagedoor1/cmd
      off: OFF
      on: ON
  - id: garagedoor2_opener
    channelTypeUID: mqtt:switch
    label: Small garage door opener
    description: Small garage door opener controller
    configuration:
      commandTopic: sensor_reporter/cerberos/garagedoor2/cmd
      off: OFF
      on: ON
  - id: online
    channelTypeUID: mqtt:switch
    label: Online status
    description: Indicates online status of this sensor_reporter
    configuration:
      stateTopic: sensor_reporter/cerberos/status
      off: OFFLINE
      on: ONLINE

That’s what the code tab is for.

Yes, I saw that … Maybe I think about switching now … up to now it worked as expected. The transformation seems not be working correctly … maybe a hint to move forward :wink:

THANK YOU for supporting me !!!