Sometimes a client is offline and when it becomes online again it should be up to date instantly. The key question is "what was the last value of...?" The solution is provided by the Last Event Cache in Watson IoT Platform. It does exactly what the name says: provide the last event that a device has sent - regardless if the device is currently online or offline. The event must not be older than 365 days. Last Event Cache ist simple to use as I will show you in this article.
Last Event Cache can be called using the REST API with the following pattern:
If you are interested in all events that the device has sent regardless of the event ID, then simply use:
To ensure that data is only accessible by those who are authorized you have to authenticate using basic auth. You need to create an API key in Watson IoT Platform. The key is used as username and the authentication token is the password.
Example needed? I want to get the last values for my thermostat and use the following call:
And I get this JSON strcuture as result:
Please note: this is an array containing an object. Would my device send different events with different event-IDs, the array would contain one object with the latest value for each event-ID. Probably the most used fields are the last two:
timestamp shows when Watson IoT Platform has received the message from the device,
payload conatins the device data in a base64 encoded string. This can be converted back to the original device data with a simple Node-RED flow:
Starting point is the HTTP Request as depicted above (e.g. triggered by an inject-node for testing). Next we extract the object from the array:
msg.payload = msg.payload.payload;
Then we decode the string using the base64 node and convert it into an utf8 string:
msg.payload = msg.payload.toString('utf8');
Using the JSON node we construct a JSON-object out of the string. This makes it easier to work with the data in the following course of the flow. Using the above example I receive the following msg.payload:
Don't want to miss anything?
Newsletter Subscribe Content