Percentage changes to decimal with Homie

  • Platform information:
    • Hardware: _ RPi4 4GB
    • openHAB version:2.5.4-1

Hello everyone,

I have an issue when I want to transfer humidty data from my ESP32 to openHAB via MQTT and Homie. The value gets converted to decimal instead of staying percentage.

I use Leifhomielib for the ESP32. It is working fine for everything except percent-values.

The homie node looks like this:
grafik
Unit is set to %.

openHAB is receiving the value as decimal instead of percentage:

2020-04-29 22:11:44.291 [vent.ItemStateChangedEvent] - OU_Backyard_RelativeHumidity changed from 0.5009 % to 0.5005 %

This is the item:

Number:Dimensionless          OU_Backyard_RelativeHumidity         "Relative Luftfeuchte [%.1f %%]"                                           <humidity>                      (OU_Backyard)                                                             			       {channel="mqtt:homie300:mosquitto:test-sensornode:barometer#relativehumidity"}

What is wrong here?

Another question:
How can i give an item a custom unit?
For example if i get the absolute humidty from the ESP32 (unit g/m3).
grafik

Number          			  OU_Backyard_AbsoluteHumidity         "Absolute Luftfeuchte [%.2f g/mÂł]"                                         <humidity>                      (OU_Backyard)                                                             			       {channel="mqtt:homie300:mosquitto:test-sensornode:barometer#absolutehumidity"}

The log viewer doesn’t display a unit:

2020-04-29 22:16:36.307 [vent.ItemStateChangedEvent] - OU_Backyard_AbsoluteHumidity changed from 10.78 to 10.79

How can i get the unit there? Although it is showing correctly in the sitemap.

Thanks!

Best regards

I do not think the base MQTT binding yet supports UoM

1 Like

I realized now that this wasn’t a problem before.
On my second stable installation the percent values get changed to decimals too.
This must have happened through an update.
Unfortunately I don’t know how to contribute to solve the issue. I can only give out logs.

I tried a workaround and just multiplied the outgoing value times 100.
openHAB is realizing it is out of range now.

2020-04-30 08:53:07.883 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command '5434.96' not supported by type 'PercentageValue': Value must be between 0 and 100

My second question is solved:

2020-04-30 08:48:38.645 [vent.ItemStateChangedEvent] - OU_Backyard_AbsoluteHumidity changed from 11.32 g/mÂł to 11.29 g/mÂł
Number:Dimensionless          OU_Backyard_AbsoluteHumidity         "Absolute Luftfeuchte [%.2f g/m^3]"                                        <humidity>                      (OU_Backyard)                                                             			       {channel="mqtt:homie300:mosquitto:test-sensornode:barometer#absolutehumidity"}

I havn´t tried myself, but I would assume a JS script in the transformation path for the thing could be used to multiply by 100. It works for modbus, where I use it. But I´m not sure MQTT support it (It should do as default, in my opinion).

This is how it looks like in modbus2 binding:

Thing data inp204 [ readStart="204", readValueType="int16", readTransform="JS(divide100.js)" ]

This is the script for multiply by 100:

filename:
multiply100.js

// Wrap everything in a function
(function(i) {
    return Math.round(parseFloat(i, 10) * 100);
})(input)
// input variable contains data passed by openhab

So if you insert this part into your thing setup…

readTransform="JS(multiply100.js)"

copy the multiply100.js to your transform folder
Make sure you have the transformation service added.

… it might work.

If not… I would say some maintainer of mqtt have a job to do :smiley:

1 Like

Thanks!
I will try this workaround.
But still there is a problem with MQTT which wasn’t there before.

I´m still at openhab 2.5.0 (stable). So I cant say if this is a newer openhab problem. MQTT has been changed so it might be a MQTT issue/change.

As of 2.5.4 Homie properties sent with $unit % will now function as expected. Prior to 2.5.4 the Homie binding divided percentage values by 100.

1 Like

Sounds great but I can’t confirm this yet. I am using the stable release of 2.5.4 and it is not working. Do I have to install some milestone?

I am on OH 2.5.4-1

Need to clarify - are you having issues with your humidity item?

1 Like

I think I have the same build as you:


It is happening to all humidity items through Homie.
This one for example:

As you can see. A value of 54.40 in sent via MQTT with unit %.
OpenHab converts it to 0.544 %:

This it the item:

Number:Dimensionless          GF_Terrace_Humidity                 "Rel. Luftfeuchte [%d %%]"                                                  <humidity>                      (GF_Terrace, gHumidity, Weather)                                    ["CurrentHumidity"]      {channel="mqtt:homie300:mosquitto:terrace-sensornode:thermohygrometer#humidity"}

And the Homie Things:

    Thing homie300  bedroom-sensornode   "Schlafzimmer Sensorknoten" @ "Schlafzimmer" [ deviceid="bedroom-sensornode" ]

    Thing homie300  bathroom-sensornode  "Badezimmer Sensorknoten"   @ "Badezimmer"   [ deviceid="bathroom-sensornode" ]

    Thing homie300  terrace-sensornode   "Terrasse Sensorknoten"     @ "Terrasse"     [ deviceid="terrace-sensornode" ]

Every other item from this Homie device is working as expected. Only humidity has problems by dividing the value by 100.
Does your item look similar?
Thanks for assisting me! :smiley:

Yes, can confirm its a bug when using an item type of number. If you use Dimmer it works. Would you open report on github?

1 Like

Done!