Variables?

We are talking about variables, but what good is storing a variable if you don't eventually use it somewhere? I'm attempting to show you an example of how a variable can be used to answer one of your original questions.

You’re assuming I know how to store information in a variable which I don’t !! I don’t even know where variables come from..

Here you create a variable type and give it a name (any name you want).

This is an action from the above rule that takes the value from my outdoor temperature sensor and stores it in the variable "outdoorTemp" that I created.

The action in the rule that follows it, sends the stored value in the variable to my thermostat's display.

If you needed a variable that stored a value and only wanted to use it within a single rule, you create and name the local variable within that rule instance. There's a section to create a 'local variable' on the rule actions page:

If you needed a variable to be used in multiple rules and/or needed to create a device for that variable (the device connector can be used so that the variable can be added to a dashboard to display it's value) You create those in Settings > Hub Variables. That's a 'hub variable':

Also every rule has a built in 'Private Boolean' (True/False) variable that can be accessed. These confused the life out of me at first. They're very handy to stop a rule from re-triggering before all the actions are completed from the first time it triggered. You do this by making 'Private Boolean is True' a required expression for the rule. Then the first action in the rule is 'Set Private Boolean False' and the very last action in the rule is 'Set Private Boolean True' (allowing the rule to be triggered again.

2 Likes

I am more confused than ever.. why would I need a variable to represent a device when I can just use the device seem where just renaming things ??

Maybe another example. Let's say I want to turn on an exhaust fan, but only if the average temperature is above 85°F. Let's also say I have 3 temperature sensors spread throughout the house. With these I could write a rule that says every time one of the sensors reports a change recalculate the average of the 3 sensors and store it in a variable called avgHouseTemp. Another rule could then look at avgHouseTemp and decide if it needed to turn on the fan or turn it off.

1 Like

You don't - case closed :rofl:

1 Like

Where are u getting “avghousetemp” from??? I understand it’s a hub variable but how do u put info in it.. how do u get the temp sensor info into the variable there’s no way to link them in the variable setup

You write a rule/piston/app to read the sensor data, calculate the average and write it to the variable.

Hub Variables are created once in the settings.

Then as an action in a rule you can set them to some value:


This is how I progressed with variables:

  • WTF is a variable - people keep talking about them
  • I don't need them my automation rules work fine
  • I want my rule to do something a bit more complicated as I've got an idea for something that would be cool
  • To the community - How do I do this - I can't think of a way to do this with my rule?
  • From the community - Create a variable, do x, y, and z to make your rule work

Now it made no sense at first. However once you have a need for them and you have them set up in your rules to do something more complicated, you can see from your own rules use how it's working and you'll start to find ways to use them to solve problems.

1 Like

Curious how would u get RM to avg the 3 sensors

No the variable is the value output for that device. For instance. This came up the other day. This is very simple.

I have a google speaker. It's playing music and lets say a volume value of 35. This same speaker is used for system announcements. Now since I may not notice the announcement due to the level my speaker is currently at (that 35 would be low music in the background) I would want the announcement to be way louder. But when it returns to playing music I would not want the music to be that loud, I would want it to return to the level 35. With me so far??

So I want to make a rule that would look at the current level of the speaker (35) and insert that number into a variable. We'll call that variable %speak-level%. (You can use whatever name you want for the variable) The next part of the rule is notifiy "whateverspeakeriscalled" but do so at lvl 100. After announcement is done set the level back to 35.

If Door Opens
log speakername lvl to %speak-level%
Send TTS (the anouncement) to whatevergooglespeaker lvl 100.
Set speaker lvl to %speak-level%

As you can see that last bit was already populated by the existing speaker level (in this case 35) so after the announcement was made at 100 it went back down to 35 and exited the rule so that the music playing on it would continue at it's original level.

This is an over simplification but hope it helps.

3 Likes

Variables can also be triggers.

For example, the descriptionText for an event might not be directly actionable. But it can be placed into a string variable, which can function as a trigger, with the actions being dependent on the content of the string.

Make sense?

No I don’t can u give an example please

Sure.

So how would you get RM to average the 3 sensor and send them to the varable

Using rule actions. :slight_smile: The same way you do anything with a rule. The ones you'll need are mostly under Set Variable, Mode or File, Run Custom Action > Set Variable. For example, for your specific request, first* you'd need to store the three sensor values into three separate variables. Here's how you'd do it for one, except you'd choose your devices and the right attributes (temperature instead of level--I just used a device I could show this with):

*Actually, first you'd need to create the hub or local variables of the appropriate type (probably decimal for your use case), but that's easy enough.

For your final step, you'll need to use regular math via "variable math" under "Select operation" to add the variables and then divide by the total number of things you added. (Depending on how you do this, you might also want yet another variable to store the sum as you make it--you can only do two operands at a time.) If you get that far and need help, someone can probably show you, but I just wanted to give you a general picture of the idea since you asked.

Adding to the above: in general, a variable is just way to store a value. Another possible use is if you want to reference the same value in multiple apps but only have to change it in one place rather than "hard-coding" it into multiple apps (and having to change them each every time you change what you want the value to be).

If you can't think of a use, you probably don't need them. :slight_smile: But if you have an automation you actually want to create, can't think of how, and think this might help, someone can probably point you in the right direction.

2 Likes

@user5068 You never gave a specific use case for why you wanted to know how to use variables. Maybe you were just curious.

While the answer @bertabcd1234 provided for averaging temperature using variables is 100% correct, you can see how it's really not that simple to average multiple values with Rule Machine and why there have been at least three different community apps that I know of to average values.

Specifically for averaging temperature (which is probably the most common use case for averaging a value), I would suggest an easier and more flexible method using the Hubitat Built-In app called Thermostat Controller and a simple Rule Machine action to store the result in a variable.

  1. Create a Hubitat Virtual Thermostat device (name it whatever you want).
  2. Install the Built-In Thermostat Controller app and specify your Hubitat Virtual Thermostat as the device to control.
  3. Select as many temperature sensors as you want to average.
  4. Press Done and it will create a device with the prefix thermController

  1. Create a Hub Variable with a name of your choice and the type Decimal

  1. Create a Rule Machine rule with some kind of trigger. I would probably use changed from any of the temperature sensors I specified in the Thermostat Controller app.

  1. Set the rule action to Set Variable and the operation device attribute
  2. Select the thermostat controller device that was created by the Thermostat Controller app.
  3. Select the attribute Temperature

You will now get an average temperature stored in the Hub Variable you created in Step 5 from all temperature sensors you selected in Step 3 and you can use that variable in Rule Machine rules, or Apps and Drivers that utilize a temperature value.

2 Likes

Thanks guys and/or gals I think understand the concept and how to create and use them I am sure the first time I use them I’ll be back for help

1 Like