Update of PercentType shows incorrect value

Hi,

currently I am trying to update the state of an item with type Number:Dimensionless. The logging shows that I get the correct number (19) but when I update the state of my item a value of 0.19 is shown.
Is this the intended behavior for percentage values?
If yes could you please give me a hint how I can format it in the UI to show it as ‘19 %’?

2023-04-02 14:32:20.184 [INFO ] [inding.devolo.internal.DevoloHandler] - try to update channel stats-cpu
2023-04-02 14:32:20.184 [INFO ] [inding.devolo.internal.DevoloHandler] - new value 19 for channel stats-cpu
2023-04-02 14:32:20.185 [INFO ] [inding.devolo.internal.DevoloHandler] - int value 19 for channel stats-cpu

In code I set the value as follows:

...
case STATS_CPU_CHANNEL:
    value = dataState.getProperty(STATS_CPU_KEY);
    logger.info("new value {} for channel {}", value, channelID);
    int cpuLoad = Integer.valueOf(value);
    logger.info("int value {} for channel {}", cpuLoad, channelID);
    state = new PercentType(cpuLoad);
    updateState(channelID, state);
    break;

Thank you for your help

Best regards,
Patrick

Yes. 19% is 0.19 and no unit.

Add a literal ‘%’ to your log command.
Don’t confuse your debug output with all the conversion stuff going on when you display an item in a widget or sitemap.

Thank you for the fast reply.

I don’t want to add the ‘%’ to my log comments.
I would like that the state of the item is displayed as ‘19 %’ in a UI widget.
Changing the state description pattern for example to ‘%d%%’ (with or without a space) still shows 0.19.
Any idea what I am doing wrong?

I use the 4.0.0-SNAPSHOT docker image: openHAB 4.0.0 Build #3387

Query the item via API results in the following JSON document:

{
  "link": "http://openhab/rest/items/Devolo_Magic_2_Lan_11_Thing_Status_CPU_Load",
  "state": "0.19 %",
  "stateDescription": {
    "pattern": "%d %%",
    "readOnly": false,
    "options": []
  },
  "editable": true,
  "type": "Number:Dimensionless",
  "name": "Devolo_Magic_2_Lan_11_Thing_Status_CPU_Load",
  "label": "Status CPU Load",
  "category": "",
  "tags": [
    "Point"
  ],
  "groupNames": []
}

Ahh I think I found a solution for my problem.

I changed my code from PercentType to QuantityType and than I get the value correctly formated in a widget:

state = QuantityType.valueOf(value+"%");

Is there some documentation which Type classes should be used in which case?

Thank you and best regards
Patrick

Then you should have said that and should not have given a debug log example.

There’s many ways.
The default would be to create a display pattern. That’s the pattern field in metadata of the item as documented e.g. here.

You should be reading the full OH documentation, it’s all there.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.