How to get deviceId from a device specified in "Device: State"

I’m building a simple workflow to set the connection status of a specified device in Losant. I use the “Device: State” trigger to select the device, and then I set its connection status using the “Device: Set Connection Status” node.

While I’ve been able to run this workflow successfully by manually entering the device ID, I want to use a variable such as {{data.deviceId}}, derived from the device selected in the “Device: State” trigger. Is there a way to do this?

Hi @David_Santoso,

Yes on the Device: State Trigger the device that sent the state is attached to the payload under the “device” property name. To access this devices ID the payload path would be device.id and to use that in handlebars it would be {{ device.id }}.

You can view most of the device properties on the example payload on the device state trigger.

I hope this helps and let us know if you have any more questions.

Best,
Erin

Hi @Erin_Thompson, I’ve tried {{ device.id }} or {{device.deviceId}} but still getting “Validation Error - deviceId required”.

Best,
David

Hi @David_Santoso,

I went and peaked at your workflows to see what is going on. You are using a virtual button to trigger a workflow that is then using the Device: State Node and then trying to conditionally set the connection status for that device. In this workflow you are not using the Device: State Trigger at all. Let me know if I am looking at the wrong workflow but I am looking at the one that has been updated today and is named “Set Device Connection Status”.

My suggestion to you would be to have one flow that is a Virtual Button to the Device State Node. And then have another flow that is the Device: State Trigger and then you can update your conditional node to check the device state for the error property and then set the connection status to either connected or disconnected. When you have a workflow that starts with the Device: State Trigger you will have access to the device property.

If you want to keep this as one workflow, you will have to use the Device: Get node to find the device and add it to your payload.

I hope this helps.

Best,
Erin

Hi @Erin_Thompson,

Yes, it’s the correct workflow and sorry, I’m using Device: State Node and not Device: State Trigger.

Since I’ve already specified the device in Device: State Node, isn’t there a way to get the deviceId of that device?

Best,
David

Hi @David_Santoso,

Unfortunately no the Device: State Node does not add a device to the payload to pass into other nodes. Here are examples of what the device state node can add to the payload. When using a virtual button as a workflow trigger you will have to use the Device: Get node to add the device directly to the payload. If you do this then you could use the device ID from the payload on the Device State node and the set connection nodes. You can also add device ID directly on to the data for the virtual button.

Best,
Erin

Thank you @Erin_Thompson, I was able to do it using the Device: Get node as per your recommendation.

I have another question though. I saw that the Device: Get node generate both device.id and device.deviceId payload. I tried both and they work, but why did it generate both?

Best,
David

@David_Santoso,

That’s to match our API schemas. Some of our API resources can return several different IDs on the same resource. It is nice to have an property with the resource type included so there is no question as to which resource an ID is referencing.

Most of our users end up using deviceId when using handlebars helpers because it includes the type of resource.

Thanks,
Erin