Table of Contents
Background
Puget Sound Energy (PSE) recently upgraded my house to include a smart meter which allows them to automatically take meter readings instead of having to physically visit my house. A side effect of this upgrade is that I can now get real-time energy readings with the purchase of a small device that will communicate with the meter itself.
Rainforest EMU-2
Enter the Rainforest EMU-2, which is made available to PSE customers at a discount and comes preprogrammed to connect to your meter and provide real-time energy readings as soon as it's turned on. The device itself comes with a display and two buttons allowing me to navigate through a series of screens and see various information from my meter. The most interesting screen, displayed below, gives me the current power usage and associated cost.
While being able to see real-time power readings from my meter is great, the historical view from the device left a lot of be desired and I wanted more. Doing some searching I quickly found that Rainforest has a GitHub account that includes an EMU-2 Python library which interfaced with the device through the serial port! Now to get started integrating the device into Home Assistant
Home Assistant Integration
Being my first time writing in a Home Assistant integration, and actually writing in Python, I immediately started trawling through their documentation and samples to see if I could cobble something together. I started by installing the docker image in a WSL2 container on my Windows machine and connecting the device, then immediately ran into a brick wall when I remembered that WSL 2 doesn't support USB devices.
After trying to get usbipd-win working with some success, I eventually gave up as I was hitting into random crashing and locking up when writing to the serial port within the WSL2 container. Instead, I downgraded the container to WSL1 which supports USB devices and kept going. While much slower, I was no longer seeing the crashing problem and I was able to successfully write to the device and read the response.
From here I created a loop to read the power output (kW) from the device and create my first Home Assistant sensor.
Creating the Sensors
My goal here was to integrate into Home Assistant's new Energy Dashboard as a consumption sensor which would enable me to track energy use and costs over time. To do this I needed two new sensors:
- A measure of the total energy consumed in kiloWatt hours (kWh)
- The current cost per kiloWatt hour ($/kWh)
This data was available by interrogating the device with the get_current_period_usage
and get_current_price
commands. After creating the two additional sensors, my device now contained the following information:
Energy Dashboard
Heading over to the Energy Dashboard configuration, I next added a Grid Consumption and chose the Period Usage sensor along with the Current Price sensor:
The final dashboard was now complete. Looking at the Energy dashboard, I can now view hourly, daily and monthly aggregated data from my meter. It's going to be a while before the monthly view populates out, but already looking at the hourly view, it's nice to be able to point out parts of the day when appliances like the water heater and dryer turn on and what impact this has on my overall power usage.
Try it Out
I have published the source code on GitHub and have opened a pull request with HACS so the integration should be available in the Community Store very soon. Until then you can add this as a custom repository with your Home Assistant..
hi, this is great, what are you using for the get_current_period_usage? i don’t see that in the api anywhere
Hey Chris,
Yeah this appears to be undocumented with the EMU-2. I found this command in a Python3 library located here:
https://github.com/Steve0320/Emu-Api
Ryan, I’ve installed this integration through HACS, but am having an issue with connecting to the EMU-2.
EMU-2 is connected with meter and plugged into USB port on Raspberry Pi 4.
HA finds the new device.
When I go to “configure” it shows the following 4 choices:
1 – /dev/ttyUSB0 – Sonoff Zigbee 3.0 USB Dongle Plus – iTead
2 – /dev/ttyACM0 – RFA-Z105-2 HW2.7.3 EMU-2 – Rainforest Automation Inc.
3 – /dev/ttyAMA0 – ttyAMA0
4 – Enter Manually
But every time I select the Rainforest Device (2), it causes Home Assistant to restart and then fails with “Unknown Error”
I thought perhaps it could be a low power concern, so I plugged in a Powered USB Hub so that it wouldn’t be robbing from the Pi Power Supply… no change… still restarts HA. I’ve also used a “Power and Data” USB cable specifically, since I read somewhere that the cable that comes with the EMU-2 is a charge only cable, which I find hard to believe based on seeing the device description in the menu above. I get the same result… it triggers a restart of Home Assistant and ends in an error.
I’m new to Home Assistant, and the Raspberry Pi, but everything else I have seems to be working well. Is there something I’m missing in configuring the EMU-2 Device?
Hi Jim,
I developed this on Windows and FreeBSD so I’m assuming I’ve missing something. Seems like there is an exception being thrown that I’m not catching but it’s hard to know what it is. Was there anything useful outputted in the home assistant log file?
Removed Integration, flushed logs, reinstalled integration, attempted configuration, as usual it triggered a restart of HA… checked the logs after it finally restarted, but found nothing related. Just this:
2022-04-15 09:44:42 WARNING (SyncWorker_2) [homeassistant.loader] We found a custom integration rainforest_emu_2 which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
But I get that for almost every HACS based integration I’ve seen.
Wish I had more info for you.
Thanks for the quick response.
Jim
I think the problem is that because home assistant is automatically restarting following the crash, any logs for why it crashed in the current log have been wiped away.
If you look in the home assistant config folder, then might also be a home_assistant.log.1 which I believe is the log file from the previous run, is there anything in there?
Otherwise I can add some additional logging to figure out where it is crashing. I thought I was catching all the errors but I must be missing something.
You could also turn on debugging logging for this component by adding the following to your configuration.yaml
logger:
default: error
logs:
custom_components.rainforest_emu_2: debug
Jim, if you’re still running into this, check out this issue I opened on the Github: https://github.com/ryanwinter/hass-rainforest-emu-2/issues/16
I also ran into this, and I sent out a PR that addresses some of it. Namely, a combination of the latest code + my pull request changes got me in a state where it still rebooted Home Assistant, but it saved the configuration so going forward, it worked correctly (seemingly).
Got it working with a lot of rebooting in between.
One key trick that got it working for me that I picked up from the other Rainforest EMU-2 project that is floating around is to plug in the USB device AFTER it has rebooted. For whatever reason, if the device is plugged in when it reboots (or boots up), it doesn’t seem to behave right, but since it’s a USB cable, it’s easy enough to reset.
oh and sorry, I forgot to say THANK YoU!!
This was way easier to do than expected and that is because you did all the hard work for us, and then shared. 🙂
Glad you got this working! There seems to be lots of issues with the serial port being locked and such that seems to resolve itself with reboots 🙁
Ryan, I noticed this project has gone stale. I’ve got an EMU-2 here at home, and have some experience from writing a Java driver for it. Is there any way I can help out on the repo wiht merging in some PR’s (and to solve my particular issue add re-connect code?)