Hi all.
I’m wondering if anyone has any good ideas on how to get a prognosis for power consumption. Given the following:
I have data going years back on my households power consumption per hour
I have data going years back on temperatures
What I would like to know in my rules are “how much power will my household consume tomorrow between 9 and 10”. In theory this should be easy, just calculate the historical average consumption on wednesdays then the temperature is x degrees. But at the same time it feels like I’m gonna need lots of coding to achieve it. Anyone who has done any job targeted at doing something like this?
This isn’t going to be super easy to do in an OH rule but it’s not going to be super aweful either. Mostly just tedious. It’s also not going to be fast so you’re not going to want to run this rule super frequently (like every minute).
Break the problem down into parts.
how to get the temperature for days past; but which temp, the max, min, average?
collect the days/time periods where the temperature was the same or at least close enough (± .25 °C?) to the forecast temp for tomorrow between those times
use the collected times to determine the average power used
I foresee a bunch of loops and lots of repeated calls to persistence for each of these three. One loop to search for periods where the temp was close to the forecast temp, saving off the start and stop times for the period. Another loop through those saved time periods to pull the power consumed at that time. Finally a loop to average the power pulled.
In JS Scripting I’d do something like this (note I’m just typing this in, it likely has errors). I’m assuming quantity types.
var forecastTemp = ...; // how ever you get that
var range = Quantity('0.125 °C'); // + or minus the forecast temp to compare to
var startTimes = [];
// Populate startTimes with the start of all the hours where the temp was within rage of the forecast temp
var currHour = time.toZDT((time.toZDT().hour-1)+':00'); // start of the previous full hour
var avgTemp = items.MyTempItem.persistence.averageBetween(currHour, currHour.plusHours(1));
while(avgTemp !== null) {
if(avgTemp.greaterThanEqual(forecastTemp.subtract(range)) && avgTemp.lessThanEqual(forecastTemp.add(range)) {
startTimes.push(currentHour)
}
currHour = currHour.minusHours(1);
avgTemp = items.MyTempItem.persistence.averageBetween(currHour, currHour.plusHours(1)); // should return null when we get to the end of the data
}
// Get the average power used for the hours where the temp was within range historically
var total = Quantity('0 kWh');
startTimes.forEach(startTime => {
total.add(items.MyPowerItem.persistence.sumBetween(startTime, startTime.plusHours(1)));
})
var avgPower = total.divide(startTimes.length);
There are a bunch of assumptions and edge cases not covered above that need to be dealt with but the above should be enough of a skeleton of an approach to get you started. This totally could be implemented in any of the languages, including Blockly.
Ah, that’s great! There’s some things to change (I’m using Rules DSL and also I don’t want all hours, just the ones with the same weekday and time of day, so I guess I’ll just go one week back at the time instead of one hour) but the principle is exactly what I was looking for.
I would suggest now is a great time to start learning any of the other alternatives. I recommend against new development of rules in Rules DSL. All of the other options have become just as easy to program in as Rules DSL with none of the limitations and weird failures that Rules DSL has. Rules DSL also has not kept up with the features of OH (e.g. there’s still no support for reading or modifying Item metadata) and is falling further and further behind.
If you are not a programmer, Blocky is great and more feature complete than Rules DSL. If you prefer to code in files JS Scripting and jRuby are both mature, feature complete, and just as easy to use as Rules DSL.
You don’t have to port your existing rules over, but I do recommend new development in anything else.
Hmmm, good points. In fact, I am a programmer by profession, mostly using PHP and Powershell in my work, and I have never really got the hang of Rules DSL, especially all the weird data type conversions going on. But on the other hand I have some quite advanced rules already made that I often steal code from, and I have never liked JS nor Ruby neither… But I’ll think about it, thanks for the tips!
Jython has a new maintainer so it’s comming back as an option (I’d treat it as experimental for now). There’s also HABApp for a pure Python environment. Groovy is still around of course and there’s a Java automation add-on on the marketplace.
You’ve got options beyond JS and Ruby.
Though, for the most part 80% of your rules are going to be interacting with OH and for the most part OH’s API is presented very similarly no matter what language you are using. JS in particular is pretty similar to Rules DSL in how it’s interactions with OH are structured. It’s almost a one-to-one translation to port from Rules DSL to JS if you don’t take advantage of some of the nice additions JS adds which Rules DSL simply does not have (e.g. time.toZDT(), rule builder, etc.).
The actual quirks of the language tend to fade into the background. This is especially true for managed rules where the only place where you actually have to deal with the language is the Scripts part with the whole rest of the rule (triggers, metadata like name and tags, etc) being handled independently from what ever language you choose.