Receiving and parsing webhook POST

So use the JSONPATH transform if you only need one value out of that JSON, or trigger a Rule on the Item that receives the full JSON string and parse out the parts you need and update or command other Items as necessary. You can use the JSONPATH transform from Rules as well.

Here is an example I just wrote today to get rid of a bunch of errors in my logs because sometimes an HTTP request I make returns an empty JSON “”.

NOTE: This is in JSR223 Jython, not Rules DSL.

from core.rules import rule
from core.triggers import when
from core.actions import Transformation

@rule("Handle empty results",
      description="Accept the full JSON and parse in this Rule.",
      tags=["nightscout"])
@when("Item NS_Reading received update")
def ns_reading(event):
    json = str(event.itemState)
    lr_time = "UNDEF"
    lr      = "UNDEF"
    trend   = "UNDEF"

    if json != "[]":
        lr_time = Transformation.transform("JSONPATH", "$[0].dateString", json)
        lr = Transformation.transform("JSONPATH", "$[0].sgv", json)
        trend = Transformation.transform("JSONPATH", "$[0].direction", json)

    events.postUpdate("NS_LastReading_Time", lr_time)
    events.postUpdate("NS_LastReading", lr)
    events.postUpdate("NS_Trend", trend)
1 Like