How to reuse one rule for multiple similar scenarios

I have read what “documentation” I can find, I have watched numerous simplistic videos and I am getting increasingly frustrated with Hubitat. The most useful stuff I have found so far appears to be written by @bravanel but mostly a long time ago.

I have a lot of heating devices that I basically want to control in the same way. I do not want to write the same long involved rules over and over again for each one, as this would be a nightmare if I need to change anything. I’d prefer not to have to write a lot of code either.

I want to control each heater based on
the time of day (which will change according to whether its summer or winter)
whether I have gone away (for a day or two or a long holiday)
the ideal temperature I want in the room containing the heater depending on the time of day (not the same for each room), which would be nice to have in a table somewhere...
and the actual temperature monitored by a sensor in that room.

Any advice on how to write one or several rules to do this would be appreciated.

Far too many hubitat examples are about lighting (including the rule machine videos). I've got a hue bridge which does all that for me quite easily (but thanks to the developers of Cocohue for the integration). It would be really nice to see some videos that did something different!

The documentation at docs2.hubitat.com should be up-to-date, though there is a wealth of information in the community (and big chunks of bravenel's "documentation" here was lifted to there, often with changes for future updates, for apps that he wrote, including older Rule Machine documentation).

In your case, I suggest describing what you specifically want to do, and then maybe someone can find a way to help you make it happen. Rule Machine, while powerful, may not be the best tool. Thermostat Scheduler and perhaps even Thermostat Controller, or a combination of the two (or even one or both of those plus some rules if you need something extra) sounds like it may be more appropriate for your use case -- but it depends on what that is.

One thing that seems like it may be in your way: hub variables can store certain data types, like strings or numbers, but they cannot store devices. So, you can't make one rule, use a device as a variable, then send commands to that device based on the value of the variable (at least not easily, i.e., with the variable as a reference to the device itself). But you could write one rule, clone it, and swap out the device ... or consider an app that sounds like it's built more for your purpose, as above.

An app that can work with references to devices is webCoRE, which is technically built-in now but still community-developed. It's about as capable as RM (some things hard to do in one may be easier in the other and vice versa; they're just different approaches to the same idea of creating arbitrarily complex custom automations). If you haven't looked into it already, maybe see if it can do what you want.

If not, share more, and I'm sure someone can help. Good luck!

You can easily clone and modify a rule if you just want to make another similar rule. Another thing you can do is use the "Run rule actions" action to call the actions of Rule A from Rule B, if the actions are literally identical.

2 Likes

docs.hubitat.com is very limited.
I have specifically described what I want to do. I did expect answers that mentioned Thermostats. I don't have thermostats, I have temperature sensors.
But it does appear from what you are saying that I will have to consider webCore.
I didn't want to have to use cocohue instead of the built in app. Its a pity that Hubitat rely on outside help to make it work well.

The documentation should describe all of the built-in apps, hub interface, etc. Being a general-purpose automation system, there are so many possible things you can do with Hubitat that it would be impossible to have a document describing every possible automation any user could possibly want to create (though there are still many examples of such things, and even more if you look here in the forums).

Not quite. Do you want all thermostats to do the exact same thing according to the same sensors? If so, RM (and most apps) let you select multiple devices at once and send the same commands, etc. to them. If you want something slightly different for each but the same general pattern, the idea above of writing once and then cloning (and swapping devices) should work. If you want to try consolidating more devices into one "rule," something like webCoRE may be the best option.

You can share hub variables across multiple Rule Machine rules, eliminating concerns you may have about changing certain values individually in multiple rules. WebCoRE can use hub variables, too. This may help, depending on whether your concern is about values you may want to change across multiple rules or about the flow/logic of your actions themselves.

This is another reason I was not sure about your setup. :slight_smile: You could use a virtual thermostat device that controls your "real" device (presumably some kind of switch/relay?), combined with Thermostat Scheduler (for time and presence) plus Thermostat Controller (to combine your sensors, unless there is only one per device, where you could simplify this layer a bit). Or you could look at custom apps someone may have already written for this purpose, maybe [RELEASE] Advanced vThermostat - Virtual Thermostat HVAC control. You can probably find more with a search.

Or maybe others who have created similar automations can chime in.

A side note:

The built-in integration works fine for many people; there were a couple features I wanted that were not available with it, so I wrote CoCoHue to fill those gaps for me and shared it here so anyone could do the same. Providing the ability to use custom code is an important part of Hubitat for me.

3 Likes

I don’t think what you’re trying to do is as crystal clear to others as you seem to think it is.

What are the heating devices you’re using? How are they controlled?

Many people consider all the additional functionality provided by community developers to be highly desirable.

6 Likes

Firstly, my apologies for not recognising your handle sooner, and thanks again for cocohue. I have a number of switches working with my Hue bridge and it’s much better to see and be able to control them within the integration.
I didn’t want to go into too much detail about my set-up as it is highly unusual. I am in the north of the U.K. where we have electric heating using “storage radiators” (lumps of metal housing heat retaining bricks). Electricity suppliers provide power to these at certain times of day (their choice) and the supply is turned on/off by them. I have heaters in several rooms and I want to be able to turn the power supply off when individual rooms are warm enough (monitored with a temp sensor in each room). “Warm enough” differs for each room and time of day (and also when I am away).
We currently have this control automated with an old Vera controller and it involved writing a lot of lua code. I was hoping that Hubitat might be a bit simpler. I shall be looking at webCoRE now.
Thanks for your advice.

It seems to me like you need to write your first rule in Rule Machine and then clone it multiple times? Is this the case?

3 Likes

There are community-developed apps that can combine a switch and temperature sensor into a virtual thermostat device.

That might allow you to make use of more purpose-built apps like thermostat controller or thermostat scheduler?

2 Likes

Thanks, but I wouldn’t have posted originally if that was what I wanted to do. If anything changes I would have to make the same update in numerous copies of a rule. To me that is crazy, and very likely to end up with differences that are hard to track down. I just want one rule. :slightly_smiling_face:

1 Like

I looked at one of those. The idea is good, but I would have to rewrite a lot of the code to make it fit my scenario. I only got the Hubitat last week so it would be a very steep learning curve! I am beginning to think that it would be the nice solution, but I’m disappointed. We ended up having to do that on our old Vera box and there were so many glowing reviews from people moving to hubitat that I thought it would be simpler.

I make some rules that are basically just "routines" that are things that commonly get called by other rules. These rules are basically just lists of actions and many of them don't even have a trigger. These would be groups of actions that I want to perform often enough, or get activated thru many different means, but I don't want to bother having so many actions in all those rules. So instead of including all those actions over and over again in many rules, I just say "Run rules actions from _______"

For example, I have many "arrival" rules that fire depending on who's arriving and at what time. But at the end of all of them, I need to reset everything, so I have a rule called "Arrival Model End" which resets a bunch of switches or variables and stops timers. The last action in all the individual arrival routines is "Run rule actions: Arrival Model End" to save me from putting the same list of rules on each individual automation.

I hope that makes some sense, I'm not fully awake yet.

3 Likes

I am still not sure what you are looking to do after reading this thread multiple times. You are asking for conflicting things here regarding simplicity vs having only one rule. There has been lots of good advice here regarding rules and how to handle heating devices.

As far as the documentation, what does it matter if you are turning on a light, or turning on a heater? The same basic logic applies, IF this (condition), do ABC, ELSE (condition) do XYZ.

One piece of advice nearly everyone says, including the staff who wrote these apps, is to keep things simple. Two simpler rules are better than one complicated one in nearly every case. The complicated rules are harder to write, harder to troubleshoot, and are often just unnecessary.

People let their OCD get in the way of just writing one rule for every device and moving on with life. Once you write a rule, it is pretty easy to edit it if for some reason things change.

If you need a more regular change like for the seasons, just clone the rule and limit each one to a particular season or dates on the calendar. I have quite a few rules that only run in the Summer and limited hours (for fans) but I could quite easily clone those rules and do slightly different logic (lower speed, different times) for Winter.

3 Likes

How are your heaters controled now? Are they simply switches?

With the amount of dynamic stuff in the request it seems, though several rules, the easiest method would be a rule written for each room

Otherwise I think you would need a written smart app with the logic in place in child apps. One install per room so you can set your values for each. Then anytime the logic is updated in the child app it immediately hits them all.

Who is to say you will want the exact same logic when this is all done.

1 Like

I’m a great believer in KISS. Having multiple copies doing exactly the same thing is not simple. It’s what subroutines were made for. I will just agree to differ with you. It’s not OCD. Rewriting (or cloning) something just to change maybe one parameter leads to bloated software and a need for unnecessary amounts of memory/storage space.
Let’s just agree to disagree.
I will agree that I have had some good advice on here
:slightly_smiling_face:

1 Like

Now they are controlled by an old Vera controller using lots of specifically written code. But the same code is used for all of them.

A rule written for each room would mainly only differ in that it uses a different temperature sensor and controls a different heater. With a few more variables like the desired minimum temperature in each room according to the time of day. But the logic is always the same.

I was thinking about writing a virtual “heating control” device. Probably based on a thermostat one. Is this what you are suggesting when you mention child apps? If not, please would you explain further or point me at an example. I am still learning. Thanks.

Have you considered simply writing a Hubitat Groovy App? They really are not that hard to write and maintain once you get the hang of it. You could reuse all the code you'd like inside one app :wink: and it could handle all of your rooms in one single instance of the custom app.

3 Likes

And then release it to the community and become a hero for those w/similar desires. :slight_smile: Lots of peeps here w/Groovy experience who could offer tips and examples.

2 Likes

I mean a parent child smartapp that has this arrangement in the App part of HE

This is my app for Humidity management. It is the same logic in both Humidity manager Study and Master bedroom. The differences are the potential parms used like switch for my humidifer or dehumidifer and the settings for optimal humidity.

You would need a smartapp to relay between the Temp Sensor and the Heater. I don't believe devices and respond to other devices like that.

Honestly other the storing your prefered temp based on time of day a smart app shouldn't be hard to do at all. If i was going to do it i would base it off of that app actually. Just rename a few things.

Yes it does. I think I came across a post that discussed that in a bit more detail. I must see if I can find it again. Thanks.