How do you get rule to trigger when ON or OFF is pressed and the device is already ON or OFF?

Question:
How do you get a Rule (or Group or Mirror Me) to Trigger when a Virtual Switch (or Physical Switch) receives an ON when its already ON or receives an OFF when its already OFF?

General Scenario:
Say you want to use a Virtual Switch to control a group of lights. Have a few options: Groups, Mirror Me or a Rule. Now one (or all) of the lights in the group of lights you want to control are OFF but the current state of the Virtual Switch is ON. So you instruct the Virtual Switch to go ON (via Dashboard, Alexa, Device Tab, etc). You can see the ON get logged for the Virtual Switch but since the Group, Mirror Me or Rule you defined already sees the Virtual Switch is ON nothing is triggered.

Real Life Example:
Wife to Alexa: "Alexa, turn off the family room lights"
Wife to Me: "The lights in the family room aren't working"
Me to Wife: "What do you mean the lights in the family room aren't working?"
Wife to Me: "The desk light and the lamp were on so I told Alexa to turn off the family room lights. She said ok but nothing happened".
Me to Wife: "Sometimes you have to tell her to turn the lights on and then tell her to turn the lights off"
Wife to Me: "That's just stupid"
Me to Wife: "You could just say Alexa, turn off the family room desk light and turn off the family room table lamp"
Wife to Me: "Like I'm suppose to remember the name of every light and lamp"
I told Alexa to turn off the family room lights but the lamp stayed on"
Me to Self: "Damn I hate when she's right!"

Comments:
Groups partially gets around the issue with the "Use group device to indicate if any members are on" or "Use group device to indicate if any members are off" mutually exclusive options.

Thanks, Glenn

Hi Glenn
My understanding of the trigger function is "when something changes" If the virtual switch is on and you tell Alexa to turn it on, nothing changes, therefore no trigger. I suppose you could define another virtual switch for off (with a reset on both) so when Alexa changes one there is a trigger and the switch is reset for the next "change".
I've not tried this as I use the grouping function in Alexa, not Hubitat. This seems to avoid the issues you are having. Good luck on sorting it out.

Yes, that's the part I'm trying to get around or find another way.

You could make a custom virtual switch driver that always sends the on/off event with the flag isStateChange: true in the event. That would fire the event no matter what the state was.

I didn't mention it but the same issue occurs with dimming. If you set a dimmer to the value it already set too nothing is triggered.

Never created a custom virtual switch. Are there instructions or examples around for that?

Something like this:

metadata {
    definition (name: "Virtual Always Switch", namespace: "dan.t", author: "Daniel Terryn") {
        capability "Switch"
    }   
}

def on() {
    sendEvent(name: "switch", value: "on", isStateChange: true)
}

def off() {
    sendEvent(name: "switch", value: "off", isStateChange: true)
}

def installed() {
}
6 Likes
  • Go to "Drivers Code" -> "New Driver"
  • Copy the above in there, hit save
  • Go to "Devices" -> "new Virtual Device"
  • Give it a name and select "Virtual Always Switch" as the driver
  • Share that driver in the Amazon Echo app
2 Likes

Here is the same thing as a dimmer

metadata {
    definition (name: "Virtual Always Dimmer", namespace: "dan.t", author: "Daniel Terryn") {
        capability "Switch"
        capability "Switch Level"
    }   
}

def on() {
    sendEvent(name: "switch", value: "on", isStateChange: true)
}

def off() {
    sendEvent(name: "switch", value: "off", isStateChange: true)
}


def setLevel(value, duration = 1) {
    sendEvent(name: "level", value: value, isStateChange: true)
    if (value == 0)
        off()
    else if (device.currentValue("switch") == "off")
        on()
}

def installed() {
}
5 Likes

Thanks, I'll give that a try.

The more I think about it, I kind of want it to work like a button. Button 1 is On, Button 2 is Off. These would always get picked up by a trigger. Problem is, Alexa doesn't work with buttons.

That is correct, you can't do that, buttons can't be done with Alexa, stick to the virtual switches

In addition, buttons can’t be On or Off, they can only be pushed or released. On and off is a switch which is exactly what is above

2 Likes

@dan.t your "always" virtual drivers completely solved the issue. They even work with Mirror Me app! :grinning:

Is what you shared common knowledge and I somehow just missed it?

An additional feature I noticed is that when you turn on the virtual dimmer it sends the On as well as its current dim level. The standard virtual dimmer doesn't do that.

Thank you very much. You have no idea how much this is going to improve my automation.

Glenn

1 Like

Is it common knowledge? I don’t know as most people only want to react to events when there is an actual change. E.g. you don’t want to get an announcement if there wasn’t a real change. What I did above is surely no secret, there are several others that could have done the same. There are scenarios where it does make sense to always react to an event even without change, like your scenario. All we want is to keep the WAF high. And that is the beauty of this platform. Allowing to create custom drivers and apps allows us to build a home automation system that fits OUR needs and we don’t have to work around that “standard” way that works for 90% of people.

Glad that it worked for you!

4 Likes

This shouldn't be needed if you have your lights set up in Alexa by rooms. In google at least, if you say turn off the "room" lights, you get an off command sent to every switch in that room. How do you have your lights assigned in Alexa? Do you have them set up in Groups in Hubitat and is that how you're trying to cross them over into Alexa?

Also, having a virtual switch that you can issue a command to multiple times isn't going to help this situation. Obviously Alexa tried to turn off what she thought she should. Doing it multiple times isn't going to get the lights to turn off.

It actually does in his case.

  • He had one virtual switch that was exposed to Alexa
  • he has a rule in RM that acts on the state changes for that switch and controls other devices
  • one device could have been manually controlled on not received a command for some other reason
  • being able to issue multiple off commands will trigger the rule multiple times and control these devices

One thing I have learned and embrace is that there are several ways to achieve a goal. It might not be the most logical for you but it is the most logical for another person. It wouldn’t be the first time that a user only shares virtual devices with Alexa and not the actual physical device. There might be other reasons on why a system is structured a certain way that we don’t know about.

1 Like

Why? There is no need for all of that complexity.

Personally, if I'm "Rube Goldberging" something and someone sees an easier way to do it, I wanna hear it.

In this case, if you don't say the name of the virtual switch exactly right, it won't turn off the lights. If you use the room name, you can say "Turn off the bedroom lights." or "Turn the lights in the bedroom." If you use a virtual switch titled "Bedroom lights" both those commands would not work correctly.

Plus, you remove one extra RM rule that is not really adding any value. With everyone bitching about hubs acting slow, shouldn't we do all the easy things we can to reduce overhead? I mean, why make it more complicated just because you can?

Not sure where to go from here based on your response, he has a solution that works for him and he is happy with. That is what counts

4 Likes

You don't have to "go" anywhere. Every response doesn't necessitate that you respond too. You thought that was the best way to do it. I was proposing a simpler way to do it. You don't have to agree with me. That's okay. My feelings won't be hurt I promise.

I like to use HE with a Virtual Switch rather than Alexa or Google Groups to control groups of lights. This way Alexa (or Google) only need send one command across the internet to HE and HE handles the On/Off and Dimming locally. Additional, by setting up my groups locally I don't have to replicate groups across the various platforms I'm using.

Another benefit of grouping with the HE is you can put groups within groups. I've created Virtual Dimmer [Groups] for each of the rooms in my house. Then I created another Virtual Switch called Main Floor Lights and put all the Main Floor Virtual Switches into it.

In my opinion, it was much easier and way less maintenance to use a Virtual Switch for groups of Light and use Mirror Me or a simple rule. The lights response quicker and Alexa always replies with ok because she's only looking for a response from the Virtual Switch, not every light she changed.

I still put the the virtual switch into an Alexa Group (room). That way no matter what room I'm in (have an Echo in and assigned to each room) you can just say Alexa, dim the light to 25% or turn the lights off.

Now if Amazon (or Google) would finally implement the ability to create "device room aliases" we'd really be getting somewhere.