It took me a long time to find, so I’m sharing what is working in OH3 for channel incoming transforms. I also have some questions/feedback about what didn’t work.
Background
I’m parsing a value from MQTT from my OpenEVSE charger, that comes in like “2”, “5”, etc. I want to map it to a human-readable string like “not connected”, “charging”, etc.
I have OpenHab 3.4.2-1 installed from the stable apt channel, running on Ubuntu, with a local Mosquitto as my MQTT broker.
What Worked
On my server, I have two files:
/etc/openhab/transform/openevse_parse_state.js
(function(data) {
var lookup = {
'0':'unknown',
'1':'not connected',
'2':'connected',
'3':'charging',
'4':'vent required',
'5':'diode check failed',
'6':'gfci fault',
'7':'no ground',
'8':'stuck relay',
'9':'gfci self-test failure',
'10':'over temperature',
'254':'sleeping','255':'disabled'
};
var converted = lookup[data];
if (converted == undefined) {
return 'unknown_' + data;
}
return converted;
})(input)
and /etc/openhab/transform/openevse_state.map
0=unknown
1=not connected
2=connected
3=charging
4=vent required
5=diode check failed
6=gfci fault
7=no ground
8=stuck relay
9=gfci self-test failure
10=over temperature
254=sleeping
255=disabled
=_source_
(OH3 seems to pick up changes to these files without a restart.)
I added two add-ons via the UI: Settings > Other Add-ons > Transformation Add-ons > Javascript and Map.
Finally, when I set up a Thing for my OpenEVSE through the UI. Settings > Things > (created the new Thing) > Channels. Either of these two values worked for the “Incoming Value Transformations”:
JS:openevse_parse_state.js
or
MAP:openevse_state.map
In the “Code” view, the Thing with the state channel looks like:
UID: mqtt:topic:d432b36bc2:b1f8c9243a
label: OpenEVSE MQTT Thing
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:d432b36bc2
channels:
- id: openevse_state
channelTypeUID: mqtt:string
label: OpenEVSE State
description: ""
configuration:
stateTopic: openevse/state
transformationPattern: JS:openevse_parse_state.js
Issues and Questions
The Javascript transformation addon docs say to use JS(transformation/myscript.js)
. But that doesn’t work for me: it has a transformation/
directory prefix that doesn’t seem necessary, and it has a JS(
instead of JS:
. I saw that in a lot of examples. Also, its “Edit this page on GitHub” link goes to this broken URL.
A lot of documentation also referenced a :%s
at the end of the transform, which didn’t work for me.
Many docs like the OpenHAB Transformations docs and posts show a different language for Thing code than what I see in he UI. Is that old/deprecated? Is there another place where the declarations look like Type switch : relay [ stateTopic="shellies/shelly1-############/relay/0", ... ]
? I didn’t see many examples of the YAML code.
The Map Transform addon page didn’t have an example of an input transform, I sent PR #14659.
I set some initial addons in addons.cfg
. Then I installed MAP
and JS
transforms, and at some point later went back and discovered they had been uninstalled. (Not sure if that was because I cleared cache.) At the top of that file it says “This is only regarded at the VERY FIRST START of openHAB”, but after listing javascript,map
in there, they didn’t get uninstalled again over some service restarts.
I spent a good few hours puzzling over error messages like Transformation service JS(OPENEVSE_PARSE_STATE.JS) for pattern %s not found!
. If there’s a way to debug whether a transform service is installed, and whether a given file is found by the service, that would be great to add to docs about debugging steps somewhere, maybe the Transformations docs.
I also initially found my way to instructions putting files in scripts/
or having a .script
extension, from the Transformations doc page; I’m not sure yet what the distinction is between a script transform and the MAP/JS input transforms I was using.
Still, I’m enjoying getting started with OpenHAB!