Conditional Expression


#1

When my wife or I leave the house, I have piston that sends a SMS notifying if one of the cameras is not on, the door is not locked and a list of matching device for any doors that are not closed. Normally as all the doors are typically closed, I get a message: Door open: Empty Device List, Lock:Locked.

Is there away to have a conditional Expression so I only get a value if a door is open or the lock is unlocked?

I am using, "Doors Open: {Door_open}, Shop is: “[Shop lock:lock]”


#2

Try this in an expression box…

count(Doors_open) > 0 ? "Doors Open: " Doors_open ", " "Shop is: " [Shop lock :lock] : "Shop is: " [Shop lock :lock]


#3

Eiber that works great! I tried to then add the Camera off in the same manner but does not seem to work.

Example

count(Doors_open) > 0 ? "Doors Open: " Doors_open ", "
count(Camera_off) > 0 ? "Cameras Off: " Camera_off ", "
"Shop is: " [Shop lock :lock] : "Shop is: " [Shop lock :lock]

I obviously do not fully understand the syntax.

Jack


#4

I’m on mobile right now but the basic syntax is;

condition ? value_when_true : value_when_false

And yes it can be nested… it helps to build it on notepad or notepad++ for easier viewing


#5

Thanks again. I can get it to work with open conditional statement for either the Door, Camera, Lights etc. But when I try to put two of them in the expression no dice, I have tried about 40 combinations but have not hit the magic combination.

count(Light_on) > 0 ? "Lights On: " Light_on
count(Closed_door) >0 ? "Doors Open: " Closed_door

"Shop is: " [Shop lock :lock] : "Shop is: " [Shop lock :lock]


#6

condition ? value_when_true : value_when_false

You are missing the colon value_when_false at the end of each statement

If you want no data passed upon failures, you can add this to the end of each statement:
: ""


You can learn more about Ternary operators here

You can learn more about Functions here.


#7

I must be daft, can make the one statement work, but not if put colon after if true with or :”” or if I have both the light and door statement. See below

count(Light_on) > 0 ? "Lights On " Light_on : “”
count(Door_open) >0? “Door is open” Door_open :””
"Shop is: " [Shop lock :lock] : "Shop is: " [Shop lock :lock]

But this works with only the door condition or only the light condition as below:

count(Light_on) > 0 ? "Lights On " Light_on

"Shop is: " [Shop lock :lock] : "Shop is: " [Shop lock :lock]

The expression above works fine for lights and locks, but when I add another count condition statement as in my 1st example it does not. I keep trying different combinations without success.


#8

What’s the end result you’re looking for? Name of open contacts (if any) I get that one, shop lock status… what about the camera?


#9

It think what you are looking for is something like this:

           this colon is just text--v                    v--this colon is expression syntax
(count(Doors_open) > 0 ? "Doors Open: " Doors_open ","   : "" )
(count(Camera_off)  > 0 ? "Cameras Off: " Camaras_off "," : "" )
"Shop is: " [Shop lock :lock]         <-- don't need expression here if you always want status

Hope this helps.


#10

Sometimes a visual helps. Here is a nested expression from one of my weather pistons.

temp

It is all one line, but I show it here split to make it easier to follow.

Basically, this says:
If the variable {wuCurrent} contains “Snow”, then set variable {rain} to 90
If that is false, then
If the variable {wuCurrent} contains “Hail”, then set variable {rain} to 80
If that is false, then
If the variable {wuCurrent} contains “Thunderstorm”, then set variable {rain} to 70
… etc …
If that is false, then
If the variable {wuCurrent} contains “Cloud”, then set variable {rain} to 5
If that last one is false, it means that everything is false, then
set variable {rain} to 0. (shown highlighted)

Notice the 9 extra closing parenthesis at the very end.


#11

guxdude and WCmore, Thank you both. quxdudude your example allowed me to better understand and has everything work perfectly. WCmore, your comment will help on some other pistons that I have. The more I have learned to more I can learn. Thanks again


#12

I’m trying to condense a huge location piston into fewer lines. Would it be possible to have Webcore look for multiple expressions amongst multiple devices? I get an error when I tried this. I’m trying to see if any of my Life360 devices change their address1 setting to a particular location.

Or might this work better (I don’t want to have to drive around to test it.)


#13

The syntax for “Contains” is:
boolean contains(string haystack, string needle)

Your first picture above only mentions the ‘needle’, but you also need to tell webCoRE which ‘haystack’ to check.

In my earlier example, this snippet:
pic
will look at my variable {wuCurrent}, and if it contains the word “Snow” then it uses 90… Else, it moves on to the next line.


What I usually do is, gather data the normal way, and store it inside a variable… Then I use “Contains” to see if that variable contains X.


#14

Well, this may all be for naught, because I can’t figure out how to pull address1 from the selected device. I have to specify the specific device to access that setting from the pulldown. Going under $currentEventDevice doesn’t have that option. There’s a Location option, but I’ll have to drive around to see if that works. Bummer… :frowning:


#15

Well, in your case, when Susie arrives at “School”, a piston can store the word “School” into a variable… Then, you can use “Contains” to see if that word is stored in the variable.


#16

One would hope…there’s got to be a better way to write this piston, lol


#17

Sorry I cannot be more helpful in a public forum… When it comes to “Presence”, I know how much work is involved, so I usually reserve “Presence pistons” for paying clients.

I hope you can understand.


#18

No, that’s cool…and a neat way to make some money (how many clients do you have?) I am sending you a PM, however. :wink:


#19

Message received… I will respond in about 18 hours…


#20

PM sent