Issue using JavaScript inline transformationPattern in mqtt things file

Hi there,

I’m trying to figure out why my transformation does not work:

Type number : StromzaehlerstandBezug       "Zählerstand Bezug"       [ stateTopic="devices/SMLReader/sensor/1/obis/1-0:1.8.0/255/value", transformationPattern="JS:| parseFloat(input) / 1000"]
Type number : StromzaehlerstandEinspeisung "Zählerstand Einspeisung" [ stateTopic="devices/SMLReader/sensor/1/obis/1-0:2.8.0/255/value", transformationPattern="JS:| parseFloat(input) / 1000"]

Messages on this topic are a single float value (i.e. “1234.5”), but those are Wh, while I would prefer kWh (i.e. “1.2345”).

Sender is SMLReader running on a ESP8266 sitting on my new smart meter.

The transformation seems to have no effect at all, but there is no error message visible neither in openhab.log nor events.log (which shows the change to the non transformed value).

Thanks for any hints!

Javascript-Transformation ( binding/addon ) is installed ?
Is your code an excerpt of a things file ? See this thread for a similar discussion:Inline JS transformation not working

Thanks @Wolfgang_S for those hints. The really strange thing is, that this is just a few lines above, and it works:

Type number : battery_soc "Batteriestand" [ stateTopic="devices/rctPower/battery.soc", transformationPattern="JS:| parseFloat(input) * 100" ]

So I think the surrounding conditions (JS transformation installed, MQTT binding can potentially do this, …) are fulfilled.
Really no clue what is wrong this time…

Also tried the suggestion in Inline JS transformation not working - #9 by airmax, but nothing changed.

To try: for the ones that aren’t working, temporarily change the / for a * to match the one that does work, and see what happens?

Also, if you remove the transformation entirely are you getting the raw value that you expect?

1 Like

Tried that, still getting the raw value.

Raw value, as expected.

As you are using a *.things file: Did you restart after changing?

Keep in mind, that updating (not creating) a channel through a *.things file may or may not work instantly, so better restart openHAB to be sure.

2 Likes

Thanks for all the input @hafniumzinc and @Udo_Hartmann!

I did not restart openHAB as a whole, but did a

bundle:restart org.openhab.binding.mqtt 

But this is a really good point - I fell into this pit before.
Will try this later when I have more time.
Thanks!

Hah!
I had a look at the API explorer and noticed that indeed, the channel is missing the transformation:

{
      "linkedItems": [
        "StromzaehlerstandBezug"
      ],
      "uid": "mqtt:topic:MosquittoLocal:smlReader:StromzaehlerstandBezug",
      "id": "StromzaehlerstandBezug",
      "channelTypeUID": "mqtt:number",
      "itemType": "Number",
      "kind": "STATE",
      "label": "Zählerstand Bezug",
      "defaultTags": [],
      "properties": {},
      "configuration": {
        "retained": false,
        "postCommand": false,
        "step": 1,
        "formatBeforePublish": "%s",
        "stateTopic": "devices/SMLReader/sensor/1/obis/1-0:1.8.0/255/value"
      }
    }

Where the other - working one - shows it:

{
        "linkedItems": [
          "RCT_battery_soc"
        ],
        "uid": "mqtt:topic:MosquittoLocal:rctPower:battery_soc",
        "id": "battery_soc",
        "channelTypeUID": "mqtt:number",
        "itemType": "Number",
        "kind": "STATE",
        "label": "Batteriestand",
        "defaultTags": [],
        "properties": {},
        "configuration": {
          "retained": false,
          "postCommand": false,
          "step": 1,
          "formatBeforePublish": "%s",
          "stateTopic": "devices/rctPower/battery.soc",
          "transformationPattern": "JS:| parseFloat(input) * 100"
        }
      }

So I guess this will work after the unloved restart of openHAB. (I’ll mark this as solved then)
Thanks all!