openhab-core icon indicating copy to clipboard operation
openhab-core copied to clipboard

[REST API] Enhancement suggestion for Item "transformedState" field

Open Rossko57 opened this issue 4 years ago • 3 comments

Background; when an Item [state presentation] (aka stateDescription/pattern) property includes a transformation, REST API GET items/itemName returns JSON with both raw Item state and transformed Item state. Example, item state=1 and MAPped to text -

{
  "link": "http://localhost:8080/rest/items/vnm_Intruder",
  "state": "1",
  "transformedState": "disarmed",
  "stateDescription": {
    "pattern": "MAP(alarms.map):intruder%s",
    ...

If there is no transformation specified in [state presentation] , the transformedState field is not returned at all.

However, the [state presentation] may call for formatting of state, or more recently for a QuantityType, call for units different from the actual Item. e.g an Item state 20.972 ˚C could be formatted using [%.0f ˚F] and value conversion implied. There is no easy way to get the expected 70˚F for external use.

It seems to me it may be useful to use the existing transformedState JSON field to also supply the "psuedo-transformed" state when no transformation is applied, but a style and/or units conversion is specified in Item [state presentation].

This needs thought about if it is a breaking change, but it is difficult to see that it would be. OH-3 may be a good break point to introduce a change.

Rossko57 avatar Jun 10 '20 21:06 Rossko57

That was actually implemented in the "displayState" introduced in https://github.com/openhab/openhab-core/pull/1364#discussion_r380606310 - so I'm all for it to be performed on the regular API calls as well.

ghys avatar Jun 10 '20 23:06 ghys

Okay, so the alternative change would be to add "displayState" field to regular REST Item JSON

Rossko57 avatar Jun 11 '20 11:06 Rossko57

Anyone has any further thoughts on this? I ran into this myself when I used a webview in Basic UI to create an input field. I only have the state available from the REST API, not the displayState. My displayState does some number formatting on a QuanityType. Here is a sample of the REST API response:

{
  "link": "http://192.168.0.10:8080/rest/items/PVEtotalGS1",
  "state": "25248.837971875 kWh",
  "stateDescription": {
    "pattern": "%.0f %unit%",
    "readOnly": false,
    "options": []
  },
  "editable": true,
  "type": "Number:Energy",
  "name": "PVEtotalGS1",
  "label": "PV Productie Totaal GS1",
  "category": "energy",
  "tags": [
    "Measurement",
    "Energy"
  ],
  "groupNames": [
    "Solar_GS1",
    "PVEtotal"
  ]
}

There is no formattedState or displayState.

Another idea would be to create a specific REST endpoint: /items/{itemName}/displayState, retrieving the displayState (or state if it is equal to displayState). This would make it straightforward to use.

mherwege avatar Apr 08 '22 08:04 mherwege