 Thanks, so I'm going to be talking about a solution I had to a problem I had at home So as he said I go to a bunch of conferences and once after an extended trip, which was mostly vacation I Came home after a heatwave It's very hot in my apartment. I didn't have a thermometer, but it felt like 45 Degrees out in the apartment and which I could believe because it was about Pushing 40 degrees and the windows were closed So I live in Poughkeepsie, New York Can't pronounce that don't worry about it That was Tuesday. I had to leave early to avoid that There's not a lot of heat problems. It's not like Singapore where it's hot all the time During the winter we get a lot of snow during the summer it gets hot, but not normally above 35 So the heatwave was a little bit strange Because of that my apartment, which this is the layout from my apartment complex website The furniture isn't where my furniture isn't where the furniture is here but The apartment didn't really wasn't really designed with cooling in mind because it's not really, you know It's not a tropical climate. So these two red bars on the bottom Those are in wall AC units and that's the only cooling I have in the apartment besides windows So during the middle of a heatwave when it's pushing 35 It's a little difficult to cool the apartment This is the window AC unit that's Very cheap very limited control you can see it's got a fan speed knob and then a Mode knob so top is off and then it's low cool high cool and then low fan and high fan So fan without the Compressor turned on And I don't own this it's included in my apartment rental So I was You know trying to figure out how can I avoid this where my apartment's on fire because I have a lot of computer equipment in my apartment because I'm a nerd and computers and Collecting things kind of go ahead and I didn't want you know to come home to that heat and potentially damage on any of my equipment So I was trying to think well, how would I solve this? Well, if I you know actually lived in a place where they cared about cooling They'd have an air conditioner with a thermostat almost every modern air conditioner I've ever seen has a thermostat whether it's rudimentary or you know something complicated, but I don't have that So I decided I'll build one So just start the process of building the thermostat You have to look at what it is and it's as basic of a closed-loop control device as you can think of it's You set a point You control the device and you sense the temperature and you just loop on that Pretty it pretty simple. So I needed to come up with an approach to do those two things check the temperature and Control the in this case my in wall AC unit So how would I control that AC unit? Well, I don't own it so I can't take it apart my first inclination Would be to rip it apart figure out how to control circuitry works splice in a wire and you know control it with Microcontroller, I don't own it. I think the apartment complex would not be too happy with me if they have when I move out whenever They come in and they see wires sticking out of the AC unit So it's not to do that. I also have no idea any information about that AC unit That picture Literally the only identifying mark on it is the GE logo. I have tried to rip the front cover off Which I accidentally ripped the unit out of the wall There's still there's no information anywhere about Specifically what model it is or how any of the internal electronics work So I had two ideas I can put a relay on the outlet and just turn it on and off that way and leave it on Leave the switch on and just have relay control power to it Or I could come up with some convoluted robotic arm thing to turn the knob I went with the relay As fun as robotics would be I don't think I'd be done If I tried doing that And because of the layout of my apartment, I don't want to be running control wires to that relay So having a wireless relay to control switching on and off the AC would be ideal So what I decided to use was Z wave Z wave is a popular home automation network framework That's run by a company on the other popular one in the space of Zigbee I've used Zigbee before mostly in college for my senior design project And I would have used that if I was building my own devices But because Z wave is sponsored by a company and has licensing around Access to the network protocol The devices that come out on it are much more Interoperable and compatible with each other and my issue with Zigbee in the past was all Of the devices that you buy it's a spec from the IEEE It's not It doesn't always work 100% and if I was building the device I'd want it to be open all the way down Z wave has open protocol specs and API specs for interacting with them But the actual hardware for the network communication is not open so that I Wish it was but it's not so but because of the compatibility. It's very Useful especially for buying things off the shelf instead of building Relays or whatever Z wave connected device and they've got hundreds or thousands of devices out there so it started to use Z wave and then I looked at how I would control that on a computer and There turns out there's a very Well built open source library for interacting with Z wave devices You can buy a little USB stick that has a Z wave transceiver in it and Well, you you use to set up the mesh network for all of the devices in your home And then open Z wave is a little C library with Python bindings that talks to it So I just use that and then I bought that switch Which was rated for the current of the AC unit which I had to measure because I have no idea how much current it actually draws and Then once I had that network setup. I could also use it to leverage more devices in the future So I could put sensors on it or other devices. They make doorbells locks all sorts of stuff So that's also what I did for the Temperature sensor in my living room. So if going back to this there are two AC units one in the bedroom and one in the living room So I needed to sense temperature and control in both I use two of these one for each AC unit and then for the living room temperature because of the way a living room is set up I needed a wireless sensor And because it's wireless I said, oh, I just built a Z wave network. I'll just buy a temperature sensor So I got this multi-sensor which has temperature humidity luminescence UV Motion and a bunch of other stuff. I use it for temperature and It you know it works pretty well In my bedroom Didn't want to do that. I had the Z wave set up But that multi-sensor is $60 60 US dollars. I don't it's whatever that is in Singapore dollars and Lot of money. I also wanted to sense two different temperatures in my bedroom. So going back to the layout The bedroom is here. I call this my data closet. It's got about four or five computers in it and all my networking gear It also has clothes in it because it's a real closet So I wanted to sense the temperature in that closet separately from the bedroom because the bedroom one would be used to control the AC But also monitoring the temper differential between the bedroom in the closet is important because of all the equipment in there So I wanted to do two separate sensory two different sensors for that It also turns out I had a raspberry pi sitting in there. You can see two of them I use the bottom one for some cloud-based orchestration some software that I developed for my day job and then the top one Was just sitting there because I bought two because they're cheap So I said, okay, I'll use that raspberry pi Then I started googling how do what temperature sensors do people use with raspberry pi? And one of the popular ones was this DS 18 B 20 which is a Dallas one-wire temperature sensor so it's you know three pins power data and ground and It's actually I've actually played a lot with Dallas one wire when I was in college So it was a good choice for that. It was also like 50 cents. It's a lot cheaper than $60 And the way Dallas one wire is constructed you can have multiple devices all in parallel because they're individually addressed So it would solve my problem for having two at once because I just wired to in parallel Which is what that daughter board is it's just a little proto board that I wired everything in parallel up to two connectors And I've got two of those temperature sensors one is running through the closet into my bedroom And then the other one is just sitting there underneath the shelf in the closet So now I have all of the sensor and all the switching how am I going to control it I Looked out there for some home automation software a big thing for me was I wanted everything to run locally I didn't want anything to be dependent on the cloud or an external service This is controlling my AC at why do I need to go to someone's server in a data center to turn my AC on that's crazy I also am wary of all of the security involved with that and things and also, you know The recent s3 outage probably taught everyone that you don't want the internet You don't want your data center to go down and then you can't turn your AC off So I was looking at things that I could host locally Came across home assistant, which I have some friends who are very active on using and developing And then I there were some other ones out there But home assistant really spoke to me because it's written in Python and I do a lot of Python development It's fully open source and it supports like everything I mean right now there are over 600 components and that number keeps growing every release and because it's Python It's super easy to write a new component to control any random device or they even do cloud services So if you have a cloud service for home automation It can talk to that pretty easily and integrate it into your local home automation platform So I decided to use home assistant to control it Another advantage with home assistant is it also has really great z-wave support with open z-wave So I said, okay, how am I going to set up a thermostat and home assistant? Well, these are the ones they support today I don't have any of them because I'm building it myself. Most of these are like off-the-shelf things There's this generic thermostat Which I ended up using didn't use it at first though because home home assistant lets you write automation rules So I plugged in all of my sensors and all of my switches and I went about writing all of the rules for how an air conditioner thermostat would work and Tried to get it to do the thing and it turns out it didn't work because automation rules are edge triggered and AC is not So but it turns out someone had already had this problem and wrote a generic thermostat module Which I needed to massage to work for an air conditioner because it was designed for space heaters originally but after throwing some code and getting that merged upstream and Disabled can have a generic thermostat module which could control my AC. So you just define which pair of sensors and switches to control an AC and then it lets you set a temperature So this picture is a screenshot that I took yesterday from my home assistant set up at home This is the web interface For it and I just clicked the living room thermostat and it's I'm sorry. It's so small. I should have cropped it more So here is the set temperature of 25 C and you can see because The AC is off and it's also freezing cold and the heat is on in New York. It's not even coming close to 25 C But yeah, that's the basics of what that generic thermostat module gives you But the other thing it gives you is you know an interface a software interface to control it. Oh Sorry, I forgot my own slides This is out of order. I apologize. So the one thing was with these sensors I needed a way to integrate them into home assistant To get that because this raspberry pi is not running home assistant. It's running on a server That's underneath the shelf in that picture So I needed a way to get the Dallas one wire temperature sensors from the raspberry pi into home assistant so I wrote some software that Polls the sensor and pushes it over MQTT and home assistant can listen to an MQTT broker and Subscribe to messages and treat that as sensor data. So that's what I leveraged and I wrote the software Dallas MQTT Which lets you have an arbitrary number of sensors. It has a YAML file that you define which sensor addresses to use and then it just pushes those over MQTT with periodic polling that you set up and it's multi-threaded to handle Arbitrary number of sensors right now. It only supports one sensor type, which is the one wire thermal temperature Sensor from the Linux kernel driver That's because it just cats that file and pushes it on MQTT after decoding it But you it's written in a way where if I had other sensors in the future or anyone had other sensors in the future They could add on to it and that's also written in Python because it's really easy So One of the issues I had when I first set this up Was cycle time Something people don't really think about because when they buy a thermostat when they buy an AC unit the thermostats integrated and all of this is done Wasn't done for me. So I plugged in plugged everything in turned it on said, okay, let's set it to 25 C It's the middle of summer. It's really hot And this is what happened. You can see it's spiking like crazy The living room was on for four minutes off for two minutes and just going like that Bedroom is about the same a little bit smaller of a room. So it was Whatever Turns out AC is a really inefficient For about the first 10 or 15 minutes depends on the AC unit and since I don't have the data sheet for mine Just guesstimating That was a problem. So I had to introduce the concept of hysteresis to home assistant Push to patch and now it's a little bit more spread out still some work to do This was an earlier version the problem also with this presentation is it's in the middle of a snowstorm So I don't have any data from more recent patches Because the AC is turned off So this is from September and the other one is from August but it's so you know working it so that I'm integrating Efficiency of the AC into the software Right now it's done by hand in the future. I would like to use Real-time data from that scent from that switch which also measures power consumption to try to figure it out Because I could graph power consumption figure out efficiency curves based on that Just by analyzing the data, but for right now it was just I was able to solve it by introducing the concept of hysteresis to home assistants thermostat module So now that I had everything working I Wanted to see what else I could do So I started looking at the home automation rules and home assistant. This is a basic rule Most thermostats that you buy today have set points that you can set throughout the day So you know if it's after midnight you turn the AC off so you're not wasting power So I decided I started experimenting with automation with that so that's what this rule is I'm a night owl. So after 12 30 a.m Turn the AC to 30 degrees which won't ever turn on normally in New York and then After 9 30 ignore that rule that's all it says And see pretty straightforward YAML syntax to define that and that was a starting point for writing automation rules for figuring out Okay, what can I do with now that I have a computer controlling my AC? What else can I do with it? The first thing that popped my head was location tracking and geo-fencing So It's a computer. I've got a cell. I've got multiple computers on me all the time How could I write rules for it to control my AC based on where I am? Should I I can set the temperature higher when I'm not home? And I can pre-cool the apartment when I'm coming home. So I don't come home to a 45 degree apartment anymore I have a set point for you know static temperature. So things don't catch fire But you know I don't need to set it at 25 C when I'm not home But I'd like it to be 25 C when I get home. So I'm not sweating So I looked at what home assistant offered and they had a module for something called own tracks Which is an Android and iOS app that's open source that just reports your location over MQTT And you can push MQTT to a cloud service. I think a lot of the cloud providers have IOT things now That have just an MQTT broker or you can run your own and since I was conscious of not having services Have my data especially location data I said, okay, I'll just run an MQTT broker and I've got it encrypted and Locked off try to be secure And then it just you know it reports where I am I was looking at the map earlier today It knows I'm in Singapore it doesn't do anything because it's a snowstorm and the AC is off But I could write a rule based on that. So now that I have piped my location info into home assistant I can control how the AC functions based on my normal Schedule when I'm at home. So I work from home and to get out of my apartment So I actually talked to people I worked from Starbucks for a couple hours at least every day So I wrote a rule that says okay when my phone is at Starbucks and leaves Wait five minutes and then set my temperature to 26 so that the wait five minutes is because at the time it takes me to drive from my house to Starbucks and through experimentation how long it takes to cool off the apartment and then The temperature is so it's cool. So it pre-cools my apartment as soon as I leave Starbucks, which I think is pretty neat And I've started writing rules for this for other common locations that I go to like the airport So where am I going to go with this? well That location example taught me that with more sensors you can have more automation and even in combinations that you wouldn't have thought of So the thing I was talking about before with the adjustable hysteresis may be doing that automatically with Power usage collection, which is something that I need to fix on the z-wave sensor side You could do something there another thing is my parents have solar cells at their house I don't have them in my apartment, but they have solar cells in their house and that's fully metered you what if you could take the solar data and Control other electronics in your house So you run things that you wouldn't normally run when you're having excess power generation or You know a lot of people use the same platform for things like controlling lights and blinds when they watch TV or other stuff But there are these combinations of things It's where the real power is and in this kind of stuff and that's you know Simple things you don't even think about like a thermostat everyone has one But then you know what could you do with a thermostat if you could talk to all of the other things you have? And you don't have to do that in a way. That's you know crazy and scary and insecure like teddy bears that listen to parents You can do it all yourself in in your house in a way that it's all owned by you And that's that's what I really find interesting about this So I pushed some links up here and I'll go back to the title page all of the slides are on github So you can see the source on how I generated graphs and stuff, too But these are just some some of the things I used and a paper that I read from 1986 Which was the last time people studied air condition or cycle efficiency because that was one solid state electronics came out and The next slide is just questions, so I'll leave this up. Does anyone have any questions help my own time? Yeah, I had that's it just requires writing it that's Yeah, I had thought about that too So that's essentially what the hysteresis thing I introduced was it's Forgot what I call it because I didn't want to call it hysteresis because a lot of people use it aren't familiar with the term Think I called it minimum cycle wait time Yeah, so it's like so I have it set to like five or ten minutes right now And I've been adjusting it based on my power bill So and that's what this paper is actually about because PID controllers came out in the 80s People were putting them on a c-unit saying oh, it makes it better because it runs less Yeah, yeah Yeah, so I don't only a c-unit I mean it's it's a power relay so if the device doesn't like being power-cycled It's not gonna like it because I mean when I push the button on home assistant. I hear the relay click It's literally like I think it's a 20 amp relay So if the device you're using to control with that doesn't like to be switched on and off Probably don't do it. I don't know about this AC. I don't really care Yeah, they don't like it on and off too quickly which was the cycle time bug But you know, maybe other electronics won't have that problem Yeah It's it's really quick Yeah, I was surprised how quick it was because I mean I did Zigbee stuff It's probably like six or seven years ago now and it There were a lot of issues with communication and delay and I was surprised how good Z-Wave was So that's something that home assistant has support for Alexa and maybe a couple of other Ones I mean it's really easy. It's all Python someone actually did Alexa because it's cheap and easy to get one of the little echo things So, yeah, it has voice control through other devices or other APIs and it's integrated into home assistant So if I wanted that I could put it in Yes for that you do that home assistant doesn't care if the component you're using talks to the internet or not I do That's that's the distinction, but yeah, no they have got a lot of like if Well the big images like these I know for example the Honeywell thermostat Just screen scrapes from their website. So it just uses a that's not actually screen scraping It just does the HTTP request to the Honeywell cloud connected thermostat website and pulls the data down and sends commands that way just with raw HTTP But it you know it offs against their cloud service So, you know if their cloud service goes down then you can't control your thermostat except to go and touch it because it's an actual thermostat So I it's running on an unrelated server that I have sitting in the house, but people run it on raspberry pi They also have a pre-package distribution an SD card image. You can just stick in a pie Yeah Yeah, it's it's it's real simple to use I've been real happy with it Yeah, it's so the z-stick has to be on the same box as the home assistant because it uses open z-wave and it's just local USB connection Good on time Okay, thank you