How to Update Virtual Sensor Values with API Request?

Hello, I’m new to smartthings.

I have a sensor that doesn’t work with smartthings, but I have an API to retrieve data.
The sensor retrieves humidity, temperature and CO2.

I made a virtual device to represent the sensor and I would like to update this virtual device with real data. Is there a way to update this device with the API request? What would be the steps to make this work?

Thanks !

Tagging @orangebucket

The answer is almost certainly yes, but the steps rather depend on the nature of the ‘virtual device’ and how you want to do it.

For example you could create a virtual device using the SmartThings CLI tool to create a device profile (smartthings deviceprofiles:create) with the capabilities relativeHumidityMeasurement, temperatureMeasurement and carbonDioxideMeasurement and then using that profile in smartthings virtualdevices:create to create a device. Like this one …

smartthings devices:status 3c374979-f3bf-4250-b177-c514161efae7
─────────────────────────────────────────────────────────────────────────────────
 Capability                   Attribute         Value
 relativeHumidityMeasurement  humidity          4.360262491785161 %
 temperatureMeasurement       temperatureRange  {
                                                  "maximum": 2028.9512493247526,
                                                  "step": 4268.191013300918,
                                                  "minimum": 6230.791732339365
                                                } F
 temperatureMeasurement       temperature       4172.907975609753 F
 carbonDioxideMeasurement     carbonDioxide     146828.37852645424 ppm
─────────────────────────────────────────────────────────────────────────────────

Now all of those capabilities are pure sensors, so they don’t have any commands that can be used to set the attributes. So instead we have to create the events for them.

> smartthings virtualdevices:events 3c374979-f3bf-4250-b177-c514161efae7
────────────────────────────────
 #  Id
────────────────────────────────
 1  carbonDioxideMeasurement
 2  relativeHumidityMeasurement
 3  temperatureMeasurement
────────────────────────────────
? Select a capability. 1
? Enter 'carbonDioxide' attribute value: 1000
──────────────────────────────────────────────────────────────────────────
 Component  Capability                Attribute      Value  State Change?
 main       carbonDioxideMeasurement  carbonDioxide  1000   undefined
──────────────────────────────────────────────────────────────────────────

> smartthings virtualdevices:events 3c374979-f3bf-4250-b177-c514161efae7
────────────────────────────────
 #  Id
────────────────────────────────
 1  carbonDioxideMeasurement
 2  relativeHumidityMeasurement
 3  temperatureMeasurement
────────────────────────────────
? Select a capability. 2
? Enter 'humidity' attribute value: 76
─────────────────────────────────────────────────────────────────────────
 Component  Capability                   Attribute  Value  State Change?
 main       relativeHumidityMeasurement  humidity   76     undefined
─────────────────────────────────────────────────────────────────────────

> smartthings virtualdevices:events 3c374979-f3bf-4250-b177-c514161efae7
────────────────────────────────
 #  Id
────────────────────────────────
 1  carbonDioxideMeasurement
 2  relativeHumidityMeasurement
 3  temperatureMeasurement
────────────────────────────────
? Select a capability. 3
─────────────────────
 #  Attribute Name
─────────────────────
 1  temperature
 2  temperatureRange
─────────────────────
? Select an attribute. 1
? Enter 'temperature' attribute value: 40
─────────
 #  Unit
─────────
 1  C
 2  F
─────────
? Select a unit. 1
──────────────────────────────────────────────────────────────────────
 Component  Capability              Attribute    Value  State Change?
 main       temperatureMeasurement  temperature  40     undefined
──────────────────────────────────────────────────────────────────────

> smartthings devices:status 3c374979-f3bf-4250-b177-c514161efae7
─────────────────────────────────────────────────────────────────────────────────
 Capability                   Attribute         Value
 relativeHumidityMeasurement  humidity          76 %
 temperatureMeasurement       temperatureRange  {
                                                  "maximum": 2028.9512493247526,
                                                  "step": 4268.191013300918,
                                                  "minimum": 6230.791732339365
                                                } F
 temperatureMeasurement       temperature       40 C
 carbonDioxideMeasurement     carbonDioxide     1000 ppm
─────────────────────────────────────────────────────────────────────────────────

I suppose I ought to do this with the temperature range as well. Just to make it more interesting, let’s turn on the debugging and do the whole lot on one line (this is on Windows).

> set SMARTTHINGS_DEBUG=true
> smartthings virtualdevices:events 3c374979-f3bf-4250-b177-c514161efae7 temperatureMeasurement:temperatureRange "{ ""maximum"":100, ""step"":5, ""minimum"": 0 }"
[2024-03-16T17:08:09.986] [DEBUG] login-authenticator - authentication - enter
[2024-03-16T17:08:09.991] [DEBUG] rest-client - making axios request: {"url":"https://api.smartthings.com/capabilities/temperatureMeasurement/1","method":"get","headers":{"Content-Type":"application/json;charset=utf-8","Accept":"application/json","User-Agent":"@smartthings/cli/1.8.1 win32-x64 node-v18.5.0","Accept-Language":"en-GB","Authorization":"Bearer aac1f59e-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}}
[2024-03-16T17:08:10.275] [DEBUG] login-authenticator - authentication - enter
[2024-03-16T17:08:10.276] [DEBUG] rest-client - making axios request: {"url":"https://api.smartthings.com/virtualdevices/3c374979-f3bf-4250-b177-c514161efae7/events","method":"post","headers":{"Content-Type":"application/json;charset=utf-8","Accept":"application/json","User-Agent":"@smartthings/cli/1.8.1 win32-x64 node-v18.5.0","Accept-Language":"en-GB","Authorization":"Bearer aac1f59e-xxxx-xxxx-xxxx-xxxxxxxxxxxx"},"data":{"deviceEvents":[{"component":"main","capability":"temperatureMeasurement","attribute":"temperatureRange","value":{"maximum":100,"step":5,"minimum":0}}]}}
──────────────────────────────────────────────────────────────────────────────────────────────────────────
 Component  Capability              Attribute         Value                                 State Change?
 main       temperatureMeasurement  temperatureRange  {"maximum":100,"step":5,"minimum":0}  undefined
──────────────────────────────────────────────────────────────────────────────────────────────────────────

There you can see the details of the API request that is being made behind the scenes so you can do it any way you like.

So I think that is probably the ‘yes’ dealt with, though not necessarily the steps you need.

3 Likes

Thank you very much for your answer !
I’ll try this tomorrow!

2 Likes

Hello this is working very well!
I made this profile:

{
    "name": "weather_station_main",
    "components": [
        {
            "id": "main",
            "capabilities": [
                {
                    "id": "temperatureMeasurement"
                },
                {
                    "id": "relativeHumidityMeasurement"
                },
                {
                    "id": "carbonDioxideMeasurement"
                },
                {
                    "id": "carbonDioxideHealthConcern"
                },
                {
                    "id": "soundPressureLevel"
                }
            ],
            "categories": [
                {
                    "name": "TempSensor",
                    "categoryType": "manufacturer"
                }
            ]
        }
    ]
}

Every 5 minutes I update the virtual device!

Thank you and have a nice day!

3 Likes

The cloud virtual devices can be a little too restricted to use as actions in Routines. They respect defined ‘enum’ and ‘setter’ commands which have implied effects on attributes and they allow other commands to be mapped to attributes within the same capability, but you can’t for example have the on command from the switch capability cause the motionSensor to go active, which is the sort of thing many users are after. There is also a bit of a resistance to anything that can’t run locally.

So it is a joy to see a case where everything just ties together nicely.

3 Likes