Solar dashboard using Adafruit MagTag
I recently had the privilege of being able to install solar on my roof and with that came the need/want to track and monitor lots of information about my solar install and my usage patterns of it.
Having recently electrified my home and completely removing gas all together (I plan to write about this too), I wanted to know where I could load shift some of my usage patterns.
Another aspect of monitoring was to know my panels were performing at their optimal, and expected, output. I have a large flat roof at the back of my house and did the calculations on tilt frames vs laying flat, I could fit almost twice as many panels if I laid them flat. I needed something to monitor that their performance was outputting expected kW and the monitoring device/software could tell me if I needed to get up there and clean them.
After lots of research I landed on Solar Analytics as it covered all my requirements and more. They have an ok app that needs updating as it seems to be a webapp shell that was last updated for smaller phones as on my iPhone X it has the black bars. Other than that the software and insights are excellent, I do have lots of UX feedback for them so if you’re reading let’s talk!
Their best feature is the performance monitoring that lets you know based on lots of data points if your expected output is matching what their algorithm comes up with.
Something to glance at
As much as the app and desktop dashboard are useful, I still wanted something I could glance at and get a rough Idea of what power I had available. This way I could think about load shifting some cooking prep, with an induction cooktop and an electric oven I could draw as much as 10kW! So I looked around for some DIY projects with a microcontroller and a display and of course I headed to my favourite website Adafruit! I soon discovered the perfect solution all wrapped up into to one handy device, the MagTag.
MagTag
The MagTag ticked all the boxes:
- It was small.
- It has built-in WiFi.
- Easy to program for using CircuitPython.
- The display is e-ink, so very low power.
- I could run it off a small battery for days at a time.
- It had magnet legs so I could stick it to the fridge.
The device was perfect and had lots of reference material and example projects to help get me started. I’ve opened sourced my code and graphics used for my solar dashboard, if people are interested I could write more about the code specifically.
For UI inspiration I took the layout from the MagTag Weather dashboard tutorial and modified it from there. I’ve also added the psd file to my github repo which you can load in the best image editing website photopea.
Missing API
Unfortunately for whatever reason Solar Analytics doesn’t have an API so this sent me down a more complicated way to gather and massage the data into an efficient format that the MagTag could use. I wanted to avoid wasting precious battery time waiting for an API to perform a series of steps.
To get the data needed for my dashboard I ended up creating a node script that would log into my solar analytics account using a headless browser, I settled on using playwright
The script does the following every 5 minutes:
- Logs into my account.
- Performs some
fetch
requests in the browser to the internal APIs. - Using
Promise.all
it takes the response from thesefetch
requests and combines it into a lightweight JSON payload. - Uploads the json to a private github gist.
To do this securely I didn’t want to setup a cronjob on my VPS as I’d have to expose my account details in env variables, so instead I looked to the very useful, and free, github actions ecosystem.
Github Actions scheduled worker
In a private repo I have a scheduled github action that runs the node script described above, and because github has good secrets management I don’t have to worry about exposing my credentials.
One thing to note that scheduled actions will only ever run at a minimum of every 5 minutes which is fine for my usage, but I found in practice it is more like at least once every 5 - 10 minutes.
name: Solar Stats
on:
schedule:
- cron: '*/5 * * * *'
jobs:
stats:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v2
- name: Install
run: |
npx playwright install-deps chromium
npm i
- name: Get stats
env:
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
run: node index.js
The action is fairly simple, it leverages the playwright cli tool to install the required headless browser needed for my node script.
The node script then handles the rest and uploads the required data into a private gist that my MagTag calls out to.
Putting it all together
Now that I have all the required information and a scheduled github action updating that information every 5 mins I can then tell my MagTag to wake up from deep sleep every 300s (5 mins) and fetch the new info from the private gist. Check out the code on github.
The reason I used a private gist is that I can get the info without having to worry about storing a token on my MagTag as you can request privates gists anonymously. The other tip is to use the /raw
url so you avoid the heavy response from github that contains the gist history too.
Conclusion
Of course this would be way simpler if Solar Analytics just had a public API where I could generate a token and get my info out, hint hint nudge nudge. But never-the-less it was fun stitching this all together into a very useful little device that sits on the fridge.
If you have any comments or feedback feel free to @ mention me on twitter.