Driver:try_create_device exception

Hi All,

I’m new here and would like some help to get my integration moving.
I built a blinds automation using a NodeMCU board and a NEMA 17 stepper motor, mostly because why not :slight_smile:

That part works fine. Now I am in phase 2 of my project where i want to integrate my board with ST. For that i used the Lightbulb-lan-esp8266 example here: Tutorial | Creating Drivers for LAN Devices with SmartThings Edge

I am taking the sample code from the tutorial and modify it to fit my project. Kinda painful as I am new to ST and documentation for edge drivers doesn’t seem to help my case.

The problem at hand is in create_device function in discovery.lua:
local function create_device(driver, device)
log.info(’===== CREATING DEVICE…’)
log.info(’===== DEVICE DESTINATION ADDRESS: ‘…device.location)
– device metadata table
local metadata = {
type = config.DEVICE_TYPE,
device_network_id = device.location,
label = device.name,
profile = config.DEVICE_PROFILE,
manufacturer = device.mn,
model = device.model,
vendor_provided_label = device.UDN
}
local success, drv = pcall(driver:try_create_device(metadata))
if success then
log.info(’===== SUCCESS CREATING DEVICE: ‘…drv)
return drv
else
log.error(’===== ERROR CREATING DEVICE: '…drv)
return nil
end
– return driver:try_create_device(metadata) – replaced with code above to handle exception
end

The driver installation on the hub worked fine. When i use the ST app on my phone and “scan for nearby devices” here is what the hub log looks like:

PS M:\Projects\Smartthings\SmartThings-Blinds> smartthings edge:drivers:logcat c71db051-5ae0-49dd-8017-de3e029355c3 --hub-address 192.168.1.112
2022-03-28T04:01:21.930529191+00:00 TRACE Blinds8266 Setup driver Blinds8266 with lifecycle handlers:
DeviceLifecycleDispatcher: Blinds8266
default_handlers:
added:
removed:
init:
driverSwitched:
child_dispatchers:

2022-03-28T04:01:21.937877191+00:00 TRACE Blinds8266 Setup driver Blinds8266 with Capability handlers:
CapabilityCommandDispatcher: Blinds8266
default_handlers:
switchLevel:
setLevel
refresh:
refresh
windowShade:
switch:
on
off
windowShadeLevel:
colorControl:
setColor
child_dispatchers:

2022-03-28T04:01:21.953832191+00:00 TRACE Blinds8266 Server.new_with
2022-03-28T04:01:21.959421191+00:00 TRACE Blinds8266 Server:post
2022-03-28T04:01:21.964810858+00:00 TRACE Blinds8266 Server:listen
2022-03-28T04:01:21.999297524+00:00 TRACE Blinds8266 Received event with handler _resync
2022-03-28T04:01:22.004975191+00:00 TRACE Blinds8266 Received event with handler environment_info
2022-03-28T04:01:22.011954858+00:00 TRACE Blinds8266 Received event with handler environment_info
2022-03-28T04:01:22.017430191+00:00 DEBUG Blinds8266 Z-Wave hub node ID environment changed.
2022-03-28T04:01:22.025332524+00:00 TRACE Blinds8266 Received event with handler discovery
2022-03-28T04:01:22.056635858+00:00 INFO Blinds8266 ===== SCANNING NETWORK…
2022-03-28T04:01:24.076328859+00:00 ERROR Blinds8266 ===== DEVICE NOT FOUND IN NETWORK
2022-03-28T04:01:24.101847192+00:00 INFO Blinds8266 ===== SCANNING NETWORK…
2022-03-28T04:01:25.276274526+00:00 INFO Blinds8266 ===== DEVICE FOUND IN NETWORK…
2022-03-28T04:01:25.290782193+00:00 INFO Blinds8266 ===== DEVICE DESCRIPTION AT: http://192.168.1.79:80/Blinds8266.xml
2022-03-28T04:01:25.340767193+00:00 INFO Blinds8266 ===== FETCHING DEVICE METADATA…
2022-03-28T04:01:25.981597193+00:00 INFO Blinds8266 ===== DEVICE LOCATION at: http://192.168.1.79:80
2022-03-28T04:01:26.006243860+00:00 INFO Blinds8266 ===== CREATING DEVICE…
2022-03-28T04:01:26.020344193+00:00 INFO Blinds8266 ===== DEVICE DESTINATION ADDRESS: http://192.168.1.79:80
2022-03-28T04:01:26.053823860+00:00 ERROR Blinds8266 ===== ERROR CREATING DEVICE: attempt to call a number value
2022-03-28T04:01:26.061588860+00:00 DEBUG Blinds8266 discovery device thread event handled
2022-03-28T04:01:51.298623872+00:00 TRACE Blinds8266 Received event with handler discovery
listening for logs… done

Any idea what this exception “attempt to call a number value” is about?

Is there documentation for driver:try_create_device that anyone can point me to ?

Have you tried printing the actual content of the variable metadata to see if the values are set correctly?
You can use stringify_table, for example:

local utils = require "st.utils"

print(utils.stringify_table(metadata))

There’s documentation about try_create_device here but I don’t know if it’s what you’re looking for

Can you see anything like that?

Is the pcall there to diagnose the problem or could it actually be the problem? It isn’t something I’ve ever used but something about it looks wrong to me. To me you are executing pcall with a returned value from a function as the argument which can’t be right.

@nayelyz am I talking rubbish?

pcall is used to handle errors in Lua but we should check everything that could be involved :thinking:, @dannica could test if this is causing the error by removing the function call, it should also catch the errors that could come up in the device creation. For example:

local success, err = driver.try_create_device(...)

I was really querying if pcall(driver:try_create_device(metadata)) was the correct syntax. I imagined it would be more along the lines of pcall(driver:try_create_device, metadata) as pcall would need to call the function itself. I wasn’t totally confident about the first argument though.

@nayelyz

Thanks a lot, actually the documentation helped as well as the idea to print the metadata !
The culprit was in the wrong device profile name.

Funny enough, i am still getting the same error but the device is created. I do have more work to do to clean up and complete the code but this is promising. Thanks for your help!!!

@orangebucket

You are correct in the sense that the documentation shows its use as:
pcall(driver:try_create_device, metadata)

but if i try it like documented i get a … syntax error!!!

2022-04-01T05:01:08.654290442+00:00 FATAL Blinds8266 Lua: callback error
stack traceback:
[C]: in local ‘searcher’
[string “env builder”]:46: in global ‘require’
[string “init.lua”]:5: in main chunk
caused by: syntax error: [string “discovery.lua”]:101: function arguments expected near ‘,’

So i followed the hint and seems to work. Not sure why though…

As things stand driver:try_create_device(metadata) is being called to create the first argument for pcall so by fixing your profile name issue the device is being created fine. It is just that pcall wants a function for the first argument, not the result of calling try_create_device. That’s why it is complaining, it is trying to use a number as a function.

After an extra bit of research, otherwise known as looking up how @erickv did it elsewhere in the forum …

I think the problem is that the : is what they call ‘syntactic sugar’ and you need to use the unsweetened version. @erickv offered this.

local _, err_message = pcall(device.try_create_device, device, metadata)

For reference:

a.b( a, c ) can be expressed as a:b( c ) except where it can’t.

1 Like

… and thinking about it, who needs any more sugar these days? Thank a lot Graham, error is gone now!