Creeate a transformation using Jython

I am trying to create a rule that is triggered when Sonos cover url changes, i want to resize the image and publish it in the OpenHab static folder so its visible also from the cloud and has a proper fixed size.

I would like to use PIL library, assuming i can import it, i think the ideal approach would be to have a rule like this:

rule "Sonos Cover Resize"
when
    Item Sonos_Current_Album_Cover_Art changed
then
    logInfo("Sonos", "Cover changed")
    var coverUrl = get value of item (figuring it out)
    var resizedFile = transform("SCRIPT", "publish_image.py", coverUrl)
    logInfo("Sonos new file", resizedFile)
    ....
end

How would publish_image.py looks like? JS and JRuby have both anonymous functions and python cannot return anything unless i define a function. Is this supported? is there another way to do this? Since i need to pass a parameter and convert an image i thought it was the right approach.

P.S. editing files via VS Code remote + OpenHab extension is JUST AWESOME

Please be aware that the configuration has changed slightly (again), see

So you’ll have to call

var resizedFile = transform("PY", "publish_image.py", coverUrl)

afaik all installed Scripting languages are supported, but openHAB has to know which language should be used for the specific file (and they don’t bother using the file extension for that purpose :wink: )

Other than for JS and DSL, openHAB won’t create a short example (yet?) for python, but the example code of JS may be a hint:

(function(data) {
  var returnValue = "String has " + data.length + " characters"
  return returnValue
})(input)

So, you’ll have to create a function with a return value. Something like this (I’m not good at python):

def function(data):
    returnValue = 'String has ' + data.len() + ' characters'
    return returnValue

That did work!!

This doesn’t work, i can see the script being called but the function is not, i tried printing something but that output is not considered a return value, i also tried defining __main__ but no luck

Forgot to mention i am using 4.0.0.RC1

Well, I was afraid of this option… We will have to wait for a more experienced user, then :wink:

Try just writing the code without any functions.

Thanks, tried that but you cannot return a value in a python script, also i checked both the ARGV and ENV and the parameter sent to the transformation is not in any of those :frowning:

Maybe its just not supported

I don’t use python but maybe this could work?

I think its not possible to use jython for transformations, i had a quick look to the addon source and it seems like there is no scopeValues method implemented so it seems parameters are just not passed to jython :frowning:

You could write it in js / jruby. Shouldn’t be a big deal - you don’t have to rewrite all your other scripts.

Indeed not a big deal but i am not sure why Jython is a second class citizen, also if i have to download libraries i prefer to have only python ones rather than mixing JS/Gems and Python ones. I will try to dig a bit deeper in the code to understand why its not there or maybe a dev shows up and gives some hint :slight_smile:

Jython is stuck on Python 2.7 which is now years past end of life. Python 3 support is no where in sight.

The upstream Jython project has very little activity indicating it’s all but abandonded.

No one on the OH side has volunteered to maintain the Jython add-on and the corresponding helper libraries.

If you want to use Python, the only recommendation I can offer is to use HABApp. Unless and until someone decides to develop a GraalVM Python add-on (which would be awesome!) Jython can only be considered to be deprecated. At some point, probably soon, it’s going to break and there will be nothing the OH project can do to fix it.

It’s treated like a second class citizen because it is. Maybe “zombie” would be a better metaphor.

None of the other rules languages have any of these problems and JS Scripting and jRuby have the best overall support in terms of number of users and the quality of their docs.

But, I’m not sure a transformation is the better approach here anyway. I wonder if a call out to a native Python script using executeCommandLine to do the resize and the save to disk would not be a more appropriate approach. It doesn’t feel right for a transformation to have extra side effects (i.e. saving to disk) beyond changing the the input to a different output.

Thanks, yeah Jython Python 3.0 support is waiting from too long I will just use JS across the board. I think there should be a warning in the docs highlighting this issue.

I didn’t know transform was meant for fast tasks like string conversions only good to know, still new to OpenHab. Will still try to do what I want in JS through a transformation just as a test.

Thanks!