How to create a virtual device in a "new" way?

There is quite a mess with the documentation. Most of it talking about how to do something an “old” way. There is documentation talking about “new” way. Just a few weeks ago that “new” way documentation was relocated and it looks like certain things were removed. I’d like to ask about creating of a virtual device that recently was described on this page SmartThings Developers | Documentation (bingj.com). It is from the search engine cache. Is it still possible in the Developers Workspace? I cannot find many things described in that cached article. Where is a new page talking about creating virtual devices?

The old page is still indexed in a search engine but I cannot file a new one.

I need to create a completely custom virtual device. Not a virtual switch. I need it for better structuring of automation. Maybe I’m wrong in my thinking, let me know if there is a better way to do that, again in a “new” way using Rules API etc. I wanted to have a few sensors, then I wanted to aggregate an information from the few sensors in a virtual device using a rule. Then I wanted to have other rules using that virtual device attribute and doing something to other devices.

I can give you a simplified example. Let’s say I have a light sensor or a few of them. I decided to create an algorithm to translate the numeric values from those sensors into a descriptive value like: dusk, earlyNight, night, dawn, earlyMorning, morning, brightDay and so on. Then I will use that value in other rules in other complex expression. So, I was thinking why don’t I create a virtual device with an enum attribute for than and corresponding commands with all those values. Then I create a rule and put an algorithm there to set those properties on that virtual device. Then I would use that virtual device values in other rules.

Why I want this virtual device? Because I want many rules to use the attribute from that virtual device and I don’t want to repeat the algorithm that I have in the rule reading light sensors in all the rules.

1 Like

Hi, @alex.49.98. Your use case sounds awesome!
What type of devices are the physical ones?

You could use the following:

  • Edge Drivers to create the virtual device. This “hello world” sample is an easy way to get started. (for this option, you require a SmartThings Hub)
  • ST Schema - This is a Cloud-To-Cloud integration that uses OAuth
  • SmartApp Connector - Another C2C integration, this doesn’t use OAuth

It looks like you are looking at this problem from a different perspective. You look at it putting device code at the first place. I’d like to avoid writing device code at all if that is possible. So, let’s consider my example again and let’s simplify it it even more. Let’s say I have one light sensor, thus I should be able to write a rule similar to this

{
    "name": "Rule to set the property on my custom virtual device (simplified)",
    "actions": [{
            "if": {
                "lessThanOrEquals": {
                    "left": {
                        "device": {
                            "devices": ["04625c32-dcf2-40d7-a3f6-bd5350d7308b"],
                            "component": "main",
                            "capability": "illuminanceMeasurement",
                            "attribute": "illuminance"
                        }
                    },
                    "right": {
                        "integer": 5
                    }
                },
                "then": [{
                        "command": {
                            "devices": [
                                "my-custom-virtual-device-id"
                            ],
                            "commands": [{
                                    "component": "main",
                                    "capability": "dayPart",
                                    "command": "dusk",
                                    "arguments": []
                                }
                            ]
                        }
                    }
                ],
            }
        }
    ]
}

To create my custom virtual device I was thinking about creating custom capability first like this

{
   "name": "Day Part",
   "attributes": {
      "dayPart": {
         "schema": {
            "type": "object",
            "properties": {
               "value": {
                  "type": "string",
                  "enum": [
                     "evening",
                     "night",
                     "morning",
                     "day"
                  ]
               }
            },
            "additionalProperties": false,
            "required": [
               "value"
            ]
         },
         "enumCommands": [
            {
               "command": "evening",
               "value": "evening"
            },
            {
               "command": "evening",
               "value": "night"
            },
            {
               "command": "morning",
               "value": "morning"
            },
            {
               "command": "day",
               "value": "day"
            }
         ]
      }
   },
   "commands": {
      "evening": {
         "arguments": []
      },
      "night": {
         "arguments": []
      },
      "morning": {
         "arguments": []
      },
      "day": {
         "arguments": []
      }
   }
}

Then I would go to the Developer Workspace and create a virtual device with that custom capability. After that I suppose I should be able to use the virtual device attribute to create other rules like

{
   "name": "A rule triggered by the virtual device attribute change which is done by another rule (simplified)",
   "actions": [{
      "if": {
         "equals": {
            "left": {
               "device": {
                  "devices": ["my-custom-virtual-device-id"],
                  "component": "main",
                  "capability": "dayPartCapability",
                  "attribute": "dayPart"
              }
            },
            "right": {
               "string": "dusk"
            }
         },
         "then": [{
		     ........
         }]
      }
   }]
}

Thus, to my understanding I don’t need to write any device drivers, connectors, cloud stuff, any code besides configuring a virtual device with a custom capability and writing a bunch of rules. I don’t understand how to bring ends together though. There are hints for that in the documentation that is completely gone from the servers.

Getting back to the original question. Can I create such virtual device? Frankly speaking I already created that custom capability. It was accepted by the system. I tried to create a device in the Developer Workspace using that custom capability. I actually created a few of them but they seems to be development versions of a real device that I’m supposed to add to the system later but I want this to be a virtual device with one attribute of that custom capability. I suppose it should be easier that writing Edge Drivers or cloud connectors but there are things that I’m definitely missing to bring all those separate parts together.

The guide you shared before is not up-to-date, the option to add virtual devices from the Developer Workspace is no longer available.

In the Dev Workspace, you have to create the ST Schema and SmartApp projects which:

  • will have a device profile that you’ll use in the integration. This defines the device metadata (capabilities, presentation, etc.)
  • is where you register them to install them from the ST app later. The information here includes:
    • the URL where those projects are hosted
    • OAuth credentials (if needed)

There are some users that mention they use the “Labs” tool in the app to create some simple devices but I believe it is not available for some regions.

As far as I know, The only device the virtual switch creator in SmartThings Lab can create is a simple on/off Binary switch.

It’s possible that @alex.49.98 could use that for some use cases, turning it on when all the sensors hit a specific range, but you end up needing a lot of them to cover most similar situations. So you need one switch for dusk, one for earlyNight, one for night, one for dawn, one for earlyMorning, one for morning, one for brightDay, etc.

And consequently a different set of automations for each of those switches.

It’s not impossible, but it’s really tedious to set up and maintain as opposed to a single virtual sensor.

Way back in 2015 or so when they weren’t as many choices, there was a virtual Dimmer, and then people would use that to represent multiple values from sensors, etc. because the dimmer could be set to a value either from 0 to 100 or from 0 to 255 depending on the details. That required more of a programming mindset, but it was less tedious to set up. But I don’t know whether the labs option offers a Dimmer possibility or not.

It seems then that the ability to create virtual devices in the Developer Workspace was irreversibly removed. I don’t think that is a good decision. Now we lost the only ability to create state for the rules.

Yes, we still can create cloud base devices, smartapps but the complexity level is not the same. As soon as you start getting into that you’ll need to either setup AWS or your own server. Not too difficult but still more than I’d like to have for a task of maintaining a state among rules.

1 Like