CoRE - Variables 101

As a new member, I’m attempting to wrap my former Python/C mind around programming logic statements into a handheld application. After reviewing the docs and this board, I am unable to find step-by-step processes for setting up and using variables in CoRE. Thus, I start this learning thread just for conversation about CoRE Variables.

Please, supply case studies and code/screen-capture examples.

Questions:

  1. When should you use a local variable vs. a global variable?
  2. When a variable is global, does that mean it is available to all Pistons?
  3. When entering the name of a global variable, do I place a @ or $ before it? Is there a difference?
  4. Where do you print the result of a variable? i.e. which screen shows the output of a variable for testing?
  5. Must you set the name and initialize a global variable under Global Variables on the main page? If not, what is the Global Variables on the main page meant for?

Case study, how would we get average temperature and print it? I only have it to a point and I don’t know if it is correct.

Piston Mode: Basic
if (each sensor did not change in the last 1 minute)
USING sensor
First: Save attribute to variable->temperature->Avg-number->@averageTemp
And Then: Load attribute from variable->temperature->@averageTemp

Example:

Great, I think I now have an average temperature loaded into the variable @averageTemp, now what? How to I see the resulting value?

How would I:

  1. text myself the value of this variable?
  2. use this value in an other interesting ways?
  3. send this value to another Piston?

Peace.

2 Likes

I tried using variables in CoRE, but when it wouldn’t work for me, I figured I must just be missing something, and gave up. Well, I did ask a question in the ‘help’ thread, but the lack of response on that lead me to think nobody else knows how they’re supposed to work either. lol

Thanks for creating this thread. :slight_smile:

I don’t have all the answers but…

Questions:

  1. When should you use a local variable vs. a global variable?
    I try to just use local when I don’t want to share the value of the variable with other pistons.

  2. When a variable is global, does that mean it is available to all Pistons?
    Yes

  3. When entering the name of a global variable, do I place a @ or $ before it? Is there a difference?
    When creating it just type the name you want to use “variableName”. When using the variable in text you need to use “{variableName}”.

  4. Where do you print the result of a variable? i.e. which screen shows the output of a variable for testing?
    You can do a sendNotification

  5. Must you set the name and initialize a global variable under Global Variables on the main page? If not, what is the Global Variables on the main page meant for?

I can’t remember.

I don’t think you want to use “Save attribute” and “Load attribute”. I think you want to use “Set Variable”.

Here is an example of me using a variable… “diningRoomLightsOnByThisPiston”.

I might not be talking about the same thing you want. However, here is a post where someone is discussing average temp and core.

2 Likes

Here you go

1 Like

@alanrosesf, please update the original post for this thread as you get answers. This is definitely needed!

What are the user defined variables available to CoRE and their functions?
Boolean-Function?
Decimal-Function?
Number-Function?
String-Function?
Time-Function?

How do you know what type of variable to use?

Can you create a variable with the same name of another local or global and if so does the most current setting over-ride the last? (so so important to know this!)

Example:
Boolean Variable-The Boolean data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic in CoRE. Use it when you want to set a condition or group of conditions to a “Hard” True or False state. Meaning it will remain True or False until you change the True or False state with the same variable name and command.

Someone with more knowledge of the definitions can chip in as it relates to CoRE.

So far, I am familiar with Boolean, Number, String and Time. Have no idea what decimal is used for…

Here is an example of a seemingly complex Piston and the associated Notifications as a result. In reality, once you understand CoRE and variables, the Piston is fairly simple in nature.

Printing Variables as Strings

It would appear the proper way to format say, a text message would be as follows:

Hello, your order number #{myOrderNumberVariable} is ready for pickup.

Note, there are no quotes around the curly brackets.

How about some really really basic instructions for creating variables. Screenshots of what core piston pages to go into,…

Just some very basics for getting stared. I have no idea what to put into these pages ::

1 Like

Great thread.

I’ve been wondering how “Execute during validation stage” can / should be used.

Here’s a basic example (a variable wouldn’t be necessary in this specific case, but it works as an example). Let’s say you wanted to monitor a door opening, and send a notification when the door opens with the door name. You would have something like the following:

If any of Patio Door, Front Door, Garage Door contact changes to open
Then Using location
Set Variable @intrusionDoor = ${currentEventDevice}
Send Push Notification "The {@intrusionDoor} just opened"

If you leave the “Execute during evaluation stage” as default, the variable set and push notification commands happen at the same time. This means that the variable value may not have been updated before the push notification was sent, so you could have bad or no data in the variable. If you tell CoRE to execute the Set Variable as part of the evaluation stage, it ensures that the variable value gets updated before the rest of the actions are performed, so you get the correct variable value included as part of the push notification.

Here’s a more complicated example. This is the piston that I use to keep my alarm system in sync with SmartThings (since it’s an old “dumb” alarm system). I’m using the @alarmstate variable to restrict which actions take place. If I didn’t have the “Set during evaluation stage” option set, the variable value would be wrong when the actions were executed and the wrong actions would execute.

1 Like

Case Study: Night Light

The following case study includes use of And-If, Variables, and Nested Loops. It will demonstrate the use of setting, using, and printing variables in CoRE. It will demonstrate the use of printing variables to assist in debugging.

Scene: If it is between midnight and sunrise, and if motion sensor is triggered, set a variable for wait time and turn on Bulb. Wait, then turn off Bulb.

Requires:
1 Motion Sensor
1 Bulb

Before starting, set for expert mode: Settings->ExpertFeatures turn ON Expert Mode

Logic:

if( time is between midnight and sunrise )
{
Send Push Notification we are inside the loop
}
And If
{
if(MS1 is triggered)
Using bulb…
Set number variable to 17
THEN
Turn on Bulb
Send Push Notification we are in the THEN statement and the value of the variable
Wait 15 seconds
}
Else
{
Using Bulb…
Wait variable amount
Turn off Bulb
Send Push Notification we are done and include the ending variable value
}

This shows the screenshots and the nested loops. Click image to view all panels.

Once a variable, @waitTime is created using Set Variable, it is used as a wait timer in seconds for when to turn off the Bulb. Then along each step, add ‘print statements’ or ‘Send Push Notifications’ to watch your progress and see just where in the code you are. Start out very simple. No fancy fades and such. Just simple turn on/off and build from there.

A print statement for Send Push Notifications can look like this:

In End-If loop. @waitTime value is set to: {@waitTime}

1 Like

How about something REALLY simple?

e.g.
I was trying to use variables recently to help with making something only happen ‘once per period of time’.
Since I couldn’t figure it out, and wasn’t getting any response, I just created a virtual switch to act as my variable instead.

So, without any other complexity other than just showing us how to use a variable, would somebody be willing to do this example (preventing an Action from happening more than once per 15 minute period)?

Much appreciated @destructure00 !

1 Like

Simple Example 1 - Hello World

This example will demonstrate the use of Variables. A motion sensor will trigger the Send Push Notification a variable containing text.

Piston Mode: Basic

Before you begin, change to Expert Mode: Settings->Expert Settings->Expert Mode

Logic:

If (Motion Sensor detects motion)

Then
Using Bulb
Set variable
Send Push Notification using variable

Knowledge is like manure. It’s meant to be spread around.

1 Like

Ok as requested. This piston will turn on a light when my front door opens. But it will only do it once every 15 minutes. There is no logic in here to turn it off. Keeping it simple. So ask away if you have questions. But if you wanted it to turn off then you would just add a wait command (and amount of time) and turn off to the light task.

2 Likes

@c1arkbar

Thanks! :slight_smile:
So, do I understand it correctly that the desired effect is that the door could open (hypothetically) twenty or thirty times in that fifteen minutes, but it would only trigger the light to turn on the first time it opens, and then, it will wait until 15 minutes has passed, and then the next opening of the door will trigger the light to turn on, and so on?

Next question is (and this is probably just from my beginner status with understanding how CoRE executes actions/tasks within a Piston; with respect to the order of things being done), why is the code to turn the light on AFTER the code to wait 15 minutes? Does the wait command not effect the timing of that particular ON command?

You are correct, this is designed to only become true once every 15 min. Yes you can open the door as many times as you want in that period.

@c1arkbar has his actions seperated so both the using location and using light will happen at the same time. You could also just use one action but I don’t think there will b any difference either way.

1 Like

So as @2l82try1 was saying. everything that is separated will executely simultaneously. So while the set variable, wait 15 minutes and set variable is above the light command both will execute their first actions at the same time. So sry variable and turn on execute at the same time. And yes with this setup you can open the door infinite amount of times in the 15 minutes and the light will only turn on the one time.

Now to make sure you understand why this is happening. The doorFlag variable is a Boolean. So it is either true or false. In my conditions I check for the flag being false. If the flag is false and the door opens then set the flag to true and turn on the light. Also wait 15 minutes and then turn the flag back to false.

Case Study: Incrementing Variables - Counting Up

The following case study will demonstrate creating a variable and adding 1 to it until we reach 10. It includes variable creation, initialization, and incrementing.

Requires:
1 Motion Sensor

Before starting, set for expert mode: Settings->ExpertFeatures turn ON Expert Mode

  1. Add Piston
  2. Once added, scroll to the very bottom of that page and select ‘Local Variables’

  1. Select ‘Initialize a variable’. Create a variable called ‘counter’ and set its value to 0. Press ‘Initialize!’. This is important otherwise the variable will not exist. Should you need to change this number in the future, you will need to change the value and press ‘Initialize!’ once again.

  1. Piston Mode is Simple (If-Then-Else)
  2. Follow below example to set up If statement
  3. For the Then statement, you will create an action/task Set Variable:
    -set the name of the variable to be the name of the local variable we set at the beginning, ‘counter’
    -set variable type to ‘number’
    -set ‘First operand…or variable value…’ to be ‘counter’
    -Press ‘Add operation’ and select ‘+(add)’
    -set ‘Second operand…Value’ to be 1




As you can see, you have multiple operations you can choose from when applying to variables:

Be sure to remember once your counter reaches 10, to continue testing you must reset the local variable within the Piston back to 0.

Knowledge is like manure. It’s meant to be spread around.

1 Like

Can you please tell me how you are getting “Please put the clothes in the dryer! 2 Hours has passed” notification? Are you using Power meter or some other trigger?

I can’t seem to find the logic for that in the screenshots you posted.

I use a total of 3 pistons for laundry.

The “nagger” piston below incorporates the 2 Hours has passed notification. You should be able to follow it. The variables washeron and dryeron are no longer used and have been replaced with virtual switches and a piston to control them. This way I can easily tell if a load is running in the washer or dryer and start a timer when they are running and how long.

Let me know if you need any further explanation.

The Laundry Washing Machine Contact Sensor is not being used as it was tapped to the drum inside the washing machine tucked in a ziplock bag. Worked good but the Power Meter works better. I will eventually re-purpose it.

Here is the Piston to control the Virtual Switches which are incorporated into the piston above.

My wife thought I was crazy trying to get this all working correctly but laundry is no longer an issue. Yep, all the complaining about how much money I was spending on this “nonsense” has equaled to her doing a full 180 and now creating various HA scenarios for me to work on!

CoRE and Variables makes for endless possibilities in SmartThings!

2 Likes