Help with rule and string within a string

Anyone know how i can pull the value of rad out of this string in a rule to update an item. This is a little different then normal as its a string within a string so the traditional method isnt working…

{"_type":"waypoints","waypoints":[{"_type":"waypoint","tst":1486582266,"lat":42.771794842596,"lon":-78.834592094718,"rad":0,"desc":"Home"}]}

That is a json string!
If you have that string in the variable JSON_String it should be:

  var Number myRad = transform("JSONPATH",  "$.waypoints[0].rad", JSON_String)

The “.waypoints[0]” is calling the first element in the Waypoint array. Your posted string contains one item!

1 Like

Yes im trying to change this rule into that. This rule works for normal waypoint updates but when you manually push update waypoints it sends a string in a string which is different so this rule dont see it…

rule "OwnTracks Waypoint Jay"
  when
    Item JCWayPointData received update
  then
  logInfo("MQTT", "OwnTracks waypoint updated for Jay")
    val String json = (JCWayPointData.state as StringType).toString
    val String type = transform("JSONPATH", "$._type", json)
    if (type == "waypoints") {
      val String desc = transform("JSONPATH", "$.desc", json)
      JCRadius.postUpdate(desc)
      logInfo("OwnTracks", "Jay is at " + desc)
    }
end

The json you posted above has a waypoint of type “waypoint”, the code you posted only reads “transition”.

Yea i know, forget about that part… im just looking for the correct string to use in that rule to pull the data i need when i manually push update waypoint :wink:

i just keep getting “given new state is NULL, couldn’t post update for ‘JCRadius’” cause it cant read the above string properly

You changedthe posted code, however now it will reads “waypoints”, that is the name of the array! You need “waypoint”!

I cant use “waypoint” currently because i cannot read that string in a string. Currently i can only grab the initial “waypoints” from it to use. That’s why i needed help to read that. Then i can change it to the “waypoint” string in there.

This is the manually pushed string: (trying to read this in a rule)

{"_type":"waypoints","waypoints":[{"_type":"waypoint","tst":1486582266,"lat":42.771794842596,"lon":-78.834592094718,"rad":0,"desc":"Home"}]}

and this is the automatically sent string: (no problem reading this in a rule)

{"_type":"waypoint","tst":1486582266,"lat":42.771794842596,"lon":-78.834592094718,"rad":0,"desc":"Home"}

see the difference?

I don’t get your problem
The first one can be read with:

var Number myRad = transform(“JSONPATH”, “$.waypoints[0].rad”, JSON_String)

the second with

var Number myRad = transform(“JSONPATH”, “$.rad”, JSON_String)

I used this one to test:

This is what i was looking for but how do i use it in the rule? just remove the similar val thats in there?

var Number myRad = transform("JSONPATH",  "$.waypoints[0].rad", JSON_String)

All depends on how the json string is posted to the rule! As stated above, the variable JSON-String expects the json as posted above (first example).
I’d use some lines like

logInfo(“CheckMyRule”, “MyCheckedVariable = {}”, MyCheckedVariable)

Make a test-run and look into the log! (Using karafs log:tail!)

Its posted exactly like this:

{"_type":"waypoints","waypoints":[{"_type":"waypoint","tst":1486582266,"lat":42.771794842596,"lon":-78.834592094718,"rad":0,"desc":"Home"}]}

Trying this with no luck…

rule "OwnTracks Waypoint Jay"
  when
    Item JCWayPointData received update
  then
  logInfo("MQTT", "OwnTracks waypoint updated for Jay")
    val String json = (JCWayPointData.state as StringType).toString
    var Number myType = transform("JSONPATH",  "$.waypoints[0]._type", JSON_String)
    if (mytype == "waypoint") {
      val String myRad = transform("JSONPATH",  "$.waypoints[0].rad", JSON_String)
      JCRadius.postUpdate(rad)
      logInfo("OwnTracks", "Jay is at " + desc)
    }
end

Error during the execution of rule ‘OwnTracks Waypoint Jay’: The name ‘JSON_String’ cannot be resolved to an item or type.
2017-02-11 11:26:08.331 [WARN ] [.c.i.events.EventPublisherImpl] - given new state is NULL, couldn’t post update for 'JCRadius’
2017-02-11 11:26:08.562 [INFO ] [openhab.model.script.OwnTracks] - Jay is at null

Tried this also,

rule "OwnTracks Waypoint Jay"
  when
    Item JCWayPointData received update
  then
  logInfo("MQTT", "OwnTracks waypoint updated for Jay")
    val String json = (JCWayPointData.state as StringType).toString
    var String myType = transform("JSONPATH",  "$.waypoints[0]._type", JSON_String)
    if (myType == "waypoint") {
      val String myRad = transform("JSONPATH",  "$.waypoints[0].rad", JSON_String)
      JCRadius.postUpdate(myRad)
      logInfo("OwnTracks", "Jay is at " + desc)
    }
end

You used the name “JSON_String” from my example, however your string is named “json”!!!

1 Like

owwwww let me try

You are the man! thanks so much!!!

Cheers!

Works like a charm for manual waypoint sending.

rule "OwnTracks Waypoint Jay"
  when
    Item JCWayPointData received update
  then
  logInfo("MQTT", "OwnTracks waypoint updated for Jay")
    val String json = (JCWayPointData.state as StringType).toString
    var String myType = transform("JSONPATH",  "$.waypoints[0]._type", json)
    if (myType == "waypoint") {
      val String myDesc = transform("JSONPATH",  "$.waypoints[0].desc", json)
      JCRadius.postUpdate(myDesc)
      logInfo("OwnTracks", "Jay is at " + myDesc)
    }
end