We’re working on a project where we want to be able to set some “derived attributes” on a Losant device. For example, our real device can send a “successful event” attribute to Losant, and we’d like the device to be associated with a count of the number of “successful event” messages it’s sent. However, we don’t really need the device itself to remember or track how many of the messages it sends, so it makes sense to compute that value on the server side instead of the client side.
One option for creating some computed or derived attributes would be to assign them to a virtual device corresponding to the real device (which we’re also playing with). Workflows already have support for sending output to a virtual device. However, we thought it would be cool to be able to create derived attributes on the main Losant device, using a workflow. Then we could show those properties in the device’s dashboard just by looking through its list of attributes, instead of associating it with a corresponding virtual device. We think of these as “virtual attributes.” If only there were a “Device” output node in the workflow editor… It turns out we can make one with a Lambda function!
I’m posting to Show Losant in case anyone else wants to do something similar. It might slightly break the clean separation between Device and Virtual Device in Losant, but perhaps it will be useful. We created an AWS Lambda function which acts essentially as a “Device” output node in the workflow. You send it the ID of the device you’d like to update, and you also send the attributes you’d like to set. It logs in to Losant as that device and sends the attributes.
The update appears to come from the device itself, so it will trigger Device triggers in the workflows, just like a normal attribute report. Be careful not to create an infinite loop of reports If you end up using this in workflows with a Device trigger, you’ll likely want to have a conditional node after the Device trigger to check whether the updated properties are “real” or the computed “virtual” ones coming from Lambda.
Here’s the repo: https://github.com/ottobonn/losant-lambda-device