[SOLVED] Percentage value channel sent via MQTT needs format conversion

I have defined a Channel with the type percentage value. The dimmer value is sent via this mqtt channel to the broker.
A fhem device subscribes this parameter.

The percentage value is transmitted with 1 digit after the decimal separator e.g. 10.0 for 10% via mqtt. The chem device can not handle “10.0” because it expects “10”.

I tried to use Outgoing value transfer in the openhab Paper UI. But the value on mqtt is still “10.0”.

Click the pencil icon to edit the channel, then click show more on bottom left. Change the %s to %d and click save.

My best guess with limited info given.:upside_down_face:

Unfortunately this was my first try. Whatever I tried it did not change the variable value e.g.:

Outgoing Value Format:
empty definition --> 16.0
Test %d --> Test 16.0
%d --> 16.0

The OH log is showing the number without the decimal. Where are you having an issue with the decimal point, when sending it back to the device?

You are right. The OH log shows it without the decimal but the MQTT Broker receives the value with the decimal.
OH is publisher --> FHEM is subscriber
The device which is controlled via FHEM does not accept the value with decimal.

Will need to use the formatBeforePublish on the item. Are you using files or strictly PaperUI?

I use a combination of paper UI and files (items, sitemap, rules).
How to use formatBeforePublish? I am not familiar with that :frowning:

I don’t get it how to use it and solve my issue…

I use files for everything so the only example I can show will be file-based but it’s recommended to create your Things in PaperUI and Items in files. For your case, you may be able to use the transformation part only and it work? Also, this is my example as you may need to adjust the code in one or all of the transformation files to suit your needs.


Thing topic zigbee2mqtt "Bedroom Light" @ "Bedroom" {
        Type switch : power  "Power"               [ stateTopic="zigbee/0xb0ce1814030ac279", transformationPattern="JSONPATH:$.state",
                                                    commandTopic="zigbee/0xb0ce1814030ac279/set", on="ON", off="OFF" ]
        Type dimmer : dimmer "Dimmer"              [ stateTopic="zigbee/0xb0ce1814030ac279",
                                                    commandTopic="zigbee/0xb0ce1814030ac279/set", transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{\"brightness\":%s}" ]


Switch BedroomLight "Bedroom Light"    <light> ["Lighting"]  { channel="mqtt:topic:pibroker:zigbee2mqtt:power", expire="120m,command=OFF" }
Dimmer BedroomLight_Level "Bedroom Light Level [%.0f %%]"    <light> ["Lighting"]  { channel="mqtt:topic:pibroker:zigbee2mqtt:dimmer" }

Then using js transformation (will need to install this via PaperUI) and place the code in ect/openhab2/transform naming it whatever you like with the .js on the end.




    var result;
    var json = JSON.parse(x);  
    result = json.brightness * 100 / 255;

    return result;



    var brightness = x * 255/100;

    var result = new Object();
    result.brightness = brightness;
    return JSON.stringify(result);



    var result = new Object();
    result.state = x;
    return JSON.stringify(result);

I made it very strange with a javascript transformation:

(function(myFkt) {
value = Number(myFkt);
value = Math.round(value);
string = value.toString(10);
return string;
}) (input)

That was my point earlier, you needed a transformation be it js, javascript or whatever. Glad it’s working as expected and don’t forget to click the square box on solution post.