Fire an action when switch is turned on or off while already on or off

Is there a way I can always run a rule when a switch is turned on, even if already on?

Situation: I have an IR device to turn the tv on or off, and I use a virtual switch to maintain the state of the TV, but sometimes the TV doesn’t end up in the correct state after flipping the virtual switch so I’d like the rule to fire every time I, for example, hit “on” on the virtual switch to repeat the command. Is there a way to do this or another approach I should be taking? In other words, I want it to behave like a remote but also maintain state. (Don’t want to use buttons for that reason)

Does your on/off switch support button events? That would be the easiest way to know every time the on button is pushed. Otherwise it would depend on if the driver posts an On event every time you push the button (most do not).

You could do it with two devices. Either a VS with an auto off or a button device, plus a VS to maintain the state. The first VS or button could be used to trigger the rule(s), with the second VS maintaining the state, updated within the rule when it is triggered.

I’m just using an out of the box virtual switch. It unfortunately does not seem to report events unless the state changes. I have noticed that this seems to work with room lights activator devices. Maybe there’s a way to hack this with that.

I thought about this, was hoping there was a more straight forward way to do it with Rules setting or similar. Also, I use this switch primarily through Alexa so I think it needs to be a switch at the top level or it would have to change from “turn the tv on” to something button related, throwing my whole family who I finally have trained into a tailspin. :smiley:

You can. Just put the virtual switch into a Room Lighting instance and control it with an Activator device. Make sure to select “activate even if partially activated” and “turn off even if partially off.”

Depending on the IR device, you might be able to cut out the middle man and just put the IR device in Room Lighting. If it responds as a button or switch, this would work too.

1 Like

Another idea: maybe a virtual switch driver that allows you to "force" a state change (event) for any command, regardless of the current state? Here's one I threw together for this purpose:

To ensure this actually work as you'd intend, you'd need to make sure any "command optimization"-type settings are off in any apps you use. Most Hubitat apps don't do this except for some like Groups and Scenes, but your ability to control things may be more limited with third-party integrations like HomeKit (I don't really think it's possible to turn something on that is already on, one reason a button device or two separate switches may be a better approach -- but it all depends on your particular application).

4 Likes

@bertabcd1234, first of all, my apologies for not acknowledging this post earlier as it has been a week but this is such a staggeringly cool example of this community. Thank you so much for just whipping this up and it indeed totally solved my issue!!

1 Like

FWIW, Alexa is sending repeat on or off commands which is all I need!