Map transformation re-use in python

Hi there,

is there a clever way to re-use my maps I created for the UI in $OPENHAB_CONF/transform in my Python scripts?

I do not want to create a copy in Python (e.g. as dict) but considered parsing the *.map files and create the dict dynamically.

Maybe I’m just not finding the right way to use MAP’s directly - so any suggestion is welcome.

Thanks @rlkoshak,

I found this page - but isn’t this applying only to DSL?

At least I found no reference to Python scripts - anything in $OPENHAB_CONF/\automation\jsr223\python\personal

Actions are available in all languages. There are examples in the But How Do I section of the Jython Helper Library docs for how to access the Action, but the call is the same once you have access to them.

I tried as described in Actions — openHAB Helper Libraries documentation

from core.actions import Transformation
Transformation.transform("JSONPATH", "$.test", test)

But I get this error now:

ImportError: cannot import name Transformation in /etc/openhab/automation/jsr223/python/personal/ at line number 9

That’s the old site which has been dead since OH 3 came out. You need to go to CrazyIvan359’s fork of that repo and look at those docs. You should be using the helper library from there too. If that doesn’t work I’ve nothing to recommend. I haven’t used Jython in many years now.

Note, of all the parts of openHAB that are at risk of just stopping to work suddenly at some point in the not to distant future, Jython support is the most likely. The upstream repo for Jython itself is all but abandoned. It’s been two years since the last merge to the openHAB Helper Library. And no one is maintaining the add-on.

I recommend anyone still using Jython to take this time you have now where it still works and gradually migrate to anything else. when Jython breaks you’ll have to do it all at once.

Well “CrazyIvan359’s fork” has exactly the same in the documentation. It is also not working.

Thanks anyway for trying.

I’ll have a look at other scripting languages then in the near future.

If you like python you can always try HABApp.
It’s a python3 rule engine that allows you to use any python library with openHAB.
You can install it besides your existing rules and play around with it.
You even can install it on a different machine to play around with it and connect to openHAB remotely.
It also provides full syntax highlighting and error checks in the IDE.
It’s awesome!

1 Like

Hi @Spaceman_Spiff, I actually already have HABApp installed and use it extensively - especially as it nicely used Python3 and not Python2 - but also there I didn’t find a good way to re-use my mapping tables defined in $OPENHAB_CONF/transform that I need for the UI.
Do you have a proposal how to combine the two mapping requirements best?

  1. UI mapping (e.g. for BasicUI)
  2. script mapping in Python (e.g. for log output)


I guess I could build something which exposes HABApp parameter files as a map transformation?
Or something that loads the mappings and exposes them as a dict.
Currently there is no easy solution.

It shouldn’t bee too hard to write a function that takes a filename and a string to transform as arguments, read and parse the file into a dict and then return the result. I haven’t used habapp personally, but I believe plain python functions should be straightforward to use.

It should become supported when the REST API for Actions gets merged (though I already had actually), right? Or does that end point only support Thing Actions?

I think so, but I am not 100% sure.

However doing the request every time is not ideal performance wise.
So at least for the MAP transformation it makes sense to either

  • create the mapping from openHAB on the HABApp side
  • mirror a HABApp parameter file so it’s available as a mapping in openHAB

I haven’t made up my mind yet which makes the most sense to be honest.
The second one seems to make more sense because there is no notification when the transformation changes so it’s easier to keep things in sync.

In OH 4 we now have managed transformations accessible through the API.

    "uid": "config:map:waveplus",
    "label": "WavePlus ",
    "type": "map",
    "configuration": {
      "function": "Online=ON\nOffline=OFF\nConnection\\ lost=OFF\n"
    "editable": true
    "uid": "config:js:secsToStr",
    "label": "Seconds to String",
    "type": "js",
    "configuration": {
      "function": "(function(data) {\n  const zeroPad = (num, places) => String(num).padStart(places, '0');\n  const hourSecs = 60 * 60;\n  const minuteSecs = 60;\n  var totalSecs = parseInt(data);\n  const hours = Math.round(totalSecs / hourSecs);\n  totalSecs = totalSecs % hourSecs;\n  const mins = Math.round(totalSecs / minuteSecs);\n  const secs = totalSecs % minuteSecs;\n  return zeroPad(hours,2)+\":\"+zeroPad(mins,2)+\":\"+zeroPad(secs, 2);\n})(input)"
    "editable": true

That could be an option too.

Hi @rlkoshak,

that looks like a neat idea - I will try it out on my test server.


@Schrott.Micha I’ve added support for transformations in the current dev branch. Maybe you can try it out and give some feedback? If so it would be best if you open a github issue.

Do you think it would make sense to also mirror parameter files from HABApp to openHAB (as transformations)?

Hi @Spaceman_Spiff,

sorry for the late response - but I was on vacation - and than other things kept me busy.

Thanks for the proposal. Give me some time to have a look.