OH3: profile transformations dont seem to be working

Profile transformations dont seem to be working.
Here is the channel - item - link definition:

{
  "channelUID": "airquality:aqi:e2fd79faca:observationTime",
  "configuration": {
    "function": "test1.js",
    "profile": "transform:JS"
  },
  "itemName": "AirQuality_ObservationTime"
}

test1.js (just a test script to test regex transformation using JS. Script code is fine as tested on online sites).
The input string is supposed to be in YYYY-MM-DDTHH:mm:ss.SSSZ and the result is HH:MM

cat ../transform/test1.js

(function(i) {
    const regex = /\d\d\d\d-\d\d-\d\dT(\d*)\:(\d*).*/gm;
    return i.replace(regex, "$1:$2");
})(input)

The log seems to show that there is some attempt to use transformations that were defined earlier. Below - the patterns were defined earlier and seem to have ‘stuck’ and the new ones (above) dont seem to take effect.

 2020-11-13 02:14:57.294 [WARN ] [.profiles.RegexTransformationProfile] - Could not transform state '2020-11-13T01:00:00.000+0530' with function '(.*)' and format '%1$ta, %1$td %1$tb, %1$tl:%1$tM %1$tp'

Same behaviour is observed with other transformations like regex
Pls let me know what addl logs can I provided to troubleshoot. Thank you

What version of OH3? There are 2 Milestones and several snapshots.

Snapshot 2006 - on Ubuntu 20 on a core i3 with 8gb ram

1 Like

So it isn’t that they are not working so much as that updates to them are not being loaded?

That message seems to indicate that there is a mixture of the REGEX transformation and formatting in the label. It’s important to remember that transformations only return Strings and the %1$t formatting options only work with DateTimeTypes. So the Item you are transforming has to be of type String and the label set to [%s].

Thank you
I managed to get this far with bundle restarts, but obviously the transformation is not working

020-11-13 16:15:13.119 [DEBUG] [rnal.JavaScriptTransformationService] - about to transform '2020-11-13T15:00:00.000+0530' by the JavaScript 'test1.js'
2020-11-13 16:15:13.119 [DEBUG] [ipt.internal.JavaScriptEngineManager] - Loading script /etc/openhab/transform/test1.js from storage
2020-11-13 16:15:13.127 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state '2020-11-13T15:00:00.000+0530' with function 'test1.js' and format '%s'
2020-11-13 16:15:13.128 [DEBUG] [iles.JavascriptTransformationProfile] - Transformed '2020-11-13T15:00:00.000+0530' into '2020-11-13T15:00:00.000+0530'

New simple script

(function(i) {
     return "Hello";
})(input)

Item

String test_regex8 { channel="airquality:aqi:e2fd79faca:observationTime" [profile="transform:JS", function="test1.js"]}

Thanks @rlkoshak. In the OP the item was string itself. And above also, the item is string.

Does that work? You didn’t say.

Umm, you do know that the various transformation services are separately installable add-ons?

Nope didn’t work. The log says ‘NOT transformed state’ as can be seen in the log. I am not even sure if it has loaded the correct (or latest) js file. Already tried restarting the javascript transform bundle.

Yes ofcourse all transformations are installed.

How many times do people asking for help say that when they did not specifically state that was the case??

1 Like

Alright, Log before “I changed to this” leads to confusion.

It’s perhaps worth clarifying a bit here; yes, transforms always return strings. Any formatting you need to do, you have to do it within the transform.

This also applies at the “input” end - whatever value is coming from the channel is given to the transform in its .toString form. To treat as numeric or date or blah, you’d need to parse it.

At OH2.5, you could use a profile to transform a channel data of any type, but only output to a String type Item.

This has been improved for OH3, the profile framework should now attempt to parse the transform’s string output into the Item type, e.g. “3.0” should be usable with a Number Item, just as you can do someNumber.postUpdate("3.0") and expect the framework to parse it.
This new feature should be included in the milestone


it’s quite possible you have run into some bug with this.

This feature should explain your logs referencing format %1$ta or %s - it depends on the target Item type.

All of which boils down to - it should work with what you have shown us here.
Experimenting with String type target Item is certainly smartest, you can change later when it’s working.

You can validate that your transform script and service works in a rule, for a sanity check -

var results = transform("JS", "test1.js", "2020-11-13T15:00:00.000+0530")
logInfo("test", " transform " + results)
2020-11-13 13:39:21.592 [INFO ] [.eclipse.smarthome.model.script.test] -  transform Hello
020-11-13 16:15:13.119 [DEBUG] [rnal.JavaScriptTransformationService] - about to transform '2020-11-13T15:00:00.000+0530' by the JavaScript 'test1.js'
2020-11-13 16:15:13.119 [DEBUG] [ipt.internal.JavaScriptEngineManager] - Loading script /etc/openhab/transform/test1.js from storage
2020-11-13 16:15:13.127 [WARN ] [iles.JavascriptTransformationProfile] - Could not transform state '2020-11-13T15:00:00.000+0530' with function 'test1.js' and format '%s'
2020-11-13 16:15:13.128 [DEBUG] [iles.JavascriptTransformationProfile] - Transformed '2020-11-13T15:00:00.000+0530' into '2020-11-13T15:00:00.000+0530'

Looking at the source code for the JavaScriptTransformationProfile, this probably originates in the JavaScriptEngineManager, when it tries to load the ScriptEngine or execute the script. Since you get the log message from L66, but not L70, something between those lines must be throwing an exception. Do you have anything else that uses JavaScript that’s working? Otherwise i would guess there’s something about the script engine that fails.

Allright! got it to work a few times. got the below in the logs, indicating that the js file was read properly, compiled and put into cache. this was definitely missing before.

2020-11-13 22:55:03.516 [DEBUG] [iles.JavascriptTransformationProfile] - Profile configured with 'function'='test3.js', 'sourceFormat'=%s
2020-11-13 22:55:03.517 [DEBUG] [rnal.JavaScriptTransformationService] - about to transform 'VERY_UNHEALTHY' by the JavaScript 'test3.js'
2020-11-13 22:55:03.518 [DEBUG] [ipt.internal.JavaScriptEngineManager] - Loading script /etc/openhab/transform/test3.js from storage
2020-11-13 22:55:03.533 [DEBUG] [ipt.internal.JavaScriptEngineManager] - Putting compiled JavaScript jdk.nashorn.api.scripting.NashornScriptEngine$3@137c27c0 to cache.
2020-11-13 22:55:03.547 [DEBUG] [iles.JavascriptTransformationProfile] - Transformed 'VERY_UNHEALTHY' into 'Super Hello'

I did two things:

  • upgraded from 2006 to 2008 snapshot. this should be irrelvant here because i dont think there were any changes to the code impacting this. but anways, probably restarting the whole system had an effect
  • importantly, i did what @rlkoshak suggested. Created a script to invoke javascript transformation. This was the first time i got the compile and load into cache messages in log.
var results = transform("JS", "test1.js", "2020-11-13T15:00:00.000+0530")
logInfo("test", " transform " + results)

After this, using the JS transformation in profiles seems to be working fine for a while but it again acts wierdly after a while. new transformations are not added and it keeps showing the older ones in the logs everytime i add or even change and save a transformation. sigh.
have cleared the cache and restarted again. will observe.

Yes, the issue I showed you earlier is still open.

1 Like

Hi,
I experience some similar issues with the (OH3.01) installed REGEX transformation.

Using the HP Printer binding, I want to use 4 items for each color level monitoring. Thing setup and channels are working perfect and in the items list and in the log files the level values are shown in “xx %”.

But when modelling it in the UI, something in the backround transfers the state from “xx %” to “xx.000000 %”. The datatype from the HP color level channel is number:dimensionless.

So the floating number with 6 times the 0 is also visible in label or gauge widgets when linking the respective color item.

I tried to use a profile with REGEX “(\d\d).*”, like in the example given below the input field, with %s, and different %f formats, but the item state value preview is either void (nothing displayed) or Err, when applying wrong regex patterns, or the mentioned “xx.000000 %”.

How can I transform it to use the state value as integer/string and not decimal in a widget/item? Or do I need to write a rule? But a.f.a.I.u., profiles are meant to avoid such scripting…

Many thanks in advance for helping an openHAB newbie!

If your logs are showing ‘changed to xx%’ I think you are looking in the wrong place.
Tell us more about “,modelling it in the UI”? After all the value is fine but you have a presentation issue.

Yes, I think it’s something related to the presentation and/or transformation…
Here you are (concatenated picture):

I want the “84.000000 %” transformed to a “84%” in a gauge or lable card widget on a layout page. BTW: the gauge widget shows correctly 84 percent of the circle.

As I wrote, I tried several item configs and profile reg expressions. Some warning examples here:

[WARN ] [.profiles.RegexTransformationProfile] - Could not transform state ‘85 %’ with function ‘.’ and format ‘%.1f %%’
[WARN ] [.profiles.RegexTransformationProfile] - Could not transform state ‘85 %’ with function '.
’ and format ‘%d %%’
[WARN ] [.profiles.RegexTransformationProfile] - Could not transform state ‘84 %’ with function ‘(\d\d).*’ and format ‘%.0f’

The one in the picture above shows no warning log, but I find no working regex to get the “84%” or at least “84” output. And yes, I also tried different escapes “\”.

That’s just an administrator view.

You don’t need any transformation,just a format