Supereg / homebridge-http-switch

Powerful http switch for Homebridge: https://github.com/homebridge/homebridge
ISC License
219 stars 36 forks source link

help with using the custom statusPattern #51

Closed rezizter closed 4 years ago

rezizter commented 4 years ago

Hi I have a status that is very unstructured

The entries @678ftp , @456gtr and @123df5 are the relay devices. "value": 0 is the status of on or off (shows 1 when on)

what is the correct way to search for a device and then it's status using statusPattern?

{
  "success": true,
  "@678ftp": {
    "type": "RELAY QS-R-S5",
    "firmware": "32",
    "epoch": "1587511930",
    "rssi": "55%",
    "value": 0
  },
  "@456gtr": {
    "type": "RELAY QS-R-S5",
    "firmware": "32",
    "epoch": "1587511930",
    "rssi": "39%",
    "value": 0
  },
  "@123df5": {
    "type": "RELAY QS-R-S5",
    "firmware": "32",
    "epoch": "1587511933",
    "rssi": "58%",
    "value": 0
  }

On and off works perfectly it is the status that is not working. I have the following:

            "statusUrl": "http://qwikswitch.com/api/v1/state/XXX/",
            "statusPattern": "/@456gtr0/\u000balue=1",
            "http_method": "GET"
Supereg commented 4 years ago

The following pattern should work:

"statusPattern": "@456gtr(.|\n)*value\": 1(.|\n)*@123df5"

Note the structure. We check that the response somewhere should contain @456gtr then there can be any possible character ((.|\n)*, including line break) followed by value": 1. If you would leave out the rest it would also match when @456gtr is off and @123df5 is on. So we also include the fact that after the matching value field there still needs to be the @123df5 in the response.

This is one way of doing it. And it assumes that the order of the above entires does not change (meaning @123df5 will always come after @456gtr). You could also try to match the opening and closing brackets, will probably be more error prone but will also require a much more complex pattern I guess.

rezizter commented 4 years ago

Thank you for the reply.

"statusPattern": "@456gtr(.|\n)*value\":1(.|\n)*@123df5` Should I add quotes or anything at the back?

Supereg commented 4 years ago

quotes yes 😅 corrected it

rezizter commented 4 years ago

Seems to still be struggling with seeing on and off. If I turn a light on, then reboot homebridge, it shows the status as off.

I have run the server in debug, but dont see the error. IS there a way to debug statusPattern?

Supereg commented 4 years ago

You can enable the debug option in the config of the plugin, this will show detailed information what the http server responds with and what if the pattern matches or doesn't.

rezizter commented 4 years ago

Works like a charm, I had a typo on my side, your regex is perfect. Thank you soo much for this plugin and the assistance!