Friday, March 1, 2019

Home Assistant MQTT discovery with ESPEasy


I bought a bunch of these EcoPlug wifi plugs from Home Depot a year ago at a good discount. I think it was between CAD$10-15 each. I saw them on the clearance shelves and could not resist. To use them as wifi plugs, I needed to use their own smart phone app (of course). I didn’t like that idea and did a google search on “ecoplug hack” and luckily ended up with two excellent resources - The Great Geekery and John’s Musings.

Most of what I have done with these is a direct result of all the research done by people from the above blogs.

I chose to flash the ESP8266 inside with ESPEasy as I had already been playing a little with that on my Wemos D1 minis and really liked it. My plan is to flash all of them eventually - I bought a dozen of them :). For now I have only flashed 4 of them.

I also installed a DS18b20 based temperature sensor in each one of them. I will write about that in a future post when I get some time.

Once I had ESPEasy running on them I wanted to control these from my Hass. I started out by creating individual configuration for them in my Hass config. I quickly realized that it was a lot of repetitive configuration that I had to put in and it was also hard to keep track of which configuration was for which device. After another quick google search, I came across the MQTT discovery feature in Home Assistant and I was thrilled, this was exactly what I wanted (and was surprised that I didn't know of this already).

ESPEasy has a "Rules" feature where one can write simple scripts which fire at certain events. For example I can have a bunch of actions fired when ESPEasy connects to my MQTT broker. That should work great for MQTT discovery, the plugin will announce itself when it has connected to the MQTT broker and send its own configuration details to my Hass instance!

My requirements were quite simple, I needed the following configuration for each plug:
  1. The switch that controls the plug itself, so any appliance that is plugged-in can be controlled.
  2. A sensor for the DS18b20 temperature sensor
  3. Availability of the device
  4. The IP address of the plug/ESP8266. This is optional, but I found it very useful for knowing which IP to use for accessing a particular plug’s (ESPEasy’s) web UI. You may or may not need this feature, depending on your network and DNS setup. I do not think ESPEasy has mDNS yet.
It took a couple of hours of trial and error to get it right, but I came up with a template for ESPEasy rules for the above purpose, that I could pretty much copy-paste to all devices.




The first command on line 2 creates the switch entity, which represents the onboard relay. The name is created from the %sysname% ESPEasy variable that is my ESPEasy “Unit Name” (see below).


This is how the MQTT controller is configured.



All devices can be configured with the same parameters for the controller. The only differentiating factor is the name of the unit, represented by "%sysname%".
The relay is controlled by gpio15 of the ESP8266 on the EcoPlug.
The second command (line 3) creates a sensor for the IP address of the unit.
The third command (line 4) creates the DS18b20 temperature sensor. I have configured it in the “Devices” tab, connected to gpio12.



The next two commands (lines 5 and 6) publish the online status and the current IP address of the unit to the availability topic and the IP address sensor configured above, respectively.

Here is how they look on my Hass UI:


For now, the IP addresses are not added to a group or view, so they just show up at the top.


Eventually, I will move to using the Lovelace UI and I think the custom monster card will allow me to group entities based on their names (like *IP*).
This setup has worked really well for me in the last couple of months I have had it. My plan is to install/plug one (or more if needed) of these plugs in each room of my house. I can use the temperature readings from each room to control the heating.
Note: The MQTT beeper in the list of switches above is also a EcoPlug in which I installed a piezo buzzer, that I use as an audio indicator for garage and front door opening/closing. I will write about that project in a future post.