Get state from JSON mqtt item in a rule

Hi all,

I didn’t know where to post this because it touches so many subjects but since I am trying to do it in a rule ill post it here.

So I have a MQTT item that receives a big payload of JSON formatted stuff

Item:
String Bluez “[%s]” {mqtt="<[happybubblesmqtt:happy-bubbles/presence/changes:state:default]"}

Payload:
{
   "beacon_info": {
      "name": "undefined",
      "beacon_id": "c869cddcf85e",
      "beacon_type": "",
      "beacon_location": "",
      "last_seen": 1500049168,
      "incoming_json": {
         "hostname": "happy-bubbles-ble",
         "mac": "c869cddcf85e",
         "rssi": -64,
         "is_scan_response": "0",
         "type": "0",
         "data": "02011a0bff4c0009060319c0a80103",
         "beacon_type": "",
         "uuid": "",
         "major": "",
         "minor": "",
         "tx_power": "",
         "namespace": "",
         "instance_id": ""
      },
      "distance": 0,
      "Previous_location": "happy-bubbles-ble",
      "Previous_confident_location": "",
      "Location_confidence": 0
   },
   "name": "undefined",
   "beacon_name": "undefined",
   "previous_location": "",
   "new_location": "happy-bubbles-ble",
   "timestamp": 1500049168
}

I’ve created a rule that looks for a change to that item. What i am looking to do is to get the value of the “new _location” JSON item, but can’t seem to get it working and am totally lost. Any suggestions?

rule "Bluez"
when
	Item Bluez changed
then
   var String json = (Bluez.state as StringType).toString
   var String type = transform("JSONPATH", "$.[new_location]", json)
   BluezStatus.postUpdate(type)

   
end

BluezStatus is another item, mainly for debugging purposes
String Status “[%s]”

I don’t know why you are trying to treat new_location like an array. Just use

"$.new_location"

@rlkoshak
Ok I removed the brackets and it is now returning stuff but its the entire json string that it returns. Its like its not parsing it. Does there need to be any special installation to use JSONPATH?

Have you installed the JSONPath Transformation addon from PaperUI?

Always look at the logs. If you have not installed the JSONPATH transform, there will be errors in the logs telling you what is wrong.

@rlkoshak @Dim I do have the JSONPath transformation addon enabled in the PaperUI

When I look at the logs i get:

16:31:21.099 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'Bluez.rules'
16:31:21.119 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Bluez.rules' is either empty or cannot be parsed correctly!
16:31:21.369 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'Bluez.rules'

This is the Bluez.rules file when it was logged:

rule "Bluez"
when
	Item Bluez changed
then

   var String type = transform("JSONPATH", "$.new_location", Bluez.state.toString)
   BluezStatus.postUpdate(type)
   	


   
end