Maker API html push json attribute

What I'm trying to do: When a contact sensor reports the status "open", I'm doing a HTML push with rulemachine.
It then does a HTML push to my PRTG server to get an active alert in PRTG.

This works like a charm when I filter on the value (JSONPath): $.attributes[1].currentValue witch gives me the value "closed" or "open".

(example)

https://[HUB-IP]/apps/api/[APP-ID]/devices/[DEVICE-ID]/capabilities?access_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

[
  {
    "capabilities": [
      "Configuration",
      "Refresh",
      "ContactSensor",
      {
        "attributes": [
          {
            "name": "contact",
            "currentValue": "closed",
            "dataType": "ENUM",
            "values": [
              "closed",
              "open"
            ]
          }
        ]
      },
      "Battery",
      {
        "attributes": [
          {
            "name": "battery",
            "currentValue": "99",
            "dataType": "NUMBER",
            "values": null
          }
        ]
      },
      "AccelerationSensor",
      {
        "attributes": [
          {
            "name": "acceleration",
            "currentValue": "inactive",
            "dataType": "ENUM",
            "values": [
              "inactive",
              "active"
            ]
          }
        ]
      },
      "ThreeAxis",
      {
        "attributes": [
          {
            "name": "threeAxis",
            "currentValue": null,
            "dataType": "VECTOR3",
            "values": null
          }
        ]
      },
      "TemperatureMeasurement",
      {
        "attributes": [
          {
            "name": "temperature",
            "currentValue": "22.88",
            "dataType": "NUMBER",
            "values": null
          }
        ]
      },
      "Sensor"
    ]
  }
]

Now my problem: sometimes Maker API seems to do a switcheroo on the attribute sequence.
So $.attributes[1].currentValue now does not give me the contact sensor's open/closed value, but it gives me the battery value.
I then have to manualy request maker api what the new sequence of attributes is.

(example)

https://[HUB-IP]/apps/api/[APP-ID]/devices/[DEVICE-ID]/capabilities?access_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

[
  {
    "capabilities": [
      "Configuration",
      "Refresh",
      "Battery",
      {
        "attributes": [
          {
            "name": "battery",
            "currentValue": "99",
            "dataType": "NUMBER",
            "values": null
          }
        ]
      },
      "ContactSensor",
      {
        "attributes": [
          {
            "name": "contact",
            "currentValue": "closed",
            "dataType": "ENUM",
            "values": [
              "closed",
              "open"
            ]
          }
        ]
      },
      "AccelerationSensor",
      {
        "attributes": [
          {
            "name": "acceleration",
            "currentValue": "inactive",
            "dataType": "ENUM",
            "values": [
              "inactive",
              "active"
            ]
          }
        ]
      },
      "ThreeAxis",
      {
        "attributes": [
          {
            "name": "threeAxis",
            "currentValue": null,
            "dataType": "VECTOR3",
            "values": null
          }
        ]
      },
      "TemperatureMeasurement",
      {
        "attributes": [
          {
            "name": "temperature",
            "currentValue": "22.88",
            "dataType": "NUMBER",
            "values": null
          }
        ]
      },
      "Sensor"
    ]
  }
]

So in the example the attribute number for open/closed is now: $.attributes[2].currentValue

Is there a way around this, or is there a way Maker API always respects the same sequence of attributes?

Maybe try (untested by me):

$.attributes[?(@.name == 'contact')].currentValue

so that you're not relying on key order?

5 Likes

That did the trick, thx!

1 Like