JSONPATH - return number value

Hi all,

i was wondering if it is possible to get a number value to the item extracted by the JSONPath transform service in the ItemChannelLink Profile. See details below.

As of now i only get a string, which cannot be displayed in diagrams etc…

  • Platform information:

    • Hardware: Synology DS1513+
    • OS: DSM6
    • Java Runtime Environment: 8 (I think)
    • openHAB version: 2.4
  • Issue of the topic:
    Channel is configured as string, to receive:
    {“Time”:“2019-06-27T09:26:34”,“AM2301”:{“Temperature”:23.2,“Humidity”:87.3},“TempUnit”:“C”}

I configured the ItemChannelLink to extract Time, Temperature and Humidity from this JSON Sting, which works like a charm. But now my issue: The Item gets the values as string and cannot convert them to numbers, so they can be stored correctly.

I also tried to add ‘:%.3f’ to the function, which also does not work.

ItemLinkConfig:

 "MarkusLivingroomNodeMCUT1Temperature -\u003e mqtt:topic:85dcec01:NodeMCUT1_Sensor": {
        "class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink",
        "value": {
          "channelUID": {
            "segments": [
              "mqtt",
              "topic",
              "85dcec01",
              "NodeMCUT1_Sensor"
            ]
          },
          "configuration": {
            "properties": {
              "profile": "transform:JSONPATH",
              "function": "$.AM2301.Temperature",
              "sourceFormat": "%.3f"
            }
          },
          "itemName": "MarkusLivingroomNodeMCUT1Temperature"
        }
      },

ItemConfig:

  "MarkusLivingroomNodeMCUT1Temperature": {
    "class": "org.eclipse.smarthome.core.items.ManagedItemProvider$PersistedItem",
    "value": {
      "groupNames": [
        "LivingRoom",
        "MarkusLivingRoomNodeMCUT1"
      ],
      "itemType": "Number:Temperature",
      "tags": [],
      "label": "Markus - LivingRoom - NodeMCUT1 - Temperature",
      "category": "Temperature"
    }
  },

Channel Config:

 {
          "acceptedItemType": "String",
          "kind": "STATE",
          "uid": {
            "segments": [
              "mqtt",
              "topic",
              "85dcec01",
              "NodeMCUT1_Sensor"
            ]
          },
          "channelTypeUID": {
            "segments": [
              "mqtt",
              "string"
            ]
          },
          "label": "NodeMCUT1 Sensor",
          "configuration": {
            "properties": {
              "postCommand": false,
              "retained": false,
              "formatBeforePublish": "%s",
              "commandTopic": "",
              "allowedStates": "",
              "stateTopic": "/kristenhome/markus/livingroom/NodeMCUT1/tele/SENSOR",
              "transformationPattern": ""
            }
          },
          "properties": {},
          "defaultTags": []
        },

Error Message:

2019-06-27 10:32:30.908 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"Time":"2019-06-27T09:26:34","AM2301":{"Temperature":23.2,"Humidity":87.3},"TempUnit":"C"}' with function '$.AM2301.Temperature' and format '%.3f'

Welcome to the openHAB Forum.

I have setup my tasmota-sonoffs using .things files. I used number channels and number items for temperature and humidity and they are showing correctly in the charts (persisted with rrd4j).

Yeah … that’s my backup-plan :slight_smile:

The only possible difference I see compared to my setup (as I can spot from reading your json output) is the use of item type “Number:Temperature” .That MIGHT be forcing to use UnitsOfMeasurement. However that is an assumption only.

Type “Number” without Temperature doesn’t affect the output.

I think the real problem is the persistence service.

Error message in the log without any formating attempt in the config files is now:

java.lang.Double cannot be cast to java.lang.String.

I tried to drop the existing MariaDB tables and let Openhab recreate them, but without success. Same error.

A question:

You posted the contents of the files that are stored by PaperUI, confirm you do the setup via PaperUI and not via those files files directly? Those files are NOT to be edited/changed by the user while openHAB is running!

As stated above I am using rrd4j as a database, which is working without tweaks concerning such data. However, the principle setup of MariaDB for openHAB should be the same. Please Show your setup files for MariaDB.

https://github.com/openhab/openhab2-addons/issues/5750

So, some updates:
I changed to rrd4j persistence service
I changed from JSONPath to JS transofrmation.

Here is my JS file, which I can confirm is beeing called

(function(i){
    var parsed = JSON.parse(i);
    return (parseFloat(parsed.AM2301.Temperature))
}) (input)

But nontheless, the value does not arrive as numeric at the item, dispaying ‘NaN’ as Number and

23.6’
as string.

Where are you doing the transform? As previously, profile transform does not currently work with Number Items.
If the binding in question allows, you can do it in the thing channel config instead. Using a channel type that permits linking to a Number type.

As the issue rossko57 points out, transforms only return a String. If you want a Number you must parse the String. If you assign the String to a Number Item, it will be automatically parsed for you. You can save the Number Item to persistence and numerical values will be saved and charted.

However, if the value returned by the transform is not just a number (e.g. has blank spaces before or after, its in quotes, etc) then you will get an error. Your transform needs to process the String so it only includes the number parts and nothing else. I believe your problem is that the transform is returning extra stuff.

One thing to note is the 2.5 M1 version of the MQTT binding can chain transformations so you could extract the value using JSONPATH and then use REGEX to strip off any extra stuff like quotes or spaces that might be along for the ride.

If the String value is parsable then you can assign the String to a Number Item and the Number Item will parse the String into a numerical value.