Fighting Climate Change with Django with Erin Mullaney

This video features Erin Mullaney at DjangoCon US 2022 in San Diego, California, USA.

Fighting Climate Change with Django with Erin Mullaney
0:25:41
Published November 3, 2022
393 views

How can Django help us move the needle away from climate change? At Energy Solutions, we help utilities manage energy demand by running efficiency programs incentivizing the adoption of energy-efficient and electrified technologies. These energy efficiency programs are managed through a Django website we call Iris. In this talk, I will highlight some of the more interesting parts of the Iris Django backend that we have built in the past 3 years.

This talk was presented at: https://2022.djangocon.us/talks/fighting-climate-change-with-django/

LINKS:
Follow Erin Mullaney 👇
On Twitter: https://twitter.com/_erin_rachel

Follow DjangCon US 👇
https://twitter.com/djangocon

Follow DEFNA 👇
https://twitter.com/defnado
https://www.defna.org/

Summary

Energy efficiency can avoid new power plants, save consumers money, and reduce deaths associated with electricity generation, so Energy Solutions uses Django to administer incentive programs for efficient equipment. Erin Mullaney explains how the multi-tenant Iris platform lets many programs share one project while keeping program-specific rules configurable through Excel files. She also describes a major performance improvement that replaced nested database loops with a single PostgreSQL query using Django annotations and JSON fields, a late redesign that allowed one claim to receive multiple incentives, and reporting tools that quantify energy and greenhouse-gas savings.

Key takeaways

  • Iris is a multi-tenant Django application serving energy-efficiency programs across different regions from one project.
  • Program designers configure program-specific fields and rules with Excel files processed through pandas, rather than relying only on Django admin.
  • Moving measure-matching data into the measure table and using Django annotations produced a much faster single PostgreSQL query instead of nested database loops.
  • A database redesign introduced subclaims so one equipment claim could match multiple incentives while preserving the original claim data.
  • Iris tracks electric, gas, and greenhouse-gas savings and provides configurable reports that help demonstrate the climate impact of each program.

Summarised automatically from the transcript.

Transcript

4,008 words · auto-generated Show

Automatically transcribed, so expect mistakes in names and technical terms.

0:21

Excellent. Yeah, so my talk is called Fighting Climate Change with Django. My name is Erin Malini. And before I dive into the main agenda of the talk, I thought it might be fun to give you all a pop quiz that you're not prepared for. Before the pop quiz, a quick definition. Energy efficiency, what does that mean? It means using less energy to get the same job done. For example, a light bulb that uses fewer watts but provides the same light as another light bulb would be considered more energy efficient than that second light bulb. Feel free to shout out A, B, or C. Nationally, how many large power plants have we avoided building since 1990 via energy efficiency investments? You guys know. C

1:06

is always the answer. I just gave my quiz away. So it's over 313 power plants that we've avoided building, which is actually pretty powerful. How much cumulative money has energy efficiency generated in savings for US customers since also since 1990? Again, this is US-based. The answer is $790 billion just for US consumers since 1990 for those energy efficiency savings. Monetary savings. How many lives per day are saved by reducing electricity consumption by 15% by one year? C is correct. It's my last question. You guys all aced it. Uh

1:51

six people per day are s lives are saved. And that's largely due to coal burning states like my home state of Pennsylvania. A little about me. My name is Aaron Mullaney. I'm based out of Hillsboro, North Carolina. I've been a developer for a very long time. I've been a Django developer since 2015. You may have seen me in Django 19, DjangoCon 2019 for the most recent in-person conference, Roll Your Own Tech Job, when I was working for myself. I now work for Energy Solutions. I had been consulting with them. That's why I have two dates here at the time, but I now work for them full-time as an employee. Because I love them. So Energy Solutions was founded in nineteen ninety-five by Sam Cohen, who wanted to move energy incentives

2:40

Closer up the supply chain, which I'll explain in a minute, energy efficiency programs are super powerful. And as we saw in those previous slides, they they really help fight climate change, and that's the mission at Energy Solutions. There's even a policy department there, and they do things Such as building codes and appliance standards. And I think it's really cool to work at a place that works on that level of fighting climate change and getting towards a lower carbon future. Here's our actual agenda for today. First, I'll be talking about energy efficiency programs, and by programs I mean like a program you can subscribe to. I don't mean like a computer program. And Iris which facilitates those programs. Iris is our Django website, our

3:26

Django project at Energy Solutions. And then I'll be talking more about iris. I'll talk about multi-tenancy within iris. I'll talk about some code improvements that really helped our performance. I'll also talk about a major database change that happened way too late in the game after we were already in production and how we handled that. And finally, I'll be talking about Reporting on energy efficiency, how we prove that we are actually fighting climate change with these programs. Okay, energy efficiency and IRIS. This quote is from the Department of Energy website in the United States. Energy efficiency is one of the easiest and most cost-effective ways to combat climate change, reduce energy costs for consumers, and improve the competitiveness of U.

4:14

S. businesses. So that's really powerful. Energy efficiency is a super powerful way to fight climate change, is what we're saying here. But how do we get, and this was a question I had when I started working on energy solutions. How do we get utility companies to agree to do these energy efficiency programs? Why would they want to sell less of their product energy, right? Okay. Yeah. Um so this is a power grid here. And so utility companies are required to provide a certain amount of energy to their customers, period. That's just how they are supposed to work, right? But as they the power needs grow, they may need to spin up a new

5:02

power grid somewhere. And power plants are expensive to build, staff insure, and supply with consumable non-renewable resources. Basically, it's more expensive for them to build a new power plant, and it's more expensive for our planet, obviously, to have more power plants than it is for them to take part in these energy efficiency programs. Okay, so now we've got the and the utilities on board. How do we actually get energy efficient equipment out in the market? I'm gonna step back again and just talk about some sort of incentives you might be familiar with because the way we get people to sign up to these programs is we convince them with monetary incentives, right?

5:47

So every year I go to the optometrist and I get my new prescription, my new annual subscription prescription for contacts, and they usually give me some sort of form and I fill out that form. And I spend a couple hundred dollars on contacts. And if I want to fill out the form and mail it in, then I might get $30 back if I feel like that's worth it. And that's an incentive. In the contact world, contact lens world. But in the energy world, we have incentives on energy efficient equipment. So on the right here, we have a water heater That is energy star rated. It's an energy efficient water heater. And on the left, the manufacturing supply chain where uh such equipment would be created with manufacturers at the top and customers at the bottom.

6:34

Let's say your water heater dies and you own your house, so you have to buy a new water heater. So if you decide to buy a water heater that's energy efficient, you might be able to get a special monetary incentive. on that purchase. And that is called a downstream incentive. And this is just some background on what we do at Energy Solutions. That's called a downstream incentive. It's um maybe not as powerful as the next type of incentive, which is midstream, those distributors and contractors that are actually out there in the market and Setting up your water heater for you and selling you your water heater, they're doing this in multiples. So they're able to get more money on each type of energy efficient equipment that they sell.

7:20

And then at the very top we've got your upstream incentive programs. And those are right directly to the manufacturers. So, you know, so these utility companies, they set up these programs and say, hey manufacturers, if you create a more um energy efficient water heater will give you this incentive and that's called an upstream incentive. Iris is our main Django project at energy solutions where we offer these incentives and we offer them at that sort of higher level of midstream and upstream because the downstream incentives are less powerful really. I know I I just got my contact lenses like a few months ago and I filled out that form and I didn't fill it out in time so I didn't get my $30. But so those downstream incentives are really not as powerful as the as the upper ones.

8:09

This is a couple screenshots from Iris. This is where the contractor that goes and sets up your water heater or whatever would fill out the information for what they purchased. Information like where it was purchased or where it's going to be installed, uh information such as the building type What actual make and model it was, the sales information, the invoice, the sales date, all that stuff goes into Iris to determine what sort of incentive they are able to get. Which brings me to multi-tenancy, my next topic. A quick definition of multi-tenancy. Multi-tenancy means a software architecture where a single software instance can serve multiple distinct user groups.

8:55

So Iris supports programs all over the country. You know, we have California Food Service, we have North Carolina Lighting, we have programs all over the country. But it's one Django website. with many of these energy efficiency programs. One Django project. Many different URLs point to that one Django project website. So, one way we thought of dealing with these many different users coming from different places was to split up the data into system-wide versus program-specific. So System-wide is data that's available to anybody that signs into Iris. So if you um want to For instance, get an incentive for a specific type of HBAC, it's the same HBAC for North Carolina and Michigan, right?

9:45

But then we have a program-specific data that we split out. And that's more specific to those energy efficiency programs. So Michigan requires building type, but North Carolina does not. So that's one way we sort of thought through our multi-tenancy system. The system wide data isn't really fancy. We just have a simple uploader where users can upload Excel files of that type of data. But the program-specific data is really at the heart of what's powerful in Iris, where we have this thing called the config file. The config file has lots of different variables and data points that these different programs require to get from their

10:31

people who are entering their claims, right? And all those variables are available to be set via the Django admin. But for our energy efficiency program designers, we actually have this thing called the config file. The config file is a file that business users love. It's Microsoft Excel. We um just noting here that we use pandas for all of the Excel within our website. I don't have any code samples here, but FYI pandas has really served us well here. This is a little part of a config file. A config file has way more data in general. There's like multiple tabs where you're setting up all this information.

11:17

But for this specific program, we know we're going to ask the user to enter sales date and we want it to show up on the sales information form in column two and we're going to ask the user to enter invoice number and we want it to display in column one in that same uh form section. So we can have our energy efficiency program designers use Microsoft Excel, they download it from the website, it gives them all the information that are that is currently set for the settings for their program. They can muck with it and then upload it back to the website. So If you saw that really cool Django Admin talk, we desi we decided to not force our users to use the Django admin. They still can, but it's nice that they have this feature. And honestly,

12:03

When we ask them if they would like to switch to the Django admin, they are not happy with us. So we are going to keep it around. Okay, next I'm going to talk about measure match improvement. So this is our Big um performance piece that uh uh in Iris that we did a couple years ago. Some background again. Definition of energy efficiency, using less energy to get the same job done. We said that already, right? So In this case, we have a picture of a refrigerator. My husband pointed out that the table is placed really poorly in this picture. But let's say this refrigerator is energy efficient. So some incentive rebate programs may be available.

12:50

We don't call them rebates in Iris, we call them measures. And it's a little bit confusing, but I've heard that we like to think of it as let's take measures to save energy. So a measure gives us the details that can be used to qualify, quantify, and confirm that a claim based on the measure provides energy savings and that the claim can be matched to an incentive payment and report out those energy savings of energy efficient equipment. So a user comes onto Iris, like let's say a contractor who's selling those water heaters, and they start filling out a claim for uh an installation of a water heater. And they give it, you know, give it all the information about the equipment and all the other information about that sale and installation.

13:37

And they submit it and in the back end. the Django backend , iris matches it to the measure, which is again that rebate idea, and provides that information back to the user. Here's some models that are currently in Iris. This will help you better understand this code sample I'm about to show. So we've got our claim data, which again is what the user is entering, about the location, the equipment, the sales information. And that's all in the claim model. And then we've got the measure model where it is actually matching to this. The big change that we made here was previously we had A foreign key, and again, this is high level, this isn't the exact words of the models, but it's similar. We had a foreign key for each attribute that a measure might

14:25

might uh need in order to match and find the correct rebate and the correct energy savings and all that all that jazz. But we moved from that to putting that data directly into the measure table. And that's a huge part of our Performance improvement here. So we have two fields. We have attribute IDs, and these are the IDs of the attributes that are absolutely required for matching, and then we have this JSON field. The JSON field looks something like this, where you have data that you need to match that claim to the correct incentive. So, you know, your manufacturer is ABC water heaters, if that's the manufacturer of that water heater, then it's gonna go ahead and maybe match to this measure if it was also submitted

15:10

after 2023, uh, January 1st. And then when we've got match criteria, and again, this is just to sort of understand the code better. This is a little more data about matching, where we've got our measure field. And that's uh in this example here, this is end sales date, the match attribute, invoice date on the claim. The match expression says if the invoice date is before or equal to the end sales date of this measure, and that data is on the claim, then it gets, you know, it matches this one match criteria So our old measure matcher was incredibly slow. If you went to that query talk, um that was really excellent. There was a little offhand comment like loops within loops, duh.

15:55

That's not gonna work. So there's some loops within loops for you. in production code. And it l it basically is keeping track of these matches in this list and then it's comparing them to the required matches for that measure. So That's the old measure matricode. That's in picture form because somebody did this picture and I was like, oh, I like that picture. Um Enter Victor Rosha. I think a few people here maybe know Victor. I don't know. I don't see any I see some cactus people. You know Victor. So Victor was contracting with us and he's super brilliant and awesome. And in 2020, he said, listen, I want to improve the measure matcher. We had all been talking about it, but he's like, I have an idea.

16:43

And his idea was to use Django Annotation Clause. So he built this for us. Where we are looping through every condition that's required for those measures. And we take that JSON field, so we've got a really long filter here where we're saying attributes data, that's that JSON field, right? Double under manufacturer, that's And let me go back to do do do do so many clicks. Okay, so that's So we see that the top level was attributes data, and then we go down to manufacturer, and then we go down to value db, which is where the actual match happens. Doo doo doo doo doo.

17:28

Okay, so we we go through each of those conditions and we build a list of conditions that we want to match. And then here's where we actually plug it in to the additate query. We're using that case when and we're saying When those two conditions are true, then one, else zero. So we repeat that for every condition in the measure, and then we go ahead and do that. This is one single Postgres call. That's it. There's no raw SQL and there's no loops within loops that are calling J that are calling the database multiple times. And it worked. This is the Postgres version, which I think I used Django debug toolbar

18:15

and sort of simplified this into some pseudocode here. And this is the improvement. I took three different energy efficiency programs and tested them locally with the old Measure Metricode versus the new Measure Metricode, and this was the improvement. So it's pretty cool. All right, so this is my fourth agenda item. It is not as Django specific as that last one, but it was more planning specific. So what do you do when you have a major database change late in the game? So we As I was talking before about those config files, energy efficiency designers can come to Iris and spin up these programs without asking us usually to add that much code.

19:02

We have to add a little code here and there, but Not a ton. And in this case, a new program came to us, and it was a huge, big, important program, and we had to do it very quickly. It's called TechLean California. It's California's flagship heat pump market transformation initiative for space and water heating. It's a super powerful program and the incentives actually sold out really quickly. So it was it worked really well. But that's after we did the database change. So the thing that it required was that claims could now mul match to multiple incentives. Originally, a like I said before, a contractor would come to our iris website and would enter their claim. And they would get an incentive and they would find out what how much energy they're saving every year by using that type of energy efficient equipment.

19:54

But Iris was built With a one-to-one relationship. The measure that it was matched to was right in that claim, again on that JSON field that I talked about earlier. So It was in there, and we had to move it out for all of the programs in order to use to introduce this new program. What we ended up doing was splitting up the data such that everything the user entered moved onto that, stayed onto that top-level claim model. But we added a new subclaim model, and uh any multiple of subclaims could have a foreign keyback to the claim, so a user could enter data. for a claim for energy efficient equipment

20:40

and they might get two different rebates from from two different sources or or more. But this was a huge, huge change, and we did learn some lessons, so I just kind of wanted to include it in this presentation for that reason. I if I had to do it again, I would make my decisions in a smaller group. We had a lot of like slack polls and Um, we involved develop all the developers on Iris in meetings. And in the end, I would have gone back and just had me and my fellow code base lead, Cabret. And you know, with some maybe input from other people, just make the call and just decide on what to do because we kind of spent a lot of our time planning. We had good coverage in unit tests, so luckily when we changed those, we were able to catch the bugs of code where we missed.

21:32

uh things when we made that switch over. And finally, we had recently started this front-end automat automation testing suite, which tests the front end of the website. If you've ever seen one of those where, you know, it just sort of like goes through and enters a bunch of data and makes sure that it works and the data comes out correctly. That also had to change because we had this new idea of multiple subclaims and multiple incentives, but that also really caught some some good things. So that's that's one other thing we learned. Finally, let's talk about reporting. So, how do we actually show that these energy efficiency programs are fighting climate change? Well, aside, so I talked a lot about incentives and what people

22:17

could get monetarily when they enter a claim for one of these energy efficiency programs. But Iris is also tracking savings data, and that's not monetary savings, that's your saving energy when you're using this water heater. You're saving energy. We know how much energy you're saving. compared to a baseline each year. And we track these in these two variables. One is for electric equipment in kilowatts or kilowatt hours, and the other is for gas equipment in therm. or in BTU. So we track all this in iris. We have this data. We can you it can be used. Um This is our report builder in Iris, which shout out to Scott Hacker for designing this.

23:03

Our energy efficiency designers really like this this uh This report designer here, they can select what they want to filter down on, and then they can select the data that they want to show. So you see here our our energy efficiency program designers are actually really savvy if this looks like model names, it's because it is. They're really savvy. They can they can handle this and report out again in Excel. And again, we use pandas for that. It's super powerful and our business users love to use it. I think I'm getting, I thought I had one more minute. Do I have one more minute?

23:48

Okay. Tech Lean California, again, that program that I talked about before. We built this website in Wagtail, and it's really beautiful. I'm just gonna skip ahead. It has some cool reporting features. Doo to do. But on this screen on the website, it's at techcleanca. com, I think. org. Anyway, it'll be in my notes. Um This data point here, 2,900 metric tons of CO2 per year in greenhouse gas savings. Hey, that's great, right? What does that mean? I I don't I have no concept for that. I don't even have concept for watts when I'm looking at light bulbs. So what does that mean? Um there's a website on the EPA's website uh where you can get a greenhouse gas equivalence

24:37

for those those numbers. In this case it was CO2 per year. In other cases, obviously we also track kilowatt hours and other stuff. But in this case, I'm gonna take CO2 per year and plug it in And that one program alone, that's one program, and there are so many in Iris, uh facilitated through Iris, I should say, saves 564 homes worth of electricity for one year. 625 gas-powered cars for one year and 48,000 tree seedlings grown for 10 years. So it's super powerful. Definitely plant trees. This doesn't replace it, but it helps. Uh thank you for my talk. Please go to my URL there if you're interested in in um

25:22

energy wonk stuff. I have a ton of links there. And thanks for coming.

Questions this talk answers

Why would utility companies offer energy-efficiency programs if they make money selling electricity?

Energy-efficiency programs can cost utilities less than building, staffing, insuring, and supplying new power plants as demand grows. They also avoid the environmental cost of adding more plants.

Discussed at 4:14

How do upstream, midstream, and downstream energy-efficiency incentives work?

Downstream incentives pay the customer after an efficient product is purchased. Midstream incentives pay distributors and contractors, while upstream incentives go directly to manufacturers to encourage them to produce more efficient equipment.

Discussed at 5:47

How does Iris support multiple energy-efficiency programs in one Django project?

Iris uses a multi-tenant design: one Django website serves programs across different regions, with system-wide data shared across programs and program-specific requirements separated through configuration.

Discussed at 8:55

How can energy-efficiency program designers configure Iris without using Django admin?

They can download an Excel-based configuration file, edit the program’s fields and form settings, and upload it back to Iris. The system uses pandas to process these spreadsheets, while Django admin remains available as an alternative.

Discussed at 9:45

How did Iris make matching claims to energy-efficiency measures faster?

The team moved matching data from many foreign-key relationships into the measure table, including required attribute IDs and a JSON field. They then used Django annotations and a single PostgreSQL query instead of nested loops and repeated database calls.

Discussed at 16:43

How did Iris change to let one claim receive multiple incentives?

The team kept the user-entered information on the main claim and added a subclaim model, allowing multiple subclaims—and therefore multiple incentives—to reference one claim. Unit and front-end automation tests helped catch problems during the migration.

Discussed at 19:54

How does Iris report the climate impact of energy-efficiency programs?

Iris tracks energy savings against a baseline for electric and gas equipment, and its report builder lets users filter and export that data. The results can also be converted into familiar equivalents, such as homes’ electricity use, cars’ emissions, or tree seedlings grown.

Discussed at 22:17

Presenters

Note: We understand that names change, people change, and bodies change. We respect each individual's journey and privacy. If you have any concerns about a video or need us to remove content, please don't hesitate to contact us. We will handle your request with care and promptly address any issues.

More videos by Erin Mullaney

More videos from DjangoCon US