How can I create a virtual temperature sensor that averages some physical sensors?

In my bedroom, my wife and I each have a SmartThings button that we use to control our bedside lamps plugged into a Zwave plug. These buttons also report temperatures. So in my "master bedroom" dashboard, I currently see 2 different temperatures. They're usually pretty close together, so I don't really care to much except I want to use this as a learning exercise to become a little more familiar with my Hubitat. I haven't had much of a chance to do anything more than add a few very simple lighting rules.

I've played around with it and nearly got it figured out. I created a virtual temperature sensor and named it "Master Bed Avg Temp". I went to RM and created a rule. I created a local variable called AvgTemp. I set the trigger condition to be when Button1 or Button2 reports a temperature change.

What I can't figure out is how to set my variable to be an average of the 2. And then how do I set the temperature of the virtual temperature sensor to be that of the variable?

1 Like

You can use @bravenel's averageTemp app that is in Hubitat's GitHub.

5 Likes

Have you looked at @Cobra's suite of apps.
He has one called 'Average all' which will do what you want.
hubitat.uk

4 Likes

Andy's new domain is cobra-apps.co.uk :grin:

3 Likes

Currently, both domains go to the same place :slight_smile:
http://hubitat.uk is redirected to https://cobra-apps.co.uk

However; this won't be forever so please update any bookmarks with the new domain

Thanks

Andy

4 Likes

BTW
Average All got an upgrade.. it will now group switches or contacts too :slight_smile:

4 Likes

Oh... so the fact that someone has developed an app to do this, does that mean it's not as simple as I thought?

I created the app a looooooong time ago (more than 2 years)
This was when RM was fairly basic so it was easier to create the app.
But to be honest, most people will tell you for some things (inc this) a little app can be quicker.

You can do this by
copying in bed1 temp to a variable called bed1temp
copying in bed2 temp to a variable called bed2temp
Then
bed1temp + bed2temp / 2 = variable bedtempaverage

Create all your global variables first and then use RM4 to evaluate all your variables.

I only mentioned @Cobra's app because I believe in using what is there than re-inventing the wheel.

3 Likes

That takes me to a login page and if you try to register, it says

**** Please note:
Although this site is open for user registration, it will be a number of weeks before it is complete and you are able to login.
Please be patient, you will receive a mail once the site is open with your login details (It will be worth the wait!)

Many thanks, for your continued support.

I had already tried creating variables for this. But in RM, I don't see a way to set a variable using any type of complex math. I'm sure there is a way, but it's not obvious to me. I can select the two variables and then I just have a dropdown menu of what I can do.

I only mentioned @Cobra's app because I believe in using what is there than re-inventing the wheel.

I don't see a purpose for installing an app when I should be able to do this with a single line of code. I don't like the fact that I have to create an account to access his site without even knowing what's there. I use a password manager, but I don't like creating accounts that I don't need.

This is what my entire rule looks like currently. It works. If I go to devices and manually set the temperature value of the virtual device to something other than 0 and then run the actions, it sets the two temperature values to the current temperature and changes the temperature of the virtual device to 0.

I just can't figure out how to actually get the average of the two variables stored in a 3rd variable.

It’s free and always will be.
The contents are hidden because I want to be able to restrict who has access to the code and ideas.
If you knew my history, then you would understand why I have done this.

But, as always it’s your choice.

Andy

2 Likes

This should have been removed before now.
The site is live but we forgot to remove this notice

Andy

The text regarding the site not being open has been removed.
Thanks for letting us know

Andy

It is cumbersome to enter, but possible in a couple action steps. You need one action step to add the two variables. (One of your above posts indicates that you figured out how to do that.) Then, you need another action step to divide the result by two. It looks like you can use a variable within an action to set the same variable, so you wouldn't need to create an additional variable for the intermediate result.

Weird new problem. Now when I open the app, and click the "select actions to run" button I get a null pointer error.

java.lang.NullPointerException: Cannot invoke method tokenize() on null object on line 2547 (selectActions)

Of course I have no way of knowing what object this function is being called on do I? I could always delete it and recreate it. It's just really weird because it was working earlier when I posted the screenshot. I didn't change anything, and now it's not working.

I missed a step/variable out

copying in bed1 temp to a variable called bed1temp
copying in bed2 temp to a variable called bed2temp
Then
bed1temp + bed2temp = variable bedtemptotal
bedtemptotal / 2 = variable bedtempaverage

This should work

Don't even need a temp variable. Pseudocode:

Temp1 = Button1.getTemp()
Temp2 = Button2.getTemp()
AvgTemp = Temp1 + Temp2
AvgTemp = AvgTemp / 2
VirtualTempSensor.setTemp(AvgTemp)

I had to recreate the rule because of the error I mentioned above, but this seems to be working.