1

I'm trying to log an event that happens with a Smartthings device into a Firebase database. The function in my Smartthings app is:

def reportSwitchOnHandler(evt){
log.debug "reportSwitchOnHandler called: $evt"

def params = [
    uri: "https://<project-id>.firebaseio.com/switchStateData/params.json?auth=<key>",

    body: [
            switchState: "test"
        ]
]

try {
    httpPostJson(params) { resp ->
        resp.headers.each {
            log.debug "${it.name} : ${it.value}"
        }
        log.debug "DEBUG (POST FIREBASE): response contentType: ${resp.    contentType}"
    }
} catch (e) {
    log.debug "something went wrong: $e"
}

My "params" database structure returned json is:

{"lights":0,"switchState":"off"}

I'm only wanting to update the switchState.

So with that said, when I run the above code, under the "params" structure, rather than updates the switchState child, it creates a new child with some random value like "-Kyn_TIEItKNNACLuEk5" and under that switchState is there with the "test" value.

{"-Kyn_TIEItKNNACLuEk5":{"switchState":"test"},"lights":0,"state":"off"}

Any ideas what I'm missing for it to actually update the original switchState child and not be creating a new sub child (-Kyn_TIEItKNNACLuEk5)?

2 Answers2

1

Using POST pushes data to a location, you need to use PATCH to update the data without overwriting keys that are missing.

curl -X PATCH -d '{"last":"Jones"}' \
'https://[PROJECT_ID].firebaseio.com/users/jack/name/.json'

You can update specific children at a location without overwriting existing data using a PATCH request. Named children in the data being written with PATCH are overwritten, but omitted children are not deleted.

https://firebase.google.com/docs/reference/rest/database/#section-patch

tommybananas
  • 5,718
  • 1
  • 28
  • 48
  • Hey Tom, thanks for replying quickly. I had ran across that in the documents but the problem is that Smartthings doesn't support curl commands. I did some more digging through Smartthings documents and found a beta feature that allows the PATCH command. –  Nov 13 '17 at 13:26
0

For future reference, I found a beta (as of this writing) feature in the Smartthings documents that now allows the PATCH command. Check out here, http://docs.smartthings.com/en/latest/smartapp-developers-guide/async-http.html.