Basic WebC0RE function questions


#1

As a complete novice at WebC0RE I am trying to teach myself the basics. However, "learn to walk before you try running springs to mind". I have loads of paused pistons that I have attemped to get working and failed so I decided put a few basic questions out there the answers of which will probably help me no end. Please bear with me as I say, I am a complete beginner.

1. What is the proper use of the "else" function in an "if" statement? It is my understanding that it could be compaired to a "false" statement. example:
IF motion is detected,
THEN(true) turn on lights.
ELSE(false) turn off lights.
END IF.

2. & 3. What is the function of the "disable automatic piston state" in the piston settings? I have seen this in a lot of example pistons and it is not put in for fun so must actually do something. Similarly, What is the "Task Cancellation Policy" used for?

4. Finally, for now, what the hell is a "Semaphore" and why do some of my pistons seem to like waiting at them?! lol.

Thanks for the help and advice.


#2

(1) Generally speaking, I avoid using ELSE. The exception is if there is only one trigger or condition attached to it. (such as your example) Be aware that if you had two triggers in that piston, the ELSE would fire at inappropriate times!

(2) Disable automatic piston state lets you change the true/false on your Dashboard to anything you want. Such as:

temp

(3) Normally, when a trigger changes away from the code, the rest of the code will be aborted. (IE: If Contact changes to open is only true for a short moment) If you have a WAIT in your code, then the rest will no longer be true after the wait. Setting TCP to Never means it will continue processing, no matter what.

(4) Semaphore means there is a delay somewhere. There are many possible reasons for this. (sloppy code, loops, network congestion, etc)


#3

@WCmore answered all of them perfectly.
I just wanted share that in my experience the best way to learn is through example pistons.
I didn’t have coding background so maybe it only worked in my case but I recommend you share some of the troubled pistons and see how we can try to help. (I’m not very advanced but there are tons of pros around here in this amazing community.)


#4

Not much to add here except that:
Else refers (as you might infer) to every possible case other than the one specifically stated in the If. As your Ifs gain conditions, it becomes more difficult to keep in mind that Else is anything contrary. That’s why I’d agree with MCmore that you should use them minimally.
Semaphore is a term used frequently in systems programming and generally refers to a method to have the execution wait for some state to stabilize before it continues. Because execution is just stalled at that point, you want to see that “waiting at semaphore” message rarely. If you do see a particular piston waiting consistently, take a close look at the code to see if you can find a more efficient way to do what you want.
I use Piston States a lot for basic at-a-glance information, so most of my pistons have automatic state disabled and then I specifically set the piston state in the code:


#5

Thank you for the great answers! It has definitely helped me start to understand Webcore better. Regarding troubled Pistions, please can i get some advice on the example below?

It is a basic Location mode setting piston that should be fairly straight forward however, i’m finding that the modes sometime do not change to what they should be at the defined times when i am or, have been present. Im also finding that the lights do not turn on when i open the front door on arriving home from being out. My thinking is that the example can be streamlined and if the first part is written better then I wouldn’t need the second IF function block (the if presence for 300sec part).


#6

This is because your only top level trigger fires when you first arrive home.
temp


This is because you have a trigger inside of another trigger. The likelihood of you arriving home at the exact moment the door opens is very slim.


The way I would approach this is:
I would totally drop the light turning on (lines 42-54 and 69-79) from this piston, and insert those lines in your other piston that monitors the door’s contact.

Same with lines 55-63 and 80-88. Those should be moved to another piston that normally monitors that motion sensor.


#7

Awesome thanks, i’ll give it a re-write and see what happens. So really all i need in this piston is IF present THEN set location mode to… ? The lights turning on when the door is triggered when arriving home and the other lines you suggested should be moved to other pistons.


#8

Whenever possible, I try to program a piston revolved around one trigger. In this case, basing it on the “Presence sensor CHANGING TO present” sounds wise. This trigger only fires when you first arrive home. (although GPS has been known to jump around a tad bit throughout the day)

To embellish a bit, you can add as many Conditions beneath that as you like… (IF time is X, then do Y etc) but I would normally recommend having only the one trigger. (only one lightning bolt in the left margin)


The reason I suggested moving the “IF Door changes to open” into it’s own piston, is because multiple triggers in the same piston calls for a very different methodology when coding, and makes troubleshooting (and reliability) much trickier. You likely already have a piston monitoring that door’s contact. You can simply add a condition beneath that trigger that does X if you have recently arrived home.

Or, an even easier solution is, whenever you arrive home, turn on the lights immediately… (Instead of waiting for the door to open) That code is much more straightforward to work with.


Same concept with the motion sensor portion being moved to the motion sensor’s piston.

IF Sensor's motion stays inactive for 60 seconds  <-- Trigger
and
Presense Sensor IS present                        <-- Condition
Then
    IF Time is between 8PM & 4AM                  <-- Condition
    Then
        Turn off Bulbs
    END IF
END IF

Notice my choice of wording above uses only one trigger.


#9

I’ll second what MCmore is saying here. The key to programming tech is to keep the logic as simple as possible so you can follow it through easily. To emphasize what he says above, one thing to keep in mind in webCoRE is that every trigger will cause the piston to restart at the top and execute downward. Building around a single trigger with conditions whenever possible means you don’t have to figure out what will happen when one trigger happens while you’re still dealing with the first. (You can use, the Never cancel tasks option, but it’s easier not to have the potential issue in the first place.)


#10

Thanks for the advice. I’ll try that.

Sorry but it is probably a stupid question but…How would i link for example, the door contact sensor monitoring piston to a piston controlling the kitchen lights via a motion sensor if, the desired out come is when the door is opened then the kitchen lights come on? Baring in mind that the motion sensor piston controls the kitchen lights with i am home.


#11

You don’t have to make one piston activate another. You can turn on lights from any piston.

Maybe something like this?

IF Door's contact changes to open  <-- Trigger
and
Time is between X & Y              <-- Condition
and
Presence is Home                   <-- Condition
    Then Turn on Lights
END IF

If you want different actions at different times of the day, here is a slight variation:

IF Door's contact changes to open  <-- Trigger
and
Presence is Home                   <-- Condition
Then
    IF Time is between X & Y       <-- Condition
        Then Turn on Lights to 50%
    END IF
    IF Time is between Y & Z       <-- Condition
        Then Turn on Lights to 100%
    END IF
END IF

#12

Sweet. Thanks i think im slowly getting it lol.

I think that im quite logically and thought that this would be easy but im struggling so far. The wife is starting to suggest we just use a light switch, almost got a divorce then and then!!! :joy::joy::joy:

I cant believe that no one has done a WebCore “how to” youtube series yet!


#13

The challenge is, all of the best pistons are customized for that household. I have created tens of thousands of pistons, and I can assure you, no two were identical!!


#14

So if i have a trigger to turn the lights on and want the lights to turn off after X then i would usually use a Motion Sensor “Stay” function as i cant seem to get the “wait” timer to work. However, as the “Stays” function would be a second trigger, is it better to put it in a separate Piston or is there another Condition i can use to turn the light off with?


#15

This is my preferred method.
(IF Sensor’s motion stays inactive for X minutes)

Going the other route with a WAIT means we’d have to use TCP = Never, and use slightly different coding in the process.


Same piston. Separate IF block at the bottom.

Just code with this in mind:
When either trigger fires, the piston will run top to bottom, and execute any code that conditions/triggers allows.