Why transformation doesn't work in my case?

This item shows “-NaN”

Number  m8p34_temp "m8p34_temp [%.1f °C]"   { channel="megad:device:in:m8_p34:i2c" [profile="transform:JS", function="megad_i2c_1.js"] }

But when i use rule - all ok:

rule "m8p34_i2c"
    Item m8p34_i2c received update
	logInfo("-----TEST------", transform("JS", "megad_i2c_1.js", m8p34_i2c.state.toString))

12:09:50.653 [INFO ] [smarthome.event.ItemStateChangedEvent] - m8p34_i2c changed from temp:23.32/press:766.53/hum:39.927 to temp:23.31/press:766.51/hum:39.958
12:09:50.656 [INFO ] [marthome.model.script.-----TEST------] - 23.31

Any idea?
How to do transform in thing definition?

What openHAB version are you running?


Just for a test, change the item to a String

String  m8p34_temp "m8p34_temp [%.1f °C]"   { channel="megad:device:in:m8_p34:i2c" [profile="transform:JS", function="megad_i2c_1.js"] }

Yes, start working

2019-02-11 14:36:23.889 [vent.ItemStateChangedEvent] - m8p34_temp changed from NULL to 23.62

And Site map does not shows value, i delete conversion “[%.1f °C]” and SAVE items file, but error still appear:
Item is:

String  m8p34_temp  "m8p34_temp"  <temperature>   (allItems) { channel="megad:device:in:m8_p34:i2c" [profile="transform:JS", function="megad_i2c_1.js"] }

2019-02-11 14:41:36.064 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '23.66' of item m8p34_temp with format '%.1f °C': f != java.lang.String

Ok try that:

String  m8p34_temp "m8p34_temp [%s °C]"   { channel="megad:device:in:m8_p34:i2c" [profile="transform:JS", function="megad_i2c_1.js"] }

Yes, item is displayed in sitemap. :smile:
This is only String conversions? Is it possible convert to Number?

I think this is a deficiency in the fairly new profile feature.
JS transforms always return strings, so there needs to be an “extra” step in the linkage between channel and Item to parse a profile result into something suitable for the Item type.

1 Like

Yes there is. This the second case this week I have seen.

Do not clear for me, this is error in trasformation code?

I look how to make it work with http binding items
Number PV_testdel “Tagessumme [%0f Wh]” <energy3> (gFroniusFlow) { http="<[FroniusPowerFlowCache:30000:JSONPATH($.Body.Data.Inverters.1.E_Day)]" [profile="transform:JS", function="divide1000.js"] }

any idea

Yes, a transform profile will just not work with a Number Item.

could I wrap this JSONPATH in another JS(divide1000.js) somehow to divide the value before its written to the item?

{ http="<[FroniusPowerFlowCache:30000:JSONPATH($.Body.Data.Inverters.1.E_Day)]" }

This remains true, however many layers of transform you use.

but if you’re suggesting writing a single transform for use in the binding config, that both handles JSON and does maths, go ahead.

But those items use a transform… Why can they be a number then?

Because it is the binding that deals with matching and converting the string from the transform to the type of Item that the channel supports. (or the link in the case of 1.x versions)

Looking more closely at your previous post, it would never work. Profiles work with channels. but you’re using a version 1.x binding, No channel, no profile.

You can only use the transforms if the binding supports them. Hurrah, http binding does support transforms. (Not all bindings do)

Furthermore, http binding supports linking to Number type Items.

And now I tried to do 2 transforms js divide and jsonpath in one go. But if possible won’t find a working syntax :frowning:

You cannot do two transforms. The binding does not support that.
Instead, you can write one script that does both of the functions you want to do.