Correct way to get value of a switch state (on/off) in rule machine

As the title says , I want to access a switch's state (actually several switch states) in rule machine rule.
What is the correct way to do this ?
I am not an advanced rule machine user, but I read through some documentation. I see there are variables and connectors. But I could not understand how to put a switch's state to a variable.

Also, I am not sure if I have to create a global variable to get a switch's state. Maybe it is already a predefined variable which can be accessed with not additional effort.

Btw, there may also be another option : maybe there is already a custom app which reports states of selected switches. Is there any app like that ?
Eventually I want to announce the states of selected switches through my connected speakers (or Alexa)

To clarify, your goal is to pull the current state of say 5 switches and then have Alexa say them all one at a time like “kitchen… on, living room…off, master bedroom..” and so on?

well I may later add something else on top of that, but initial aim is that.

I’ve never tried it personally but think I’ve seen a question similar to this at least once or twice before. If you haven’t already try searching some older community posts, good chance you’ll find a response in there somewhere.

I started writing an example of how I might personally approach it but a few steps in realized I am not sure how Alexa devices even work. I’ve stored text in hub variables (string) before that I’ve pushed as speech to various Sonos speakers but I’ve never actually used an Alexa device before so I couldn’t say if it’s even remotely the same.

the question here is not about how to make alexa say it.
I am just trying to understand how to get the value. If I can get the value in rule manager, then I can make it a notification.
"alexa speaks" app can make alexa devices play notification btw.

I use the app Snapshot to achieve the result you want.It is available through HPM, and it's use is simple and intuitive.

thanks. I just tried it but did not like it.
Because it has predefined talking phrases. I am using a Trukish speech on Hubitat and when it says "this is all I have to say" with Turkish prononciation it is really funny.

Can I just use a connector/variable in rule machine ?
How ?

I do have this app:

However, the current version doesn't support reporting the status of switches. I plan to add lots of "custom" types, including switches, in the next release. It might do what you're looking for then.

Back to your question, I don't think you need variables at all to do this, unless you want to access these values outside the rule (e.g., maybe you have an app to help you use Echo Speaks and you don't want to do it in a rule). If you do need variables, I'd avoid global variables (part of Rule Machine Legacy) and start using Hub Variables (new in 2.2.8 and accessible under Settings) instead. But I'm not sure you do. Unfortunately, writing rules like this gets messy fast if you have lots of devices--that's the reason I wrote a custom app. :slight_smile: But as you can see in that thread above, here is how I wrote the rule before I wrote this app to replace it. You could do something similar for your switches--just use that instead of the locks and contact sensors I'm using:

1 Like

ok that seems like a good way to achieve what I want.
However, I will have to write 2 conditions per switch.

why do you suggest using this instead of variables ?
how can I connect variables to switch state if I prefer that ?

I should note (which I did not mention above) that I did use a variable, but it was just one local string variable to hold the value of the text to speak. I did not need variables for each switch (or, in my case, lock or contact sensor). You could use a hub variable for this if you wanted, but if you don't need it outside the rule, I'm not sure why you would. This met my needs, so I did not pursue a more complicated solution (except the custom app, ha).

I'd just use an ELSE. Switch attributes can have only two states, on or off. So instead of each little section I have like:

IF (Front Door Contact Sensor is open) THEN
 Set strSpeakString to "The front door is open!"
END-IF

You could do something like:

IF (Switch 1 is on) THEN
  Set strSpeakString to "Switch 1 is on!"
ELSE
  Set strSpeakString to "Switch 1 is off!"
END-IF

So, you're ultimately still looking at two conditions; you just don't need to explicitly write the second one. :slight_smile:

Download the Hubitat app