Background / Sonoff:
Once upon a time I wanted the ability to know when a device plugged into an electrical socket was no longer using electricity. This lead me to get a "Sonoff POW 2" - a popular "smart" electrical switch with power monitoring capabilities. While this did what I needed it forced me to use their software (and have my plug talk to the cloud), and I wanted to do more.
I found out about Home Assistant which seems to the best way of managing all your smart devices. Personally I find it pretty complex and ugly... although powerful. Out of the box it didn't support my Sonoff device (or not over LAN I can't remember), but through the discovery of "HACS" - a kind of marketplace / plugin manager - and then AlexxIT's Sonoff integration for controlling the device over LAN.
After a lot of fighting I finally got things working, found and enabled Home Assistant's API and can now do what I set out to... poll my smart plug to know if my appliance is still consuming electricity or not. While Sonoff claims to have a "LAN Control" option, I'm pretty sure my Home Assistant setp only works via the cloud.
I've since bought few smart plugs of different brands and firmwares, and this blog post is some high level thoughts and lessons learned in adding them to Home Assistant.
Tasmota:
The smart plugs I bought, including the Sonoff, are all "ESP" chipset based and support an open source firmware called "Tasmota" which makes life a lot easier (eg: you can interact with the device directly over HTTP, no need for Home assistant).
Depending on the plug (or other smart device like smart light bulbs) you could swap the firmware with Tasmota using "Tuya Convert" - this process sounds a lot more complicated than it really is, I live booked a Kali Linux ISO in my macbook, plugged in a usb wifi adapater, and just followed the steps.
The main provider of most of these smart devices appears to be "Tuya" (hence Tuya Convert). Unfortunately they recently changed their encryption or firmware processes in newer devices preventing Tuya Convert from working. While efforts to get Tuya Convert working on the new devices was in progress, Tuya have also moved over to a Realtek chipset which Tasmota wont work on (currently).
The alternative (for the ESP chipset devices) is to open their casing - if you can, most of my smart plugs are glued shut - connect to the ESP's pins and use a USB adapter to flash new firmware to the device. Not something I wanted to mess with given these are devices I intend to plug into my house's electricity mains and leave unattended.
I have one smart plug running Tasmota, and I guess it will be my last.
Tasmota doesn't seem to have a (nice) HTTP API, but by watching how your browser interacts with the device's web UI it's easy to find useful URLS... thoug they may require some fiddly parsing or removal of HTML tags, eg: http://my.plug.address.local/?m=1
returns something:
{t}{s}Voltage{m}228 V{e}{s}Current{m}0.059 A{e}{s}Power{m}2 W{e}{s}Apparent Power{m}13 VA{e}{s}Reactive Power{m}13 VAr{e}{s}Power Factor{m}0.14{e}{s}Energy Today{m}0.010 kWh{e}{s}Energy Yesterday{m}0.001 kWh{e}{s}Energy Total{m}0.322 kWh{e}{t}ON
Not exactly JSON, but not too hard to extract data from. Tasmota also supports MQTT which is a nice, neutral, way of interacting with devices - but requires you run a MQTT server for it to talk to (easily done with Docker).
Apparently Tasmota measures power usage (my main use case in all of this) more accurarently than the stock firmware, but I found this not to be the case and instead had to calibrate the smart plug... I found this more difficult than I was hoping, and wish the instructions were more along the lines of:
"Using another smart plug, or a kill-a-watt, or guessing (eg: run a 60watt lamp off the plug), visit the following URLs in your browser adjusting the values as necessary:""
http://my.plug.address.local/cm?cmnd=VoltageSet+230
http://my.plug.address.local/cm?cmnd=PowerSet+60
Certainly a lot more straight forward, even if less accurate (or safe?) than the official documentation.
Tuya:
As mentioned above "Tuya" are the company that make, and white label, many smart devices. These devices typically talk to Tuya's servers and are accessible via the "Smart Life" app... all your smart devices... talking to the cloud... not ideal.
Unable to flash Tasmota to one of my newer smart plugs, I found out about "Local Tuya" which allows you, well Home Assistant, to talk to your devices over your LAN. Unfortunately the setup process is a little complex requiring you register on Tuya's developer protal, get API keys, and eventually get a "device id" and "local key".
I'm not going to cover the process here as it seems to change from time to time and is already covered in the Local Tuya repo, but thanks to "Local Tuya" these widely available devices actually integrate into Home Assistant fairly simply and easily. Add them to your Smart Life app (following the official setup process), get the device id + key from the Tuya API site (or using the "tuyapi" cli tool, and add it via the Local Tuya Home Assistant integration.
Home Assistant:
This is (meant to be) the brains of a unified home automation system of the process. I don't want to take any credit away from the amazing work the contributors to this project and related components have put in, but it falls so short from what I wish it was. Maybe I'm hoping for Iron Man's "JARVIS" system, or a more Apple like interface (I know there's Home Kit, just haven't found anything to integrate with it yet).
Home Assistant's main dashboard shows "cards" - basically widgets, showing data of the different components of your system. Mine has two columns, I'm not sure why it has two but depending what type of new card I add it sometimes makes a third column. I can rename things, and move them up and down in columns, but I can't move things between columns... I'm unsure if I'm incredibly dumb, too lazy to read the first page of the documentation, or if this system really wants to do things its way and I have to just go along for the ride like it or not. I have seen some really good dashboards online so the fault must be mine.
Moving away from how it looks (because I intend to just use its API), it makes use of "Integrations" to talk to different devices. Adding a Tuya device happens via Configuration -> Integrations -> Local Tuya
. As with the other integrations I've seen you're then asked to choose the type of device (is a smart plug with electricity monitoring a "switch" or a "sensor"?). Selecting "Switch" for my Tuya smart plug I was also asked which pins for it's voltage/current/etc... doesn't help I have no clue, but by putting load on the smart plug (and restarting this process, requiring I restart Home Assistant to avoid a "port already in use" error from the Tuya device detector) I found some values that seemed to line up.
Despite telling Home Assistant / Local Tuya about the voltage and current of the smart switch, Home Assistant insists on treating it as Switch, giving me only the ability to turn it off and on.
I'm still not sure how much should setup and config should happen in the Home Assistant UI (ideally "all of it"), but it seems to rely fairly heavily on manually editing a "config" yaml file.
In order to get HomeAssistant to know about my ("UltraLink" branded) smart plug's power monitoring abilities I had to add this to my config file, and could then add a "History Graph Card" etc to plot these values.
sensor:
- platform: template
sensors:
ultralink_plug_voltage:
value_template: '{{ state_attr("switch.ultralink_plug", "voltage") }}'
friendly_name: 'Voltage'
unit_of_measurement: 'Volts'
ultralink_plug_power:
value_template: '{{ state_attr("switch.ultralink_plug", "current") / 10 }}'
friendly_name: 'Power'
unit_of_measurement: 'Watts'
Note the "current" value was reported as x10 higher than it actually was, but could be modified here to report correctly. While installing MQTT support (integration) in Home Assistant was easy enough, I had to do something similar to the above in my config file mapping MQTT topics to Home Assistant... entities? variables? I'm not even sure anymore.
Enabling the Home Assistant REST API also required an entry in the config file:
api:
Yes really, that's it... 1 line with no vale. Why is this not a checkbox in the UI somewhere? (maybe it is and I just didn't find it, or it was added after I setup my Home Assistant install).
Conclusion:
It's called the "Internet of Shit" for a reason, and I hate all of this. I have too many official smart device apps on my phone (from initially setting up the few smart plugs I've bought), at least 3x different firmwares across the different plugs, and what feels like a bloated integration platform that requires me to know way more about the devices than I wanted to in order to just provide a "device status" JSON from an HTTP endpoint.
Presumably at some point I'll know what I'm doing and why Home Assistant works the way it does, allowing me to create an amazing dashboard I'll never look at and define clever "scenes" and automation I likely wont need (eg: turning on a lamp at sunset... but what about really dark and stormy days?).
I guess this turned into (another) "rant" post... but "smart" this process is not :(