MQTT dimmer, struggle with OFF state

  • Platform information:
    • openHAB version: 4.2.2
      Hey, trying to setup a dimmer with mqtt, but i struggle with the OFF state, i see this error:

2024-10-29 13:01:41.758 [WARN ] [t.generic.ChannelStateTransformation] - Executing the JSONPATH-transformation failed: Invalid path '$.brightness' in '{"state": "OFF"}'. Pattern: '$.brightness'. Value: '{"state": "OFF"}'

When dimmer is OFF:


When dimmer is ON with brightness (no issue)

this is the code, the issue is with the “Dimmer” type…
you can see indeed in screenshot above, if the state = OFF, there is no brightness attribute

    Thing topic dimmer "Dimmers" {
    Channels:
        Type switch : dimmer1 "Eethoek Raam" 		[ stateTopic="openhab/light/dimmer1/state", 
														commandTopic="openhab/light/dimmer1/set_light", 
														transformationPattern="JSONPATH:$.state", 
														formatBeforePublish="{\"state\":\"%s\"}", 
														on="ON", 
														off="OFF" ]
		Type dimmer : dimmer1b "Eethoek Raam"        [ stateTopic="openhab/light/dimmer1/state",
														commandTopic="openhab/light/dimmer1/set_light", 
														transformationPattern="JSONPATH:$.brightness", 
														formatBeforePublish="{\"brightness\":%s}",
														min=0, max=255, step=25 ]
    }

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.

Something like a JS script transform

var parsed = JSON.parse(input);
(parsed.state == "ON") ? parsed.brightness : 0

quite new to OpenHab, not sure how i can do that :frowning:

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?

  1. 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.
  2. JavaScript Scripting - Automation | openHAB (additional documentation and examples: Transformations | openHAB)

Note: all the examples of a JS script transformation in the docs will put the transformation into the following structure:

(function(data) {
  // transformation goes here
})(input)

While that is usually JS best practice, it’s not necessary. The whole reason it’s best practice doesn’t exist the way OH uses JS.

Script transformations can either be defined in files in $OH_CONF/transformations or through the UI at Settings → Transformations

ok, installed addon, now i see indeed a new “js” folder, placed brightness.js file in it …

code =

var parsed = JSON.parse(input);
(parsed.state == "ON") ? parsed.brightness : 0

then something like below?


		Type dimmer : dimmer1b "Eethoek Raam" [ stateTopic="openhab/light/dimmer1/state",
														commandTopic="openhab/light/dimmer1/set_light", 
														transformationPattern="JS($.brightness.js)", 
														formatBeforePublish="{\"brightness\":%s,\"state\":\"ON\"}",
														min=0, max=255, step=25 ]
    }

No,

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 :man_shrugging: I’d rather spend my time helping people with home automation problems instead of syntax errors.

thats was a typo indeed :-), ok, moved it to correct folder now, and i see the item now listed in the UI, so thats good…

Now hopefully someonce can help me indeed with the correct script/thing setting

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.

the file is already in correct folder, but i dont know how to define the mqtt thing to use the JS file

is it like:

transformationPattern="JS($.brightness.js)", formatBeforePublish="\"brightness\":%s,\"state\":\"ON\"}",

would that work, probably not? maybe also a change is needed here:" formatBeforePublish" ?

ok, this indeed works!!

		Type dimmer : dimmer1b "Eethoek Raam" [ stateTopic="openhab/light/dimmer1/state",
														commandTopic="openhab/light/dimmer1/set_light", 
														transformationPattern="JS:brightness.js", 
														formatBeforePublish="{\"brightness\":%s,\"state\":\"ON\"}",
														min=0, max=255, step=25 ]

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?

        Type switch : dimmer2 "Eetkamer Doorgang" 		[ stateTopic="openhab/light/dimmer2/state", 
														commandTopic="openhab/light/dimmer2/set_light", 
														transformationPattern="JSONPATH:$.state", 
														formatBeforePublish="{\"state\":\"%s\"}", 
														on="ON", 
														off="OFF" ]
		Type dimmer : dimmer2b "Eetkamer Doorgang" [ stateTopic="openhab/light/dimmer2/state",
														commandTopic="openhab/light/dimmer2/set_light", 
														transformationPattern="REGEX:(.*brightness.*)∩JSONPATH:$.brightness", 
														formatBeforePublish="{\"brightness\":%s,\"state\":\"ON\"}",
														min=0, max=255, step=25 ]

see what i now see in events…

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

i dont see dimmer2b changing to “0”

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.

confirmed working, many thanx!! :slight_smile:

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