How to retrieve data using attribute_read


#1

Hi,

I tried to read the value of LED(attribute#1024) with the below cloud API.

v1/accounts/{accountId}/devices/{deviceId}/actions

But the response was something as below. no “data” available.

{“type”:“attribute_read”,“requestId”:XX,“timestampMs”:1489571481493,“sender”:“ClientApi”,“source”:{“type”:“user”,“userId”:“XX-XX-XX-XX”,“firstName”:“XX”,“lastName”:“XX”},“attrId”:1024}

Could someone provide me with some sample script?


#2

Hi!

I’m able to retrieve the attribute value through the API /v1/devices/{deviceId}/attributes just fine.

Let me see if I can find a way to get the same values from /v1/accounts/{accountId}/devices/{deviceId}/actions as well, I will follow up here shortly with an answer.

Thanks!

Joe


#3

Hi Joe,

Could you tell me how to use /v1/devices/{deviceId}/attributes?

I don’t mind using this new API as long as I can read / write attributes.

Best,
Wataru


#4

Hi!

This is actually a pretty straightforward API. It’s similar to /v1/accounts/{accountID}/devices?expansions=attributes in the developer doc but a little simpler to use. Why it’s not in the developer doc, I don’t know, I’ll work on having it added.

Basically you can get your list of deviceIDs through /v1/accounts/{accountID}/devices and then perform individual attribute actions on a specific device with:

GET /v1/devices/{deviceID}/attributes to get a list of all the of the device’s attributes and values, or
GET /v1/devices/{deviceID}/attributes/{attributeID} to get the value of a specific attribute (some standard ones are defined at https://developer.afero.io/docs/en/?target=AttrRegistry.html, otherwise for MCU attached devices use the attribute numbers defined in device-description.h in your profile)

You can set a specific attribute with

POST /v1/devices/{deviceID}/attribtues/{attributeID} with the JSON data:

{
“data”: “”,
“value”: “”
}

“data” and “value” are related information - I think you only may need to specify one of them. I thought there was a post about this on the developer forum already but I’m not finding it right now. “data” is a little-endian hex representation of the atttribute contents, “value” is the more human-readable version of the same contents (for example “data=0100” and “value=1” would both set the attribute to “1”, and so on).

It’s difficult to explain but your attribute data should be pretty obvious to you if you read it from this API, and modify or set it accordingly.

If I can find the other forum post on the discussion about it, I’ll link it here.

Cheers,

Joe


#5

Hi Joe,

I tried the following API.
GET https://api.afero.io/v1/devices/{deviceID}/attributes/{attributeID}

And the server returned the following response. The entire message was over 10,000 characters long so I’ll post just the beginning of it.
{“timestamp”:1489938189921,“status”:500,“error”:“Internal Server Error”,“exception”:“com.kibanlabs.shared.exception.InternalErrorException”,“message”:“Device is not virtual”, …

Best,
Wataru


#7

Hi Wataru,

My apologies! I gave you the wrong API to use. I’m sorry! :slight_smile: That one worked for me but it apparently is an old one.

The proper API is close to the one you were using before, it’s:

/v1/accounts/{accountId}/devices/{deviceId}?expansions=attributes

(instead of “/actions” use “?expansions=attributes”)

This one is also documented in the developer documentation at https://developer.afero.io/docs/en/?target=APIEndpoints.html#CurrentDevState as well.

That one should work better for you. :smile:

Cheers,

Joe


#8

Hi Joe,

Thanks. The below command worked well.
/v1/accounts/{accountId}/devices/{deviceId}?expansions=attributes

Best,
Wataru