CoRE - Variables 101

Here’s an example of a Switch statement action, if that’s what you were looking for.

Thanks. Have switches working. Also just learned FOR loops. Check out the above NightlightPro example that uses switch statements.

Thanks.

1 Like

What power meter do you use for your dryer?

Iris Smart Plug

So to have a variable displayed in a message I simply put the variable in the message (ie. Justin returned home at $now).

However, if I want this to sync with a delay how do I add 1 minute to now in the message?

{$now +1}? did not seem to work.

That will work in CoRE (SE). In CoRE, you need to use a Set Variable (time) and set it to $now + 1 minute.

Check out the above example: Case Study: Incrementing Variables - Counting Up

Case Study - Checking Temperature Every Few Minutes

This will demonstrate the use of variables in acquiring the sensors current temperature. It also demonstrates the use of a call to ‘refresh’ to hasten the update of the sensors check cycle.

Required:
1 Sensor with temperature capabilities

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

The entire core sits in a Basic setup and will require you only to add code to the ‘if’ statement. To change the frequency of the checks, alter the ‘happens every X minutes’ section of the trigger.

1 Like

OK. So you have a gas dryer that just needs 110v for the electronics, correct?

Can anyone provide examples of date math/functions? I have two variables I’m using to track start and stop times then would like to display the length of time between beginning and ending in minutes, but I’m having trouble tracking down documentation as to how to work with dates and times.

Case Study - Revolving Counter

This example will demonstrate the use of a variable to keep count of how many times a motion sensor was triggered. Once X number of triggers (iterations), the counting variable ‘count’ will reset back to 0. It also uses a variable ‘iterations’ to set the number of times to cycle before resetting count back to 0.

Required:
1 Motion Sensor

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

Piston Mode: Basic

You will first create a local variable within the Piston called ‘count’ followed by a variable called ‘iterations’:

count = 0
iterations = 2 (or however many times you wish to cycle)

*Don’t forget when you are testing/playing with the code to reset the ‘count’ variable back to 0 if things get out of hand and you lose your way.

Yes, my Dryer is gas.

Case Study - Average Temperature

This example will demonstrate using a local variable to store the average temperatures of all your devices that report temperature. The trigger is a simple ‘happens every minute’ and can be adjusted to your needed frequency.

The variable, ‘temp’, stores the temperature as a decimal which reports with 5 digit precision. If you wish to only see whole numbers, change the ‘Convert to data type’ from ‘decimal’ to ‘number’

Required:
At least one sensor capable of reporting temperature.

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

When we save the variable is where we do the math. By setting the Aggregation to ‘Avg’ or Average, the math will be done for us and average more than one sensor’s data.

Example:

Flash Light Door Open Return Original Level

Ok - I think this is where I want to ask my question. I’m trying to set up a light to turn on 3 times a week at 9:30 pm, but on random days. So one week will be Sunday, Tuesday, Friday. Another is Money, Wednesday, Saturday. If possible I’d like the interval offset by one day, so if the light is turned on on Monday then it will not turn on on Tuesday.

Piston 1

Ok so I will break this down to help you understand or others who might want to implement something like this as well.

  • First off I have the trigger to happen every day at 9:30 pm. That is simple enough.

  • Next we check a variable {prevDay}. If the piston evaluates as true then we will set this variable to true so that the next day it will not turn on any lights.

  • Now we are going to check how many times this week the lights have turned on. We do that with the {@weeklyCount} variable. As long as it is less than 3 then it will continue.

  • {$random} is going to be the final deciding factor to whether the light turns on or not. This is a system variable that produces a decimal number between 0 and 1. So as it is currently written you have a 50% chance of the light coming on. You can increase or decrease that as you see fit but adjusting the .5 up or down.

In the actions portions now if everything is true and the piston fires then the following will occur.

  • We will set the {prevDay} variable to true. This will then turn false after 25 hours. We do not want this to be true for the next day.

  • We will also add to the global {@weeklyCount} variable. This will get reset in another piston.

  • After that you is all up to you. You can turn on the lights, wait, turn them off. Whatever you want.


Piston 2

The second piston will handle the weekly count.

Every Sunday at 10 pm this piston execute.

It will reset the global variable {@weeklyCount} back to 0 so we can start the process all over


Just a couple of final notes. Make sure in the actions you group everything together like this and in this order!

The {prevDay} variable is fine as a local variable. We do not need to access this variable outside of this piston. However the {@weeklyCount} variable does need to be global. Denoted by the @ symbol. This is because we are manipulating that variable in 2 pistons.

3 Likes

I’ve set this up. For some reason I can’t access Location as an option so I stored the variables on my phone as a presence sensor and it seems to work. Over the next week I’ll let everyone know if it works.

Case Study - Turnstile

This example will demonstrate the use of both a local variable ‘count’ and a global variable ‘@totalCount’. We use a global variable so that the total number of people who have opened the door or tripped a sensor will be carried over from day to day.

The daily ‘count’ variable is reset to 0 each day at midnight or whenever you wish. A push notification indicating how many customers you had that day and a total number of customers so far.

Requires:
1 contact or motion sensor

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

Never mind. Got it. How come your piston says it’s basic but you have an else if block?