bitfocus / companion-module-generic-http

Generic HTTP module
MIT License
9 stars 19 forks source link

Parse Variable Results #53

Open ukiews opened 6 months ago

ukiews commented 6 months ago

When I send an http request I get the following response that is saved in a variable:

{"id":"8","name":"Zooz ZEN04 Smart Plug","label":"Projector - Power","type":"Zooz ZEN04 Smart Plug","room":"Main Hall","attributes":[{"name":"power","currentValue":19,"dataType":"NUMBER"},{"name":"voltage","currentValue":122,"dataType":"NUMBER"},{"name":"amperage","currentValue":0,"dataType":"NUMBER"},{"name":"energy","currentValue":0,"dataType":"NUMBER"},{"name":"frequency","currentValue":null,"dataType":"NUMBER"},{"name":"switch","currentValue":"on","dataType":"ENUM","values":["on","off"]}],"capabilities":["Configuration","Actuator","VoltageMeasurement",{"attributes":[{"name":"voltage","dataType":null},{"name":"frequency","dataType":null}]},"CurrentMeter",{"attributes":[{"name":"amperage","dataType":null}]},"Refresh","PowerMeter",{"attributes":[{"name":"power","dataType":null}]},"EnergyMeter",{"attributes":[{"name":"energy","dataType":null}]},"Switch",{"attributes":[{"name":"switch","dataType":null}]}],"commands":["configure","off","on","refresh"]}

I need to be able to parse to extract only the "currentValue" of the "switch" (here "on") How do I go about doing this?

ukiews commented 6 months ago

To clarify, I am using Maker API from Hubitat. If there is another, more simple way to pass the switch state to Companion, please let me know.

Julusian commented 6 months ago

I expect the internal: Custom Variable: Set from a stored JSONresult via a JSONpath expression action will be able to help with this

ukiews commented 6 months ago

Now I'm having another issue. The JSON structure order keeps changing, so the location of the on/off state keeps moving around. Initially it was: $.[5].currentValue

Then I had to change it to: $.[2].currentValue

Now the on/off state is at: $.[0].currentValue

Is there any way to lock this in? Maker API simply puts all the parameters under "Attributes," so it does not seem possible to reference the on/off state by name.

Perhaps there is another way to check if string contains ["currentValue":"on"] for feedbacks?

peternewman commented 1 month ago

@ukiews you should be able to match based on the name field to get switch each time, see here or similar: https://www.baeldung.com/guide-to-jayway-jsonpath#1-getting-object-data-given-ids

I expect the internal: Custom Variable: Set from a stored JSONresult via a JSONpath expression action will be able to help with this

For anyone else wondering, see https://github.com/bitfocus/companion-module-generic-http/issues/56#issuecomment-2146954759 for how to do this now.