Help with virtual switch that used to be controlled via webCore

I’m not as smart as most on this board. I generally hobble solutions from other’s great work. The loss of webCore in ST broke something critical for me that I need some help fixing. I used the http commands associated with webCore pistons to turn a virtual device (My hot tub motor status) on/off. I have a server script that queries my spa every 5 minutes and POSTS the motor status to a webCore piston. This gave me access in ST to know if my spa was on or off within 5 minutes of accuracy and I have automations that need that status to work.

Now what? I started reading about edge drivers, but didn’t see how to update a virtual switch status remotely. Sounds like I may not need to go through the cloud anymore? That would be nice, but really need some guidance of anyone is interested…

Any help appreciated.

Thinking that this driver from @TAustin will help you.

1 Like

SmartThings has a REST API so you can probably update your virtual device status directly from your script. It would require a POST with an Authorization header containing a Personal Access Token.

1 Like

@orangebucket , looks promising…

I could definitely use some help setting up a new virtual switch to be changed with my server and the actual POST or GET commands to issue from my server to change the virtual switch…anyone understand the Rest API and a virtual switch that could hold my hand through setting this up?

I think webrequestor is backwards for what I need. I need to use a POST or GET to change the status of a virtual switch…

So from your server, you want to change the state of the virtual switch? If so, then you will probably want to use the REST API as mentioned in an earlier response.

This might be of interest.

In the first post the -d '{"command":"on"}' in the GET doesn’t need to be there.

Thanks for the replies.

Two follow up questions. I already have a virtual switch, but not sure if it’s groovy based. Will it still be viable and how do I check?

Second, the REST API looks to be calling, yet I thought this new foundation was supposed to be local?

1 Like

You can check what type of driver your device is using using the ST CLI or the API Browser+ from @TAustin. A device with show type DTH if it’s a Groovy based driver. Edge virtual devices should show type LAN.

While drivers run locally on your hub and routines that utilize local devices also do, programming your hubs behavior is done via the ST API which is hosted in the cloud and communicates to your hub over the Internet. So, if you lose your Internet connection, local automations can run autonomously, however, you won’t be able to use the APIs or the ST App to make changes to your environment until Internet is restored.

Found these replies very helpful!
I was able to get my PAT and get a list of devices. From there, I isolated the virtual switch I want to control and was able to check it’s status and see it go from ON to OFF after changing it manually in the Smartthings app. That’s progress!!

But, when I tried to turn it OFF (or ON, really just trying to change it). It errored. Here’s the command I issued and the response/error I got:

curl -X POST -H “Authorization: Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx” -H “Content-Type: application/json” -d ‘{“commands”:[{“component”:“main”,“capability”:“switch”,“command”:“on”}]}’ “

{“requestId”:“xxxxxxxx-arequestid”,“error”:{“code”:“ConstraintViolationError”,“message”:“The request is malformed.”,“details”:[{“code”:“BodyMalformedError”,“target”:“httpRequestBody”,“message”:“The request body is malformed and cannot be processed by server.”,“details”:}]}}

Here’s the switch’s details listed when I listed all my devices:

{“deviceId”:“xxxxx”,“name”:“spa_pump”,“label”:“spa_pump”,“manufacturerName”:“SmartThings”,“presentationId”:“generic-switch”,“locationId”:“xxxxxx”,“roomId”:“xxxxx”,“deviceTypeId”:“e233618f-da91-4e0f-8993-ae1c99f2aca2”,“deviceTypeName”:“Virtual Switch”,“deviceNetworkType”:“UNKNOWN”,“components”:[{“id”:“main”,“capabilities”:[{“id”:“switch”,“version”:1},{“id”:“sensor”,“version”:1},{“id”:“actuator”,“version”:1}],“categories”:[{“name”:“Switch”,“categoryType”:“manufacturer”}]}],“createTime”:“2021-02-22T03:32:34.280Z”,“parentDeviceId”:“xxxxx”,“dth”:{“completedSetup”:false,“deviceNetworkType”:“UNKNOWN”,“deviceTypeId”:“e233618f-da91-4e0f-8993-ae1c99f2aca2”,“deviceTypeName”:“Virtual Switch”,“executingLocally”:true,“hubId”:“xxxxx”,“networkSecurityLevel”:“UNKNOWN”},“type”:“DTH”,“restrictionTier”:0,“allowed”:}

I admit I’m WAAAY out of my element here, but I think I’ve got syntax wrong or something

The other option (and local execution) is using @TAustin’s edgebridge and lan motion device. It appears to have a switch capability that I think sycs with the motion/inactive status. You could use that by faking (e.g. sending the http request) the Shelly/blue iris formatted motion/inactive requests as a version of on/off.

The driver is in his test channel (I hope it’s okay to share) found here: Channel and Drivers Web UI

I can t find the thread where we discussed the expansion of the lan motion driver, but for your purposes, it and the http motion driver would work very similar.

I bet Todd could better articulate the solution but I think it would work.

Edit: clarity

When I copied and pasted your command line I found I had to correct the single quotes as they were fancy curly ones. The double quotes were OK. I started up a bash shell on my Windows laptop so the single quotes were valid and put in my own UUIDs for the token and the deviceId. It worked first time.

$ curl -X POST -H "Authorization: Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -H "Content-Type: application/json" -d '{"commands":[{"component":"main","capability":"switch","command":"on"}]}' ""

So you couldn’t have been far off. If you do want to use a Windows CMD shell you can’t use the single quotes. So it becomes:

> curl -X POST -H "Authorization: Bearer xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -H "Content-Type: application/json" -d "{\"commands\":[{\"component\":\"main\",\"capability\":\"switch\",\"command\":\"on\"}]}" ""

I am using Windows CMD shell, so I’ll try again by fixing single quotes using double…I’ll report back. Thanks for the tip!

Fantastic. Thanks for the Windows CMD tip with double quotes and backslashes. I needed that to confirm it works before I went after it with Python (which ironically didn’t have that problem). I worked through the Python POST specifics (needed to use JSON vs DATA). Thank you @orangebucket !!!

NOW. We’ve solved the critical part of my integration, but I’m curious about some “nice to haves”…how difficult would it be to create a NEW virtual device for my spa and figure out the API syntax to feed it? In WebCORE, I used the URL to pass variables as follows:


So, I was updating:
InvalidT (boolen for if it’s been too long for temp to still be valid)
Set Point Temperature
Display Temperature
Heater (ON/OFF)
Timestamp (last data pull)

Admittedly, the use of this info was far easier in webCore, but I’m thinking that a virtual thermostat might be a good approach. Now that I’ve got the python to API working, I have these parameters ready to pass…Anybody interested in trying to help me get the “extras”???

Thoughts, advice?