PercentageType displays as decimal (less than 1)

I am developing a binding and want to show a percentage value.
However, the value shows in the UI as a number between 0 and 1.

I have defined my item…

	<channel-type id="airconditioner-zone-flow">
		<item-type>Number</item-type>
		<label>Zone AirFlow</label>
		<description>The Zone Flow (percentage damper open).</description>
		<category>Slider</category>
		<state min="5" max="100" step="5" pattern="%d %%" readOnly="false"/>
	</channel-type>

and set it in my handler…

    updateState(new ChannelUID(channelGroupUID, CHANNELUID_AIRCONDITIONER_ZONE_FLOW),
            new PercentType(zone.getOpenPercentage()));

The value returned from zone.getOpenPercentage() is an integer between 0 and 100.

The data I get from the AC unit is whole numbers

ZoneStatus [messageType=ZONE_STATUS, openPercentage=50, powerstate=OFF, zoneNumber=3, controlMethod=TEMPERATURE_CONTROL, batteryLow=false, turboSupported=false, targetSetpoint=17, hasSensor=true, currentTemperature=16, spill=false]

When it shows in the UI it shows as 0.5 %.
I am expecting it to show as 50 %

Looking at the items in the REST API, I can see the following…

  {
    "link": "http://openhab:8080/rest/items/AirTouch4_Zone_AirFlow__Fred",
    "state": "0.5 %",
    "stateDescription": {
      "minimum": 5,
      "maximum": 100,
      "step": 5,
      "pattern": "%d %%",
      "readOnly": false,
      "options": []
    },
    "editable": true,
    "type": "Number:Dimensionless",
    "name": "AirTouch4_Zone_AirFlow__Fred",
    "label": "Zone AirFlow - Fred",
    "category": "flow",
    "tags": [
      "OpenLevel",
      "Opening"
    ],
    "groupNames": [
      "FirstFloor",
      "FredBedroom"
    ]
  },

How can I get OH to display the value correctly?
I don’t want my users to have to go and add rules or scripts just to render the values correctly.

You need to define the unit to “%” for your item, a new feature of OH4.

Thanks for your reply @Lolodomo

At the moment, I am running openHAB 3.4.1

If I was running 4.0, where would I define that? I’m hoping to set sensible defaults as the add-on developer so that users don’t need to worry about it.

Would it make a difference if channel and item type is Number:Dimensionless?

I too am working on a binding with percentage value, namely relative humidity.
The API gives me a double value between 0 and 100 and in my case, I had to divide it by 100 to show properly in the UI or I would get 5420% RH instead of 54.2%.

I have the following definitions:

channels.xml

	<channel-type id="humidity">
		<item-type>Number:Dimensionless</item-type>
		<label>@text/channel-type.airzone.zone.humidity.label</label>
		<category>Humidity</category>
		<tags>
			<tag>Measurement</tag>
			<tag>Humidity</tag>
		</tags>
		<state pattern="%.1f %%" readOnly="true" />
	</channel-type>

thing-type-zone.xml

	<thing-type id="zone">
		<channels>
			<channel id="humidity" typeId="humidity" />
		</channels>
	</thing-type>

Then in the thing handler code, I do this:

newState = new DecimalType(zone.getHumidity() / 100);
updateState(channelUID, newState);

This works right in openHAB4 and I believe it will do so in openHAB3 as well.

Well, dividing by 100 is not working properly in all cases, one should really use a QuantityType<> to return a real percent value. I now have the following code:

newState = new QuantityType<>(zone.getHumidity(), Units.PERCENT);

This way it fits in the openHAB ecosystem much better

2 Likes

Yes, new QuantityType works perfectly. Thank you!

I also changed the channel-type definition to Number:Dimensionless to be consistent with what you’re
using.