Announcement

Collapse
No announcement yet.

PurpleAir Local Air Quality Index Solution

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    PurpleAir Local Air Quality Index Solution

    Being in California surrounded by the smoke of large fires this summer, I've written a program to take readings from a local PurpleAir sensor via their API and write the contents into a text file, then wrote an HS3 script to read the text file and populate a virtual device. This enables me to integrate AQI into my algorithm as well as temperature to determine if the house should advise us to open or close windows.

    This is not a plugin, but a command line program that writes a text file for the current AQI and keeps another log file for historical readings. I'm not sure how useful the log is, but since I was dumping data into files it was a small incremental improvement.

    You may be able to find a PurpleAir sensor near you at their site (www.purpleair.com) and select maps.

    Is anyone interested in trying this software out?

    #2
    I would definitely be interested! However, I am running the Linux version of HS3 (on an SEL) -- would this still work?

    Comment


      #3
      Alternatively/additionally -- can you provide the source code of your command line tool? I may then be able to jury rig something myself, and then use your script.

      Comment


        #4
        Turning it into a plugin shouldn't be that difficult if the data is accessible. If there's interest I can look into it.

        Comment


          #5
          If you want the source code, it's here on GitHub: https://github.com/dazimmermann/PurpleAir

          Here's the HS3 script I'm running to read the AQI.txt file:

          Code:
          Sub UpdateAQI(var)
              ' Open the file for input.
              Dim Filename As String = "C:\programdata\AQI\AQI.txt"
              Dim lines() As String
              Dim nvpair() As String
              Dim AQI As Integer
          
              Dim filestr = My.Computer.FileSystem.ReadAllText(Filename)
          
              lines = Split(filestr, vbCrLf)
              'get AQI value by splitting the name/value pair on the third line of the AQI.txt file
              nvpair = Split(lines(3),"=")    
              hs.SetDeviceValuebyRef(561, CInt(nvpair(1)), True)
          
              'Using the AQI value, set global variable aqitext (type string) to the AQI range description defined by the EPA
              AQI = CInt(nvpair(1))
              Select Case AQI
                  Case 0 To 50
                      hs.SaveVar("aqitext", "Good")
                  Case 51 To 100
                      hs.SaveVar("aqitext", "Moderate")
                  Case 101 To 150
                      hs.SaveVar("aqitext", "Unhealthy for Sensitive Groups")
                  Case 151 To 200
                      hs.SaveVar("aqitext", "Unhealthy")
                  Case 201 To 300
                      hs.SaveVar("aqitext", "Very Unhealthy")
                  Case 301 To 500
                      hs.SaveVar("aqitext", "Hazardous")
              End Select
          
              'Now set the device string to the station name.  You could optionally set the device string to the EPA text strings above.
              nvpair = Split(lines(0),"=")
              hs.SetDeviceString(561, nvpair(1), True)
          End Sub
          You'll have to create an AQI virtual device and replace the ID in this script (where mine shows 561). If you use the script as-is, it will also be expecting a global variable text string named "aqitext". If you don't want to use that, you can delete or comment out everything between my comments above.
          Last edited by dzee; August 12, 2018, 05:21 PM.

          Comment


            #6
            I'll take a look. This is your "thing" so if you were going to make a plugin out of it I didn't want to step in. Do you have a PurpleAir sensor or using one near by from their Cloud data? I'm wondering if local data is available if you have one of their units. I'll send them an email asking. I have to ask/look because everyone wants local data. If local data can be pulled somehow I may have to get one to develop against.

            Comment


              #7
              jeubanks, I've been adding some doc to the GitHub site on how to set this up, and how to pick a station near you. I'm in CA between all the fires, so AQI became a much more important issue for me this year. I don't have a PurpleAir, but California Air Resources Board (CARB) is running one just 5 miles away, and it's been matching up with the degree of smoke I'm seeing/smelling outside.

              I'd love to have a plugin, but I'm more comfortable with C# and after I looked at the sample HS3 Plug-in code in VB and the other someone ported to C#, I figured I'm not going to get into it for something this simple.I just call this EXE from an HS3 event every 20 minutes, wait a few seconds for it to update the AQI.txt file, then parse it using the script I added above.

              Do you work in C# or VB? If you do a plug-in in C#, I'd sure like to see the source if you'd be willing to share. At least it would be a concrete example. I have lots of other JSON APIs I think would be worth the effort to turn into plugins: SolarEdge solar inverter, IrrigationCaddy, and Hunter Douglas blinds.

              Comment


                #8
                Originally posted by dzee View Post
                jeubanks, I've been adding some doc to the GitHub site on how to set this up, and how to pick a station near you. I'm in CA between all the fires, so AQI became a much more important issue for me this year. I don't have a PurpleAir, but California Air Resources Board (CARB) is running one just 5 miles away, and it's been matching up with the degree of smoke I'm seeing/smelling outside.

                I'd love to have a plugin, but I'm more comfortable with C# and after I looked at the sample HS3 Plug-in code in VB and the other someone ported to C#, I figured I'm not going to get into it for something this simple.I just call this EXE from an HS3 event every 20 minutes, wait a few seconds for it to update the AQI.txt file, then parse it using the script I added above.

                Do you work in C# or VB? If you do a plug-in in C#, I'd sure like to see the source if you'd be willing to share. At least it would be a concrete example. I have lots of other JSON APIs I think would be worth the effort to turn into plugins: SolarEdge solar inverter, IrrigationCaddy, and Hunter Douglas blinds.
                I have family in Redding so I'm aware of the fires. It's a mess up there.

                Currently I'm doing my plugins in VB but I also want to migrate and move them over to C#. It's just a large hurdle with the HS3 SDK. At some point in the hopefully near future I'll be putting my focus on that.

                Comment


                  #9
                  The checked in source code seems to be missing the actual functionality. It is just a entry point with no functionality. The PurpleAir class is not even included in the project.

                  Did you forget to push the latest updates? I would love to use this - might even make it a plugin.

                  Thanks,

                  Jason

                  Comment


                    #10
                    Jason, I had to refresh my memory on how to use GitHub from within VS. I'm a CTO and it's been a _long_ time since I was using code repositories. I had to do a commit and push, so now I see some files in the repo that say they're only a few minutes old. They were there before, but perhaps not referenced in the sln file. I don't see NewtonSoft.json, so you might have to add that reference yourself as I ref'd it from another folder I use across projects on my system. I see the PurpleAir.cs class file was there and didn't update. Do you see it now?
                    ​​​​​​​Click image for larger version

Name:	purple-air-github.png
Views:	521
Size:	42.1 KB
ID:	1246352
                    Last edited by dzee; September 8, 2018, 04:11 PM.

                    Comment


                      #11
                      FWIW, i have a PurpleAir and was able to modify dzee 's script to get the values for air quality, temperate, humidity, and air pressure locally. To access a PurpleAir locally you just use "http://[local ip]/json?show=" in the config file. You don't even need the station number. I pared down the exe program quite a bit as all I wanted were the key values, created virtual devices for each variable and use the "Device History" plug-in to track/graph the values. For my HS script, I just fire the .exe and then use hs.GetINISetting to get the values from the text file every 10 mins.

                      With this set-up I can track the key weather/environment variables off the PurpleAir locally, have devices that I can use in events (if needed) and track the values locally including ones that PurpleAir's map/graphs don't publicly track such as temp, humidity, pressure.

                      I will say that my initial impressions is that the temp registers a bit higher than a pure temp gauge, possibly due to heat from the device itself.

                      I want to thank dzee for doing all the hard work, much appreciated!


                      Comment


                        #12
                        I don't see the .exe file. I see the script to read the txt file.
                        I would like to try this, but I can't build from source. Is the .exe available?
                        Bruce

                        Comment


                          #13
                          Hi dzee
                          Downloaded this to my homeseer box (which runs on linux), put it in it's own directory off the main homeseer directory and ran the exe file with mono and it creates the txt and log files just fine.

                          Now I can set up the above script and report it on my homeseer interface - wall tablet

                          Thanks
                          Bruce

                          Comment


                            #14
                            Great to hear it's working on Linux. Hadn't tested it. Is your Linux running on a PC or a RasPi?

                            Comment


                              #15
                              Actually it is running on a rock64 (raspberry pi like). I am running full homeseer edition, not the raspberry pi edition. It is a derivative of Debian. It has mono 6 on it.
                              I plan on migrating to a gigabyte brix (SEL hardware), running Debian 9 with mono 6. I had a previous SEL from HS, but it was flaky, turning itself off for no good reason, that I could figure out. So not good trying to run 24/7

                              Bruce

                              Comment

                              Working...
                              X