[How To] integrate a Tasmota device in Home Assistant using MQTT

QIoT technology to allow you to explore potential IoT business opportunities.
Smart home/Smart building/Smart City/
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

[How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

[Edit] Updated on December 26-2020
There are many hardware devices available in the market capable of switching mains (110V/220V) on/off in a Smart Home in order to remotely control various appliances.
But, regretfully - the two most popular brands - Phillips Hue and IKEA Tradfri - does not offer a device like this.
(Well, to be quite honest, IKEA does have one, but so far only in a bundle with their own Zigbee wall switch)
Among the many brands to chose from, I haven't been able to find anything compatible with either Phillips or IKEA using the Zigbee standard.
But I have found several based on ZWave and WiFi.
Long story short: My final choice was the Chinese brand Sonoff. They have many low cost devices, and my favorite was the Sonoff Basic switch; https://www.itead.cc/wiki/Sonoff, but since this guide was first written, Sonoff has release many exiting new products, including also Zigbee based units. Check out their website. (P.S. I am not affiliated with Sonoff or Itead in any way)
The only disadvantage with this brand, is that this manufacturer - just as most others - tries to lock you in as a customer, to use only their products - and become a user in their Cloud offering.
But you can relatively easy break out of this straitjacket by replacing the firmware in the Sonoff device with the open source firmware named Tasmota. This firmware allows you to integrate the Sonoff switch into your own Smart Home platform, independently of the manufacturers limitations. There are lots of Youtube video's and tutorials on the internet on how to flash the Sonoff with the Tasmota firmware, but the objective of this guide is not to explain this. If you are interested in how to do it, the Tasmota Wiki on GitHub is a good place to start; https://github.com/arendst/Sonoff-Tasmota/wiki or read my guide here: https://www.reddit.com/r/homeassistant/ ... _with_the/

Remote control of the Sonoff switch with Tasmota firmware, is mainly done via MQTT.

If you are like me - a passionate fan of Internet of Things (IoT), you may already be running some kind of Smart Home server platform.
I use Home Assistant (HA) and I run it inside Container Station (CS). My objective was to get started with a very simple integration of a single Sonoff switch into HA.
I tried first usingt Belkin WeMo Emulation, which is supposed to be supported by Tasmota, but apparently it is only intended for use with Alexa. The same goes for the Phillips Hue Bridge emulation. So none of those options were usable.
That leaves you with MQTT - which can be very intimidating for a beginner like myself. Every article and How-To Guide I've found, assumes that you know a lot about Linux, command line, JSON and YAML in order to perform even the simplest task (just to mention a few).

So, here is how I did it;
I must make som assumptions/prerequisites - otherwise this guide would be a book, and note just a forum post.

1. I assume you already have Home Assistant v0.118.5 or later, running on your NAS - preferably in a Docker container in CS, and that you know where and how to edit your configuration.yaml file.
2. I assume you have a Sonoff Basic device ready flashed with Tasmota 9.2.0 firmware (or later).
3. I assume that you have been able to connect your Sonoff device to your WiFi (WLAN) network and found it's IP address.

First step; Install an MQTT broker in a new container on your NAS.
MQTT serves as a "middle man" sitting between your physical devices and your Home Assistant platform. MQTT is designed to interface with any IP connected device on your LAN/WLAN. It does not support other protocols like Zigbee or ZWave. It has become wide spread due to its flexibility and strong community support, but at the same time it has grown in complexity. But still, it is possible to get started in a very simple way.
MQTT is open source and can be found in many different flavors and versions. You install and run the MQTT broker as a server on your NAS just like any other software.

Image

In my system, I have chosen to install MQTT in a Docker Container in CS.
As you will find out, there are many docker images available to choose from. I chose to use the one from Eclipse named Mosquitto v1.6.8 which you will find on the Docker HUB.
I am not going through the basics of how to install a docker image in CS here - there are plenty guides around on how to do that, but the specifics for this particular image is here:

a) Create a folder somewhere on your NAS, i.e. under Public, and name it whatever you want. This will be the place where you store all your MQTT config, log and settings data - outside the container itself. It's not a good idea to store this inside the container, as it it will be lost if you ever have to reload a new image. Keep in mind that QNAP use the term Shared Folder in several ways. Using File Manager, Shared Folder means a folder which is shared on your network, but when installing a container, Shared Folder means a folder which is shared between your NAS and CS.
b) Inside this new folder you just created, you need to add three other folders and name them config, data and log and finally, inside the config folder, you should create a dummy text file named mosquitto.conf. Remember that everything from now on is case sensitive!
c) Inside CS search and find the Mosquitto MQTT v.2.0 image (or later) on Docker-Hub and install it.
d) During installation, go into Advanced Settings in CS
e) Select Network Mode = Host
f) Define the following three Shared Folders: Volume from host
- browse to the config folder you created in step b). Mount point - should be /mosquitto/config
- browse to the data folder you created in step b). Mount point - should be /mosquitto/data
- browse to the log folder you created in step b). Mount point - should be /mosquitto/log

That's all. Your MQTT container should automatically start after installation. Just now, nothing more will happen. MQTT doesn't have a web front end that you can visit.

Second step; configure your Sonoff device to talk to the MQTT broker.
The Tasmota firmware in your Sonoff must be told (configured) how to find and talk to the MQTT broker. This is how:

i) Direct your browser to the IP address of your Sonoff device, that you found in step 3) above and you should see the opening Webpage.
ii) Click on Configuration and then on Configure MQTT
iii) In the following table, insert from the top: The IP address of your NAS. Set port number = 1883, and in the Topic field, type a name of your choice (avoid using spaces, hyphens and other special characters). Leave all other fields as they are with default values.
iv) You should go into the Configure Other section and add your own Device Name as well as Friendly Name. These names are automatically read by HA and it makes it easy to recognize devices end entities which otherwise are just given names using random letters and digits. Remember to use only lower-case alphanumeric characters in the Device Name. Avoid spaces and special characters. For Freiendly Name however, you can type anything.
v) Click Save and the Sonoff should restart automatically.
vi) Now you should be able to see in the Console windows in CS on your NAS that the Sonoff is connecting (you may have to restart MQTT). It should look something similar to this:

Code: Select all

1568892265: New connection from 192.168.0.21 on port 1883.                                                                                               
1568892265: New client connected from 192.168.0.21 as DVES_7CAC60 (p2, c1, k30, u'DVES_USER').
Third step; Configure Home Assistant to detect and talk to your Sonoff through the MQTT broker.
There are basically two ways to set up integrations in Home Assistant; One is to manually define it in the configuration.yaml file, and the other is to use the Lovelace front end and add it using the Integration option under the configuration menu. I tried both, and it's up to you which method you prefer. Using integration and automatic discovery is definitely easier. I will explain both methods below.
If you have tried the integration method, and for some reason it failed, remember to delete the configuration before you move on to the manual method. It's important to remember that in HA, the automatic integration takes precedence over manual entries in the configuration.yaml file. You cannot use both.

Integration and auto-discovery [IA]
IA1) Open up the web deamon in your Tasmotized Sonoff switch by typing its IP address into your favorite web browser, typically ; http://192.168.0.XX
Once you get to the main menu, click on the button that says Console and the console window should open up. Here you can watch all Sonoff events as they occur in real time in the upper part, and below is a command line where you can type in any and all commands supported by the Tasmota Firmware, including those not available in the GUI.
On the command line, type the following: (this is also described in the HA Tasmota Integration doc : https://www.home-assistant.io/integrations/tasmota)

Code: Select all

setoption19 0
Then watch as the firmware automatically reconfigures itself for discovery and then restarts the Sonoff.
If everything goes well, your Sonoff should come back online and you can return to the main menu.
Then, configure the Sonoff to report itself to HA as a switch (or as a light) using:

Code: Select all

setoption30 0
(Switch)

IA2) Now, open your mosquitto.conf file that you created in step f) above. This file should now have been populated by your MQTT broker with all the various settings that is supported by MQTT. If the file is still empty, it's a sign that it was not found during installation of MQTT in Container Station (CS). Most likely becasue you typed something wrong when configuring Mount Point in the Shared Folder section. If so, you have to go back and reinstall MQTT again as these settings can only be changed during installation. Another way to check if the container has found your config file, is to open a terminal window inside the container and use regular Linux command line options to locate the folder which should be inside /mosquitto/config/
Inside the mosquitto.conf file, you must make at least two changes. Starting with the release of Mosquitto version 2.0.0 the default config will only bind to localhost as a move to a more secure default posture. You have to change this setting to let HA bind to your MQTT server (Mosquitto). Search for bind_address and remove the comment tag (#) and insert the IP address of the server where your MQTT server is running, i.e. bind_address 192.168.0.X
Then, you need to allow anonymous connections (unless you wish to add user names and passwords for clients (Sonoff devices) to connect. Search for allow_anonymous and set it to allow_anonymous true. You may want to change this later to obtain some minimal security.
After making changes to the mosquitto.conf file, remember to save your changes and restart Mosquitto to include the changes.

Now go to you Home Assistant web GUI.
Assuming that you haven't entered anything in your configuration.yaml file related to a Sonoff switch (If you have, then clean your configuration file before you continue), and choose configuration > integration and then click on the big + icon in the lower right corner of the GUI and select the MQTT integration. Add the IP of the NAS where your MQTT server is running. Leave the port number on its default 1883 and leave user name and password blank (so much for security, ha, ha :)
Your MQTT integration should connect to your Mosquitto server successfully. If it fails, then you have made some mistakes - probably typos.

Lastly, you need to install the Tasmota(beta) Integration in Home Assistant. This integration picks up all sensors from your Sonoff devices and exposes them to Home Assistant. It may take some time 10-30 seconds for each device to be picked up. You should see 1 device and up to 9 items (sensors) for each Sonoff. When you view the entities found by the Tasmota Integration, you'll see that most of them are "unavailable". Just select the ones you wish to use, and enable them from the menu in HA. It takes a while before they become available, so just be patient.

Now jump to HA3) below.

Manual setup in the config file
Only perform these instructions if you decided not to use the Integration and auto-discovery above. You cannot do both.
HA1) Open up your configuration.yaml file, where ever you might have stored it, and add the following section anywhere suitable in that file:

Code: Select all

#Mosquitto MQTT 
mqtt:
  broker: NAS-IP

#Sonoff Switch
switch:
  - platform: mqtt
    name: name_of _your_switch
    state_topic: "stat/name_of _your_switch/RESULT"
    value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/name_of _your_switch/POWER"
Make sure you don't already have a section named switch in this file. (You cannot have more then one instance of each section)
The name_of_your_switch must be replaced with the name you gave your Sonoff switch iii) above.

HA2) Save the configuration.yaml file and in your Home Assistant under Configuration - Server Control, click CHECK CONFIG and then if it's ok, restart the server under Server Management. You should never restart Home Assistant directly from CS unless something is wrong and it is no longer responding (hanging).

HA3) Once HA is again up'n'running, you should be able to see your newly achieved accomplishment in the Lovelace Overview page by clicking the three dots in the upper right corner and selecting Unused Entities. You should also be able to find under States in the Developer Tools section. Look for the keyword Switch.

If you've successfully got this far, you can now test the functionality by toggling the switch in one of three possible ways:

* Directly using the button on the switch itself
* Through the Toggle button in the Tasmota Web interface
* Or by clicking the horisontal slider in the HA Switch entity

No matter which one you choose - they should follow one another.
Well done!
Your next step will be to learn advanced MQTT configuration and scripting, create HA Automation scripts which includes the Sonoff switch, and add security according to your own requirements.

Now run along and buy a lot of Sonoff cheap quality devices (I am not sponsored by anyone - I just like cheap stuff that works) :lol:
Last edited by oyvindo on Sat Dec 26, 2020 10:47 pm, edited 11 times in total.
ImageImageImage
Rotjong
New here
Posts: 2
Joined: Wed Nov 30, 2011 7:43 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by Rotjong »

Nice !! This one helped a lot !!

I was struggeling for month now to find a tutorial like this.

I used the flashing tutorial on https://github.com/arendst/Sonoff-Tasmota/wiki/Flashing.

With this turorial I could add a couple of devices to the home assistant.

Thanx !!
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

I'm glad to hear that :P
Thanks for the feedback.
ImageImageImage
mikemor22
First post
Posts: 1
Joined: Sat Oct 22, 2016 11:30 am

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by mikemor22 »

I’m pretty late to the HA party - but this guide has answered so many questions for me. Many, many thanks πŸ™‚
NAS:
QNAP TS-453Be 16Gb
4x4Tb RAID5 + HS
QTS 4.4.1
Running Plex, Resilio, UniFi controller, ITunes Server

QNAP TS-251+ with 8Gb
2x2Tb RAID 1
QTS 4.4.1
OpenVPN
Docker installs of Home Assistant and Eclipse MQTT
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

Better late than never :)
You are most welcome!
ImageImageImage
boubi
Starting out
Posts: 25
Joined: Tue Oct 20, 2020 8:19 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by boubi »

Hi,

I tried to read a lot lately about HA.
I have a Qnap ts-128A with an ARM processor.
I found a suitable HA for it in the docker hub but later when I read about mqtt I realized that I need to perform operations that do not exist in my HA.
For example, adding a broker addon.
I saw that I do not have access to the addon because hass.io is not enabled.
From what I read, I need to use HA supervisor.
The HA currently working for me is in this version (ARM), I downloaded the supervisor in the same processor version but it does not work for me.

Your guide is excellent and explains excellently, it can be a wonderful solution to implement the HA and broker in two different containers.
Anyway, do you know a supervisor container that will work for me?
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

Hi, Supervisor mode is only available with hass.io
Hass.io is a container OS + HA bundle designed primarily to run on a Raspberry Pi. It has several nice features built in. Since all of this is open source, I know some people have made available versions of hass.io that you can run on other platforms, but I'm not aware if it will run on ARM.
It may be risky to go for a non-supported branch of hass.io, but if you are knowledgeable, it's your call.
Also, an ARM based NAS is really just for storage purpose. If you wish to extend you NAS to become an App server, you should opt for an Intel based version.
Nothing of what I have described in my guide require the use of hass.io or its Supervisor mode. I run HA Core in Container Station.
ImageImageImage
boubi
Starting out
Posts: 25
Joined: Tue Oct 20, 2020 8:19 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by boubi »

oyvindo wrote: ↑Tue Nov 17, 2020 4:29 am Also, an ARM based NAS is really just for storage purpose. If you wish to extend you NAS to become an App server, you should opt for an Intel based version.
Nothing of what I have described in my guide require the use of hass.io or its Supervisor mode. I run HA Core in Container Station.
Thank you.
My goal is to see all the functionality of the device and of course buy one that better suits my needs.
I will give this device to my brother for remote backup.

Today I installed according to your guide.
I have 2 containers (HA & mosquitto) and the HA found the MQTT with its IP.

I only have a problem with the config file.
It always stays in the container and not in the folder I created in NAS.
Anyway I copied it myself out to the folder I created and will try to resolve it.

When create a new container there is a command:
/usr/sbin/mosquitto -c /mosquitto/config/mosquitto.conf
so if I try to edit it to /config the creation failed because it can't find the config file
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

Are you using docker compose script/command to install containers, or do you use the CS UI (Container Station graphical User Interface) ?
ImageImageImage
boubi
Starting out
Posts: 25
Joined: Tue Oct 20, 2020 8:19 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by boubi »

oyvindo wrote: ↑Tue Nov 17, 2020 5:21 pm Are you using docker compose script/command to install containers, or do you use the CS UI (Container Station graphical User Interface) ?
CS UI

In addition, I intend to use the shelly module.
I saw videos that also need to add an addon from the HACS store that does not appear in my version.
Do you know by chance if it needs another container?
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

Ok, did you add your shared Folder path to Volume From Host and the config path to Mount Point as described ?
ImageImageImage
boubi
Starting out
Posts: 25
Joined: Tue Oct 20, 2020 8:19 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by boubi »

Yes, I add all the shared folders as describe.
Just, the config not work.
I add it here, and this is what happen when running:
https://imgur.com/a/nugy2Fk
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

In your screenshot, there's no data filled in for Volume From Host and Mount Point.
Did you actually enter anything there (after clicking on the Add button that you have circled?
ImageImageImage
boubi
Starting out
Posts: 25
Joined: Tue Oct 20, 2020 8:19 pm

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by boubi »

Yes, sure.
I click ADD and mount the created nas folder (MQTT/Data & MQTT/Log) to /mosquitto/data & /mosquitto/data
When I try to mount to /data & /log it didn't work.
User avatar
oyvindo
Experience counts
Posts: 1399
Joined: Tue May 19, 2009 2:08 am
Location: Norway, Oslo

Re: [How To] integrate a Tasmota device in Home Assistant using MQTT

Post by oyvindo »

Your not supposed to do that.
I suspect you are mixing things up.
Seems like you are entering data belonging to the two upper fields.
You are not supposed to enter any path information in the New Volume group (upper section) nor in the Volume from container group (lower section).
You should only fill in the path info by creating a single row in the Volume for Host group (middle section).
ImageImageImage
Post Reply

Return to β€œQIoT”