Capture ON command for Shelly Dimmer in transformationscript for MQTT

Hi all,

I have some Shelly dimmer devices connected to my Openhab 2.5 using the MQTT binding.
On the other side I have the Homekit binding to control the devices with my Phone and Siri.
When activating a scene in Homekit, there goes something wrong with my dimmer value’s.

A dimmer should for example be set to 10% brightness but it goes to 100%, I can see this going wrong but do not know how to improve my transformation script to solve this. (I’m a beginner in Javascript)

This is what I see happening in the Log file.

2020-05-14 19:38:45.756 [ome.event.ItemCommandEvent] - Item 'ShellySpots' received command 10
2020-05-14 19:38:45.789 [ome.event.ItemCommandEvent] - Item 'ShellySpots' received command ON
2020-05-14 19:38:45.831 [nt.ItemStatePredictedEvent] - ShellySpots predicted to become 10
2020-05-14 19:38:45.867 [nt.ItemStatePredictedEvent] - ShellySpots predicted to become ON
2020-05-14 19:38:45.898 [vent.ItemStateChangedEvent] - ShellySpots changed from 0 to 10
2020-05-14 19:38:45.907 [vent.ItemStateChangedEvent] - ShellySpots changed from 10 to 100
2020-05-14 19:38:45.919 [vent.ItemStateChangedEvent] - ShellySpots changed from 100 to 10
2020-05-14 19:38:45.931 [vent.ItemStateChangedEvent] - ShellySpots changed from 10 to 100

First a dimmer value is sent, and then the ON command. The dimmer value is working correct. But after the dimmer value the ON command is sent, and interperted as a 100 value, resulting in my dimmers to go to 10% and right after that jump to 100% brightness.

The script I use to convert the Outgoing Value Transformation to a JSON for MQTT Looks like this:

(function(brightness) {
if (brightness > 0) {
    var shellyobj = {"turn":"on", "brightness":brightness};
} else {
    var shellyobj = {"turn":"off", "brightness":0 };
var data = JSON.stringify(shellyobj);
return data;

This is correct for the dimmer value’s but the ON isn’t captured correct.
How can I fix this, so that for the ON command var shellyobj = {"turn":"on"} (and for OFF var shellyobj = {"turn":"off"} ) is sent, without the brightness parameter?

Your command is always passed to the transformation as a string. You should be able to have your javascript examine it for ‘ON’ or ‘OFF’ or parse it into a number, and take different actions as you wish.

Using the Shelly Binding, this should not be necessary at all.