SmartThings API Doesn't Work Unless Application is on Same Network as Hub (but Application Works Fine)

I am using the api in order to tell if the sensor is triggered or not and i have a script that uses that information. The smartthings mobile app works fine when the mobile is away(in another city away) or close to the hub(in the same house as the hub). The problem is that the api doesnt work consistenly like the mobile app. The api works fine only when the mobile is in the same house as the hub but when the mobile is away the api no longer works only the mobile app. Is the API designed to work like that?

Hi @carl_lab Welcome to SmartThings Community

From the description, it seems that your device is hub-connected (zigbee/zwave), if this is the case the location where the mobile is should not be affected.

What exactly do you mean by ‘the API doesn’t work’? Is it because it’s not reflecting the current state of the device?

Also, Could you explain how your script works so I can understand more about your context?

Hello Alejando,
Than you for your prompt response.

I am using this function to read the output of the sensor from api. I generated a token from Samsung account and i used that token to get the information. The scripts works fine when the mobile device is on the same network as the hub but when i took the phone away and tried to read what the script is giving me it wasnt the same as what the app was saying. so when the phone shows that the motion sensor detected motion while the output form the api shows that it detected nothing

async def print_devices(self):
# link to get tokens: https://developer.smartthings.com/docs/advanced/authorization-and-permissions#personal-access-tokens
token = os.getenv(“SMARTTHINGS_TOKEN”)
# device_names = [‘multi-sensor’]
async with aiohttp.ClientSession() as session:
api = pysmartthings.SmartThings(session, token)
devices = await api.devices()

        while True:
            start = float(time.time_ns() // 1_000_000_000)
            for device in devices:
                # door sensor
                if device.label == 'dining_motion_sensor':  
                    await device.status.refresh()
                     self.dining_motion_sensor_active = device.status.values['motion'] == 'inactive'
                

            end = float(time.time_ns() // 1_000_000_000)
            await asyncio.sleep(self.update_period - (end - start))
            self.updated = True

If I understand correctly, your script checks if the capability motionSensor has the value ‘inactive’. However, if you’re only checking the status of one device, I would recommend checking the state of the capability directly with the deviceId, another option if you want to monitor when the motion sensor detects something, is to check the Subscriptions.

It seems that there may be a delay, which could be the reason for this behavior.

I am actually check multiple devices but i included one so the script isnt long. What is the difference between using the api and the subscriptions? What would be the cause kf the delay in that case is the api connected to the mobile?

Okay okay I understand, basically the subscriptions as the name suggests, are pending on the state or your device ( or other options available) when the conditions of your subscriptions the conditions are met SmartThings will send you a request with the information, so you must be a SmartApp, so in this case you don’t need request the status, SmartThings will send you it.

It can be generated for different reasons, your device is connected to a hub and the information should follow this flow

device → hub → ST Cloud

Does this only happen with sensors? I’m curious to know if other capabilities have the same behavior :thinking: