Set Status on Virtual Switch

I created a SmartApp with the intention of attaching it to 3 things:

  1. Virtual switch - Control and binary status
  2. Meter - Calculate status from above if > abc W
  3. Command device - happens to be an IR blaster in my case.

DTH for the Virtual Switch that I am using is “Simulated Switch”

I want to be able to actuate the virtual switch to turn on/off a “dumb” space heater. The space heater has an IR sensor and I’m controlling it through an IR blaster device. The meter is to tell the virtual switch if it is on or off accurately. Relevant code I’m trying to use is below (vSwitch is the virtual switch):

def powerInputHandler(evt) {
	def latestPower = smartSwitch.currentValue("power")
    if (latestPower > minimumWattage && vSwitch.currentValue('switch').contains('off')) {
    	log.trace "LatestPower: ${latestPower}W > MinimumWattage: ${minimumWattage}W" 	
        vSwitch.onPhysical()
    }
    else if (latestPower <= minimumWattage && vSwitch.currentValue('switch').contains('on')) {
    	log.trace "LatestPower: ${latestPower}W < MinimumWattage: ${minimumWattage}W"
    	vSwitch.offPhysical()
    }
}

Basically, I am having difficulty being able to calculate status and set it without actually sending a command. Switch.on() or Switch.off() appears to send another command instead of just setting status. Switch.onPhysical() and Switch.offPhysical() appear to operate in the same manner. So if I physically push the button on the Virtual Switch to turn it on, go to calculate status and send a Switch.on() command, it sends the command a second time. Basically creating an infinite loop of on/off/on/off or until the timing works just perfectly and it doesn’t send another command.

This is an issue because my command to the IR blaster is a toggle. If I send an even number of the same command, it will be the opposite of what I want, etc. So I want to be able to set the status of the virtual device and NOT send a command.

Any thoughts as to what basic syntax is alluding me? Are virtual switches “one way” only? If I just ignore this and try not to set status, things will generally be correct when turned on/off from SmartThings, but if anyone turns it on locally, status will be wrong.

I can’t help with the code, however to solve your problem with a manual change of the virtual switch you can edit the DTH to not show in ‘Things’

although you can’t ‘see’ them they are available to apps

Yeah, it’s actually if they go and push the button on the physical device. I’d like ST to see this change on the virtual switch.

It sounds like you are confusing Commands with Device Events

On(), Off(), etc. are Commands. You can add extra benign or powerful Commands to make the Device do stuff with fewer (or more!) specific side effects.

But you can only update an Attribute (and Tile States) via sendEvent. That is often called by a Command or by Parse when receiving a physical message.

By tweaking the Events, you probably can solve the problem.

I’m trying to think of a real-example with some code… I have one in mind, but have to dig it up.

In the meantime, perhaps you can give a step by step example scenario? Just one specific behavior? Then I can address how I think a certain combination of Commands and Events would implement it…

Yeah, that makes sense. It’s definitely a convoluted device setup with many devices and this SmartApp (at the least) to accomplish what should be much simpler.

Use Case

  • The “dumb” device is a space heater with an IR sensor and remote. Turning it on/off is done by way of a Toggle button.
  • The space heater is plugged into a metered Z-wave switch reporting back wattage.
  • Third device is a Z-wave IR blaster which I’ve been able to get the toggle command mapped to and works great.
  • Fourth device is this virtual device is my “smart” on/off command + status for the wife and guests, etc.

Commanding the virtual device on or off from SmartThings works fine, usually. Occasionally though, based on what I’ve determined is timing based, it will get itself confused and into a toggle on/off loop with no determinable ending which is because I can’t reliably update status, so I’ve been checking wattage and sending an on() or off() command to set the status, but then this obviously toggles via the IR blaster and away we go into the loop.

Add to this that if someone turns the heater on locally, I want SmartThings to know the status. The main reason for this little project is actually moreso to alert and/or turn OFF the unit when it’s been left on by accident and the IR blaster itself has no knowledge of the wattage or status of the unit. Really trying to make all of the backend stuff invisible and just an on/off command with on/off status for the wife, etc.

My problem is that generally the DTH handles getting itself status. Perhaps I need a custom DTH that takes power as an input and calculate it’s status that way.

This sounds like a wise strategy.

You’re currently using 4 Devices… that would cut it down by at least 1, right?
And… while you’re at it, you could possibly just extend the Z-Wave Switch to do have that extra input (extra ad-hoc “Command”), and that would eliminate all the Virtual Devices.

2 real devices, 1 virtual device, and 1 SmartApp. I don’t think it changes quantity either way, it will just move the logic to the custom DTH. I still need the SmartApp to Sheppard the data.

I am probably doing a poor job explaining.