WebHook for device measurement change, or another way of doing this?

I want to track historic temperatures from my 5 thermometers linked to my SmartThings hub.

I realise that although this information is available via the web-interface on the smartthings hub, it doesn’t seem to be accessible via the API (which is a bit annoying)

I’ve reverted to trying to get a notification sent to a web-service via a callback whenever a device’s reading/measurement changes. Then I can filter and log these changes myself.

I’ve created a personal token to allow me to use the API, and I’m able to pull back a list of devices, and some device information/status.

What I’d like to be able to do is set a webhook against all of these thermometers.

Is it possible to do this with just the API? I was hoping for an end-point that allowed me to say something like…

deviceId=123123-123123-12313; measurement=temperatureUnit.temperature.value; callback=http://my_web_address.com/dealwith_st

Is this sort of thing possible, or do I have to do this via a SmartApp created on the web-interface?

Hi, @richierich_london

As you mention at the end, this approach can be done from the Device Subscription (in this case) through a SmartApp.

Hope this approach serves you well,
Erick.

You can do this through webCoRE.

Or through IFTTT as well to log the temp to Google Sheets when the temp changes.

Thanks @erickv,

I’ve spent the last few hours creating a simple webhook (with new certificates/server etc…), and tried to create a new app.

My webhook is working, but when I make the API call (to create a new app), it says that ‘Upstream target timed out’.

I’ve done a bit more reading on this, and it mentions something about a PING request (relating to lifecycles)

Do I have to implement lots of other calls to get a simple notification from a device that something has changed?

In this case, yes. Because when your subscriptions get triggered, they’ll call the SmartApp EVENT handler.

After much persistence, I now have an app that I can install on my mobile phone, but whenever I make the API call to subscribe to an event, it returns a 403 error (with no further information).

POST https://api.smartthings.com/v1/installedapps/my-installed-id/subscriptions

body = {“sourceType”:“DEVICE”,“device”:{“deviceId”:“my-device-id”}}

Status:403

Have you got any ideas?

Hi @richierich_london

  • Is this POST request being done from the INSTALL lifecycle?

  • Check if you have all scopes enabled at the DevWorkspace project. Also, I’d recommend you to send a full body, e.g.:

{
  "sourceType": "DEVICE",
  "device": {
    "deviceId": "736e3903-001c-4d40-b408-ff40d162a06b",
    "componentId": "main",
    "capability": "switch",
    "attribute": "switch",
    "stateChangeOnly": true,
    "value": "on"
  }
}

@erickv - thanks for getting back to me quickly :slight_smile:

I was doing the POST request separately, I saw in the documentation that the subscription is ‘normally’ done in either install or update, but I didn’t get the impression this was mandatory?

I did originally have the full body, but removed the optional parameters to try to eliminate any possible errors.

Should I try to call the POST request from within the install lifecycle then?

Creating subscription from INSTALL, will guarantee that the subscription will be created as soon as you save your SmartApp (if no error in between). While at UPDATE, you’ll need to modify your SmartApp configuration (at st app) to trigger the UPDATE lifecycle.

But triggering the subscription only in Install means you can’t modify your subscriptions either. You could just have your subscriptions inside update and then call update from Install. That’s what I do.

def install(){
     updated()
}

def updated(){
     subscribe (x, Y, z)
     subscribe (a,b,c)
}
1 Like

@erickv I’m still getting the same 403 error when trying to create the subscription from within the INSTALL lifecycle.

The json I’m posting is…

{
    "sourceType": "DEVICE",
    "device": {
        "deviceId": "xxxxxxxx-xxxx-4268-9d37-ffffa3625bcd",
        "componentId": "main",
        "capability": "temperatureMeasurement",
        "attribute": "temperature",
        "stateChangeOnly": true,
        "subscriptionName": "mainevent"
    }
}

I’ve got permissions for r/w/x:devices and r:locations, could I be missing another permission?

I’ve just had a response from SmartThings support, and they were able to replicate my issue, and found that I was using my ‘Personal Access Token’ rather than the authToken that comes through in the ‘INSTALL’ lifecycle.

It’s now installed, and I’m receiving events from the device :smiley:

1 Like