How to take a state machine and code in webcore


#1

1) Give a description of the problem
I think in terms of state machines and need a little help getting started

2) What is the expected behaviour?
see picture… but the goal is that when my wife comes home and trips the garage motion sensor, then the garage door contact and finally the hall motion sensor AND its between 7 and 9 am - say Hello on echo speak…AND only do it once that day.

3) What is happening/not happening?
sorry nowhere yet

4) Post a Green Snapshot of the pistonimage

5) Attach logs after turning logging level to Full

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

note my confusion is in how to wait in S2 or S3 and if trigger doesn’t happen how to go back to start


#3

Try this:
(Note: I don’t have an echo, so replace the wait 0 seconds line with making the Echo say “Hello”)


#4

Wow that is amazing !! I plugged in my sensor names and the echo and it worked the first time!!!
Now when I tried it a second time…its not working so I’m studying the tracing to try and understand why.
The define of the booleans have what appears to be a comment /* true*/ is that a comment or actually setting to true ?
Thanks
Tim


#5

i figured out the answer to my question


#6

Great. There are several really helpful aspects of WebCoRE. One of them is those comments that show the current value of variables. I also find the Evaluation Console quite helpful.
The issue in the second run might be due to one of the state flags not getting set back as it should. One thought I have on looking over the code another time is using an integer that holds the current state instead of the binary flags like this:


#7

THANKS ! That seems a bit clearer… I’m at work so can’t try RN but should the first check include a check that state = 0 ? Also in the define for state should I set to 0 (does that just get run once ever or everytime the piston runs ?)
Thanks


#8

Unfortunately, if you set something in the define section, it reinitializes it every time the piston is run. So, what happens instead is that any time motion is sensed in the garage between 7am and 9am, the state gets set to 1 and the process starts over.


#9

I was afraid of that…so how do I handle the first async……since it seems that I could be in state 2 and get garage motion and set the state back to 1…seems like there should be a way to setup initial values for variables : )


#10

so here is my latest code…
It will not go from state=2 to state=3 even though I move in front of the motion sensor…
it doesn’t make any sense



image


#11

Untested, but I think you’d have to drop the ASYNC’s and change the motion conditions to motion triggers. (and set TCP to Never on the motions)

I am heading out the door right now though, so cannot write it in full


#12

That sounds like a better solution. Try it when you’re home and let us know. I always worry when using Asyncs because the behavior is harder to track.


#13

Here is what I came up with…not home to test…
could NOT find TCP to set to never…where do I do that ?

What is wrong with asyncs ? Why would I use it ?
Thanks for all the help!!!


#14

Normal program execution proceeds from top to bottom. Async allows each section to be running simultaneously. It’s essentially multitasking or process forking if you know those terms. It can be harder to figure out exactly what really is happening. However, it gets you out of a major problem of having the program sitting at a wait statement and not doing the other things you want.
Also, you could get rid of the wait 0 seconds line. It was just a place holder to hold the comment to indicate where you put the Echo command. I don’t have an Echo, so I didn’t exactly know and wasn’t able to put in the proper coding.


#15

any idea how I “set TCP to Never on the motions” ?


#16

Click on the WITH that contains the WAIT, then the cogwheel at the bottom.
You are looking for “Task Cancellation Policy”…


#17

It works perfectly !!! Thanks for all the help !!!


#18

I spoke too soon.
The state is still set at 3 well after the end time.

Is it a problem that I used addMin function? Nowhere does it show what that evaluated to ….

Thanks Tim


#19

First thing to try is to edit the {$time} is greater than… line and change the Subscription method to “Always subscribe”. The lack of “lightning bolt” on the conditional means it’s not subscribing to events and thus would only run if the piston gets triggered some other way.
On the unlikely event that that fails, change $time to Time(dateTime($time24)) which will cast it into a 24 hour time integer.


#20

so it turns out $time variable does not work but $time24 does …is this a bug I should report, if so where ?
Below is picture of it not working…
Other question is how to get the lightning bolt …i looked at the “what kind of comparison” and the ones under “triggers” didn’t seem to make sense and the ones that made sense were under “conditions” …which would imply not a trigger right ?
When I use the condition, it appeared to schedule a check 10 mins after the firing of the piston. It didn’t really matter in this case but is that expected ? Is there a way to make that a different amount of time ?

Thanks
Tim