You’ll need to use a smarter transformation. JSONPATH may not be able to do it. You need to see if the state is ON and then select brightness or return 0 if the state is OFF.
can you post me an example how i can do that?
do i need to create a .js file? place it in the “conf\transform” folder?
or this folder? conf\automation\jsr223\js , seems that jsr223 is auto created?
Install the JS Scripting add-on. If you’ve installed the Nashorn JS adfd-on, remove it. The folder for JS Scripting rules is just $OH_CONF/automation/js so if you have jsr223/js that implies you’ve installed the older add-on.
The script needs to be placed in the $OPENHAB_CONF/transform folder with the native extension for the chosen language, for example stringlength.js for a transformation using JS Scripting. JavaScript Scripting - Automation | openHAB
I don’t support .things files so I don’t know. I doubt the . needs to be escaped so I’d rather spend my time helping people with home automation problems instead of syntax errors.
I’m not sure what else you need. Based on the information provided so far you’ve put the .js file in the wrong folder. I’ve posted twice what the correct folder is as well as linked to the relevant documentation.
One thing that might catch up you though is it’s only recently that the MQTT binding supports the JS(filename.js) syntax for a transformation. I don’t remember when that change got merged. If you are not on a recent milestone version of OH you might need to use the JS:filename.js syntax.
Hey @Oggerschummer , tried your approach, the error is gone, the state is also changing when i dimm the light… the only thing not working, is that the state for dimmer2b doesnt change to “0” when i turn the light off… can that be fixed too with the regex?
2024-10-31 10:49:21.725 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2' changed from OFF to ON
2024-10-31 10:49:21.731 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2b' changed from 0 to 100
2024-10-31 10:49:24.488 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2b' changed from 100 to 44
2024-10-31 10:49:28.222 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2b' changed from 44 to 10
2024-10-31 10:49:30.289 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2' changed from ON to OFF
There comes the tricky part.
One idea that might be easier for you to achieve is the create a rule the sends and update of “0” to the dimmer value whenever the dimmer state changes from ON to OFF.
Maybe this could be realized by REGEX with statements that adds a term for the missing brightness value to the result JSON in case in case state is OFF.
But I am not a REGEX crack, maybe others here could help.
Nevertheless a way forward has already been sketched by Rich.
Step1:
Create a file name “mytransformation.js” and put it into the transformations folder, content as follows:
(function(data) {
var message = JSON.parse(data);
if (message["state"] == "OFF") {message["brightness"]="0"}
var out = JSON.stringify(message);
return out;
})(input)
This is the most generic way as it just adds the missing value “brightness” to the incoming JSON in case the state is set to “OFF” and returns it. Not top level optimized and there might be smarter ways to do it, but fine for now to understand the concept.
Step 2: Change the incoming transformation to
JS:mytransformation.js∩JSONPATH:$.brightness
As the script ensures there is a brightness value in the JSON when being switched off no need for the REGEX anymore, just reading the value with the chained JSONPATH transformation.
if you’d adjust the setup you could directly return the brightness value without the need to stringify and returning the whole JSON.
Hope this works for you, did not have a dimmer to test but used a different MQTT Thing.
Enjoy.
2024-10-31 16:38:39.878 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2' changed from ON to OFF
2024-10-31 16:38:40.239 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'dimmer2b' changed from 66 to 0