[ST Edge] Issue with syntax getting current state

Newb Lua programming question, but I can’t seem to get the current state from my device. I am trying to get the fan speed from my device. Here is my code along with the error and docs:

Code:

local speed = device.get_latest_state('main', capabilities.fanSpeed, 'fanSpeed', 0)

Error:

 [ERROR 05:22:14] [string "st/thread.lua"]:76: Stairwell Fan thread encountered error: [string "st/dispatcher.lua"]:229: Error encountered while processing event for <Device: XXX (My Fan)>:
    arg1: table: 0xf84058
[string "st/device.lua"]:228: component_id should be of type string but was table

Docs:
https://developer-preview.smartthings.com/edge-device-drivers/device.html?highlight=emit#st.Device.get_latest_state

Get the latest state of this device for a given component, capability, attribute

table (e.g. it would include both the value and unit keys if both are present)

Parameters
component_id (str) – the component ID to get the state for

capability_id (str) – the capability ID to get the state for

attribute_name (str) – the capability attribute name to get the state for

default_value (any) – Optional value to return if the state_cache for the lookup is nil

default_state_table (any) – Optional value to return if the state_cache for the lookup is nil

Returns
The first return value is the state.value present for the attribute, the second return is the state

Return type
any or any

I am passing the component_id as a string, so I am a bit perplexed by the error

Change . to :

device:get_latest_state

I also am new to this lua world, and I understood that calling with . is for static methods and calling with : is for non-static methods.
If the method definition has self as the first parameter, then it is non-static.

You could keep using ., but it would require you to pass device as the first parameter.

1 Like

Interesting. Based on other languages, I assumed the opposite with “.” being non-static. Thanks for the tip.

1 Like

The function is also looking for the capability ID as a string. This should work:

local speed = device:get_latest_state('main', capabilities.fanSpeed.ID, 'fanSpeed', 0)
2 Likes

Yep. That was the next error I encountered after fixing the dot syntax