Script help - all devices with battery attribute

I would like An if statement that grabs the attributes locally for all my devices and if battery exists in an attribute it adds it to a devices variable array. I will then be able to use the variable to look for batteries that have 20% power or less.

Any help would be appreciated.

This has been solved thank you @JDRoberts

I had to use an API Call and then use replace.

image

Jared

Hello @jared.rackliff,

There are different ways to achieve what you want.

  1. Make requests to the Devices Endpoint of the SmartThings API
    You could make a request to the URL https://api.smartthings.com/v1/devices?capability=battery using your Personal Access Token to get the list of all the devices that have the capability “Battery”.
    Once you have all the device IDs, make a request to https://api.smartthings.com/v1/devices/device-id/status replacing the device ID for each one of them, and get the current value of the capability.

  2. Develop a SmartApp
    Using a SmartApp, you could subscribe to the events of the “battery” capability for each device or the capability in general, and every time that an event of this capability is generated, you can compare if the value is 20% or lower and know which device has that level.

The difference between them is that with option 1, you can get all the devices installed in your SmartThings account, and with option 2, you can only get the devices in the location where the SmartApp is installed.

Let me know if you have any doubts or if I can help you with something else.

Not exactly what you asked for, but might suffice, I created an automation that checks the battery level of 5 SmartThings buttons and notifies me if any fall below 35%

It seems to me that this one is the way to go as it may be the easiest to implement as well be the most Dynamic to setup.

You could make a request to the URL [https://api.smartthings.com/v1/devices?capability=battery](https://api.smartthings.com/v1/devices?capability=battery) using your Personal Access Token to get the list of all the devices that have the capability “Battery”.

I will have to investigate how to setup a Personal Access token. If I had a personal access token already what would this URL look like?

Thank you.

1 Like

Hi @jared.rackliff,

You can create your Personal Access Token here, you can select only the scopes to interact with the endpoint you want, for example, “Devices”, or all to use it in requests to the other ones as well. Once you create it, remember to save it in a secure place because it will not be shown again.
The token is used in the “authorization” header of the request. In Postman you would place it in the Authorization tab as in the picture below.

There you can generate code snippets to make this request in different programming languages.

Good morning,

I was able to pull data using the API. I love it, but I do have one or two questions. I get this error when I run it.Kitchen Sensor not found. I know it has to do with the around kitchen sensor.

I fixed it…

I had to use replace. Thank you so much for your help nayelyz

image

1 Like

Thanks ronczap for your assist on this. I like the button idea and will use it in another area.

Jared