[newbie] Setting a variable?


#1

Hi. I’m trying to make a piston that sends an “open” command to a roller blind, but if it was activated less than 30 seconds ago (and the blinds are still running), then instead it should send a “stop” command.

I was thinking on using a variable that turns 1 and after 30 seconds it goes back to 0, but I’m really new in webCoRE and I’m not finding a way to set it, when I try to add a new task that sets a variable (the one that says location Set variable…) I get an “invalid variable” red text and a drop down menu with no variables (I expected I’d just type its name here).

This variable should only work within this piston, but I read that local variables are reset every time a piston is started, is that right? Should I use a global one instead?

Thanks,
Rodrigo


#2

I was able to create a global variable from the panel on the right and actually it’s better than a local one because it lets both up and down buttons work as stop if the motor is working in any direction.
Still I’d be glad if anyone could help me improve this piston, any recommendations are appreciated.

Thanks,
Rodrigo


#3

The general rule of thumb is to use local variables whenever possible. They write instantly, and are immediately accessible. On the other hand, if you use global variables, they are not written until the piston completes execution. This leads me to think that line 26 will never write ‘true’, or if it does, it will only be for a microsecond.

The other thing that needs attention is line 28 will never execute since after 30 seconds, the button will not be pushed. The solution to this aspect is to select that “With” statement, and set “Task Cancellation Policy” to never.


#4

Thank you for your reply!
This piston is working but it’s too buggy, so I want to try the local variables. My problem is that I don’t know how to create a local variable. I only found the button to create a global variable (Sorry, I’m really new to this!)
My main goal is that the button starts the motor unless the motor is running. If the button is pressed again and the motor is still running, the motor should stop. As I don’t have a way of knowing if the motor is running, I use a variable that stays true for 30 seconds (the duration of the blinds full open or close travel). Does it make sense? What should I change?

Rodrigo


#5

I don’t have those blinds to test, but I believe this should do what you want:

(it uses local variables, so if it works for you, you can delete your @ns1 global)

Let me know if you need help importing this piston.


#6

You can edit any piston, and then choose “Options” in the top right.

temp

If you check “Show variables” then you will see a new block of code at the top of each piston to create local variables.

temp


For reference, most of my variables have an “Initial value” of (no value set).

temp

What this means is the variable will never change unless the code tells it to.

On the other hand, if you input data when you set the variable up top, then that data will be forced each time the piston executes.

Generally speaking, leave it at (no value set) unless you have a variable that doesn’t change.


#7

Thanks again WCmore for the tips, your code is already running better than mine :slightly_smiling_face:
I see something weird:
png
Does this mean this variable is set to true? I would like the variables to turn to false after 30 seconds of pushing the button. Any idea what could be happening? (more than two minutes passed since my last press)

Rodrigo.


#8

It should be setting itself back to false after 30 seconds…
(or sooner on a second press)

Can you post your latest green snapshot please?


Something else to keep in mind:
Each manufacturer of SmartButtons has a different timer to designate between a single and a double press. If you are pressing too quickly back to back during testing, it may be registering as a “double press” (meaning the last block would not fire)

If you are trying to get a precise angle on the blinds by rapid start/stop, you would need a ‘quicker’ trigger… and even then it would be tricky to nail it every time. If you are considering this route, I would go with a timer programed in, and an auto stop partway thru the rotation. It will be easy to tweak, and the final angle should be fairly consistent.

Such as:

IF Blah blah blah
Do
    Close
    Wait 1500 ms
    Stop
END IF

#9

This is the current piston, I tried to import it from the code you gave me but it didn’t work so I recreated it. I hope I copied it right.

My blinds are not installed yet but based on the motor speed and core size I calculated a full open or close trip to be about 30 seconds. Mostly my current manual blinds are fully open or fully closed, once in a while at half, so I don’t intend to have much precision.
Most actions I’m running on services like IFTTT or Stringify have a ~5 second delay, some actions on webCoRE are taking a bit longer.
During these tests I’m doing, I’m giving it more than 5 seconds. Usually I don’t press the button for the second time until I see the action started,

I’m using Aeotec Nanomote Quad. I’ve talked a lot with the support guy and I set up other buttons with Group Association commands that work even if there’s no internet or the hub is disconnected. I’ve seen the command list from the manual, and there are two for each button: press and long press, I haven’t heard of any double click actions.

Thanks,
Rodrigo

EDIT: I just created a plain Up action (just button 1 > motor up) and the delay is 2-3 seconds with almost no errors (once I pressed and it didn’t work and waited and pressed again and it worked fine.


#10

The only thing I see missing is your line 27. That “With” statement should have “Task Cancellation Policy” set to Never. Otherwise, line 33 will never execute


#11

Thanks, it’s working properly now! I don’t know why I couldn’t import your code, it showed a blank piston. Now I see the little N beside the “with” line :slight_smile:

Rodrigo.


#12

One doubt about this piston I created:

I ended up using global variables because of this:
I have one piston for UP/STOP and another piston for DOWN/STOP. I wanted them to work this way: If I press UP the blinds start opening and then I press DOWN while it’s moving, I want them to STOP instead of moving down.
This works fine but has a slightly longer delay than the local variable so I was thinking, can I put these two buttons inside one piston and then use local variables? Like, simply create another “if” after line 43 with the same code but changing the button and the open/close action? Does it make sense?

Thanks,
Rodrigo


#13

I do not think global variables will work as you’d expect, since the piston would not write to the global variable until the piston has completed all it’s steps. This would effectively destroy any logic we have programmed. Using local variables are instant, and seen by the piston, so should work well.


I would probably need to see the other piston to answer that question.

How many buttons are you devoting to your blinds?
The piston above does Open and Stop with only one button.


#14

Global variables are working perfectly for me, every time I press the button it either starts or stops. Then if I press one button (up) and press the other (down) after, it starts and then stops.
With local variables, it would start up and then briefly stop and start moving down.

I’m talking about this. I’ve seen in another example in the forum and would like to know if it’s right.


I’m going to try it, but I’d like to know if this way o managing pistons is right or if it could cause problems.

Thanks,
Rodrigo


#15

That last piston has 9 references to variables…
8 of them are local
and one stray global on line 54.

I would delete that one @ so they will all be uniform


#16

Yep, missed that one. Thanks, it works good :slight_smile:

R.