I want to write a device driver for a virtual switch that can turn one device on and off but the state is determined by a different sensor. Is this possible?
Can you explain what you want in more depth? This sentence confuses me:
The only device a virtual switch can turn on/off is itself. You can have an automation that uses as trigger the state of the virtual switch to control a different (physical) device.
Similarly, if you are referring to the virtual switch's state, you can have an automation that uses as trigger the state of the sensor to control the virtual switch.
So at the end of the day, I want to build a switch for my garage door that accurately reflects the position. I know there are solutions out there already but I wanted to see if it was possible to do this myself and I was thinking I need a virtual switch to do this so I can also have it accurately reflected in Google Home. I have a zwave dry contact relay and a tilt sensor, so my theory was use a virtual switch to trigger the momentary contact on the relay, and have the state of that switch be displayed by the state of the tilt sensor.
Am I thinking of how a virtual switch works completely wrong? Maybe I'm not able to have a switch reflect the state of another device it's not controlling?
Edit: maybe I'm confusing the function of a virtual switch with that of an app? Seemed easier to me to just tie a virtual switch to 2 hardware devices rather than write an entire app
Drivers can't "talk" to each other directly unless they have a parent/child (or sibling via the parent) relationship, so you'd either need that or an app.
As you've noted, there are existing solutions out there. Here is one:
Even if you don't use that, perhaps it would give you a starting point.
Or if you've already got everything else figured out, maybe just add flipping the state of both switches to your existing app (or rule)?
I think this clears up my confusion on how hubitat drivers/apps work. Looking at the code you sent, that app creates a device that does exactly what I was thinking. I wasn't aware an app can create a device you can use. SO I need to write apps not device drivers, thanks for the help!