Missing something basic

Hi all,
I’m not new to programming, but i’m new to webcore. I’m having trouble understanding exactly how and when an piston executes. My inclination is to do something like “if switch is on, then turn on lights”. But this would require constant poling of the switch value, but i’m not sure if that actually happens? I get that it can be event driven, but does mean that the outter loop of every piston should be an event trigger? If that’s so then i shouldn’t start by polling for “if time is after 10pm” since that isn’t an event. But if i have an event trigger on the outside and an inner loop for some other condition, what would cause the repeated running of the loop to check for a state change of the condition?

The examples ive found so far aren’t totally allowing me to get it. I’d post an example, but i don’t see how to insert my copied code from the dashboard. Do i need to copy and save it as a pic and host it online somewhere?

Thanks!

also post on the webcore forum, those folks love talking everything in regards to it

I generally prefer to use “if switch changes to on”

2 Likes

In most cases, whether it’s a piston or another smartapp, there is an event trigger that the smartapp subscribes to. Such as When Switch A turns on. (As @jkp points out, not “is on.”)

All the other stuff are filters on the trigger event that indicate whether the code should run when the trigger occurs. So it becomes “When switch A is turned on” and it is after 10 pm then…

You can also have the state of a switch act as a filter. “When switch A is turned on” and “Switch B is on” Then…but in that case switch B’s state is a filter. You don’t continuously evaluate it, you only look at it when the trigger event, in this case switch A turning on, occurs. :sunglasses:

As @jkp said, The folks in the webcore forum will be happy to discuss all of the specific aspects of webcore, but that’s the general process whether it’s webcore or any other smartapp.

You can have a trigger of “at 6 pm” because that’s an event you are subscribing to. But “after 6 PM“ is likely a filter on some other trigger event.

2 Likes

It makes more sense when you are familiar with how SmartApps work (a piston is implemented as a SmartApp). Basically they will start up (that happens when you click on ‘save’ in the edit screen in webCoRE), subscribe to a number of device events (i.e. register event handlers for them), and perhaps schedule a wake up. Then they will exit.

You need to know about triggers and conditions. A trigger is a conditional statement based on an instantaneous event like ‘bedroom plug switch changes to on’ or ‘bathroom sensor temperature rises above 30C’. If you have any triggers like that, webCoRE will (by default) understand that it needs to run the piston when those things might have happened, so it subscribes to changes in the bedroom plug switch and the bathroom sensor temperature and runs the piston when those events happen. It doesn’t care which way the switch has changed or what the new temperature is, it just runs the piston and the piston logic decides what happens.

A condition is a conditional statement based on the current state of things. So things like ‘bedroom plug switch is on’ or ‘temperature is greater than 30C’. If there are some triggers elsewhere in the piston, webCoRE just evaluates these conditionals when the piston is executed. You might think of them as restrictions, but webCoRE uses that term elsewhere.

Timing works similarly. The conditional ‘time is 11:00pm’ is an instantaneous event so that is a trigger, whereas ‘time is after 11:00pm’ is a condition. In the former case the piston will be scheduled to run at 11pm, in the latter case it is just a conditional applied when the piston is executed for some other reason.

Now what happens if there isn’t a trigger anywhere in the piston? In that case webCoRE will do something sensible. If will go through all the conditions and subscribe to all the device state changes, or create an appropriate schedule, that could affect the condition. So if you are interested in ‘bedroom plug switch is on’ it will run whenever the bedroom plug switch changes, and if you are interested in ‘time after 11pm’ it will schedule the piston to run at 11pm.

If you view your pistons on the webCoRE dashboard (NOT in edit mode) you will see a ‘lightning’ icon next to the line numbers. That indicates that there are subscriptions active for that line - so in effect those are the lines that are influencing when the piston will run.

It can get more complicated than that, but only if you choose to make it so.

There are webCoRE users who only write pistons that have a trigger somewhere, and also some who limit themselves to triggers based on the same device event (so ‘switch changes to off’ and ‘switch changes to on’ are the same subscription). I don’t think like that and happily use pistons without any triggers, allowing webCoRE to sort things out.

Thanks you for this. As a non-developer but someone who has managed IT for my whole career I was looking for an actually definition of triggers, conditions and the ever elusive “subscribe to”…and you nailed it. This is basically what I figured but even the Tutorial [Conditions and Triggers: The difference?] never actually defined the 2.

Sadly, clear definitions are missing for much of the webCoRE wiki so this really helped.

The one missing piece for me is why I would or would not turn on/off Event Subscriptions in the Piston settings???

Thanks and Happy New Year!

Presumably this allows you to remove any possibility of the piston being triggered automatically, without having to go through the whole piston turning off subscriptions. Why would you want such a piston? The best suggestion I’ve seen is that you could execute such a piston from another piston and it would be like running a subroutine or function.

Oh and Happy New Year. Answering this post should mean I just about stay awake for midnight.

1 Like

Maybe I asked the wrong question. I should have asked why I would turn off subscriptions for a Piston?
What use case would that make sense for? Is that a better question? Something I see people talking about but not sure what that implies.

But I do like the subroutine answer for Event Subscriptions.

Still got 6 hours to go for New Years and already ready to snooze :roll_eyes: