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.

10 comments:

  1. Hi, great article, but not resolve my problem with espeasy and momeassistant auto discovery

    MQTT message
    {"~": "homeassistant/biuro-swiatlo","name":"biuro-swiatlo plug","dev_cla":"switch","cmd_t":"~/gpio/12","stat_t":"~/button/state","avty_t":"~/available","pl_on": "1","pl_off": "0","ret": "true","pl_avail":"online","pl_not_avail":"offline"}

    in configuration file i have declared autodiscovery prefix and enable feature ...

    ReplyDelete
    Replies
    1. The payload looks fine. Which topic are you publishing this message to? So your switch does not show up in HA? Are there any related log messages?

      Delete
  2. Hi, I'm trying according to your instructions but I'm not getting well :(


    Logger: homeassistant.util.logging
    Source: util/logging.py:108
    First occurred: 19:56:47 (16 occurrences)
    Last logged: 19:58:16

    Exception in async_discover_sensor when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'name': 'ESP_Easy_Dilna IP', 'state_topic': 'homeassistant/ESP_Easy_Dilna/ip', 'availability_topic': 'homeassistant/ESP_Easy_Dilna/available', 'retain': 'true', 'payload_available': 'online', 'payload_not_available': 'offline', 'platform': 'mqtt'},) Traceback (most recent call last): File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/mqtt/sensor.py", line 83, in async_discover_sensor config = PLATFORM_SCHEMA(discovery_payload) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: extra keys not allowed @ data['retain']
    Exception in async_discover_sensor when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'name': 'ESP_Easy_Dilna Temp', 'device_class': 'temperature', 'state_topic': 'homeassistant/ESP_Easy_Dilna/Temp/Temperature', 'availability_topic': 'homeassistant/ESP_Easy_Dilna/available', 'retain': 'true', 'payload_available': 'online', 'payload_not_available': 'offline', 'platform': 'mqtt'},) Traceback (most recent call last): File "/srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/mqtt/sensor.py", line 83, in async_discover_sensor config = PLATFORM_SCHEMA(discovery_payload) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/srv/homeassistant/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping .........

    ReplyDelete
    Replies
    1. Can you share the content of rules from your ESPEasy config?

      Delete
    2. I had made a couple of changes in my configuration, but had not updated the gist. I have updated it now. The changes are:

      1. removed "dev_cla"
      2. removed "ret"

      Delete
  3. Thank you for seeing it. I have a DHT22 sensor data to send set in controllers?

    Controller Publish:

    ReplyDelete
  4. Can you share your rules as a txt file?

    ReplyDelete
    Replies
    1. Hope this helps - https://gist.github.com/atomicsamurai/4bcb03cb3188b34bc9afef395f1adfd5

      Delete