State refresh in the SmartThings Schema Cloud Connector

The device status update does not take effect. Device registration is successful and I get all the Tokens and callbacks I need. By responding the “stateRefreshRequest”, I get the values of the device’s sensors added to the SmartApp. When I try to update (State refresh) the data, nothing happens. I don’t even get an respond data.

If I have made mistakes in the device registration, I have always received the respond that something is wrong. When I use this callback (https://c2c-us.smartthings.com/oauth/token). StateCallback doesn’t answer anything. What am I doing wrong here?

I send the following data with the POST command.
URL = stateCallback = https://c2c-us.smartthings.com/device/events

Data = body: {
        headers: {
          schema: "st-schema",
          version: "1.0",
          interactionType: "stateCallback",
          requestId: "abc-123-4567",
        },
        authentication: {
          tokenType: "Bearer",
          token: tokenObj.authToken,
        },
        deviceState: [
          {
            externalDeviceId: deviceId,
            states: deviceData,
          },
        ],
      }


deviceData = [
  {
    "component": "main",
    "capability": "st.equivalentCarbonDioxideMeasurement",
    "attribute": "equivalentCarbonDioxideMeasurement",
    "value": 200,
    "unit": "ppm"
  },
  {
    "component": "main",
    "capability": "st.airQualitySensor",
    "attribute": "airQuality",
    "value": 10,
    "unit": "CAQI"
  },
  {
    "component": "main",
    "capability": "st.temperatureMeasurement",
    "attribute": "temperature",
    "value": 22,
    "unit": "C"
  },
  {
    "component": "main",
    "capability": "st.relativeHumidityMeasurement",
    "attribute": "humidity",
    "value": 30,
    "unit": "%"
  },
  {
    "component": "main",
    "capability": "st.fineDustSensor",
    "attribute": "fineDustLevel",
    "value": 400,
    "unit": "μg/m^3"
  },
  {
    "component": "main",
    "capability": "st.tvocMeasurement",
    "attribute": "tvocLevel",
    "value": 300,
    "unit": "ppm"
  },
  {
    "component": "main",
    "capability": "st.battery",
    "attribute": "battery",
    "value": 40,
    "unit": "%"
  }
]

Hi, @Heikki_Pirttinen.

I’m confused about this part, does this mean you are sending the stateCallback to the oauthToken callbackUrls?

This URL is the correct one. Do you get any response code from the HTTP request?

Make sure you’re using the following:

  1. The reciprocal Access Token in the authentication.token property
  2. You’re using the correct callback URL. It is included in the grantCallbackAccess response when you get the reciprocal access token.
  3. The correct deviceId. Although, you should get an error if it doesn’t match any of the devices discovered.
  4. Print the content of the request to validate it is using the correct format. Using the SDK, I printed the content and it shows the following:
{
  method: 'POST',
  headers: { 'Content-Type': 'application/json; charset=utf-8' },
  body: '{"headers":{"schema":"st-schema","version":"1.0","interactionType":"stateCallback","requestId":"..."},"authentication":{"tokenType":"Bearer","token":"long string"},"deviceState":[{"externalDeviceId":"smartThermostat4","states":[{"component":"main","capability":"st.thermostatMode","attribute":"thermostatMode","value":"eco"},...]}]}'
}

I tried sending the one you posted. I get the following in response.

    "globalError": {
        "errorEnum": "INVALID-TOKEN",
        "detail": "access token provided is malformed or call is made to wrong callback URL"
    }

Looks right. When I replace the Token with the correct Token, I get an empty response {}.

The problem is that if the deviceStatus is wrong, there is no error. The POST is accepted even if the deviceStatus is wrong. The SmartApp Cloud Connector returned an error if there were errors in the content.

I just noticed that the error messages are coming to my callback address.

Hi, @Heikki_Pirttinen. Thanks for following up

So, what error did you get in your server with the command you shared above?

The response of 202 - Accepted right after you send the command is expected, early this year, there was a change applied in this integration to return this response code, which means the request is being processed, that’s why you get the error afterward. Take a look at this post:

@nayelyz let me know if you’d prefer I start a new thread, but I’m getting the “access token provided is malformed or call is made to wrong callback URL” error and can’t figure out what is going wrong when trying to make a stateCallbackRequest.

I have an accessToken that I received via an accessTokenRequest and the associated accessTokenResponse (which provided an accessToken and a refreshToken). The token has not expired, and the stateCallbackRequest is going to the URL provided via the callbackUrls portion of the response (https://c2c-us.smartthings.com/device/events in this case).

My request POST body looks like the following:

{
    "headers": {
        "schema": "st-schema",
        "version": "1.0",
        "interactionType": "stateCallback",
        "requestId": "f2815e72-666a-42c4-8bbf-ea95cb7bbaaa"
    },
    "authentication": {
        "tokenType": "Bearer",
        "token": "eyJhbGciOiJIUzM4NCJ9.ODMyYzdmZTMtNGRlMS00ZGJiLWI0MDMtZjQ3ZGE4YzE5NDdlOlNsZWVwTnVtYmVyIEJl<truncated_for_security>hNtN77EsYVFxKDx0cV0Brc0ilmR"
    },
    "deviceState": [
        {
            "externalDeviceId": "<the value>",
            "deviceCookie": [],
            "states": [
            ...
            ]
        },
        {
            "externalDeviceId": "<another value>",
            "deviceCookie": [],
            "states": [
                ...
            ]
        }
    ]
}

with headers ['Content-Type' => 'application/json', 'charset' => 'utf-8']

Can I get some guidance on how to triage this?

And just like that I figured it out. Turns out that the accessToken values are longer than 255 characters, so I was storing a truncated value and using that. I increased the field length in my database and used the full value and it returned a 202. Apologies.

1 Like