MQTT or Modbus for my "VanAssistant"?
By Rob Litjens
The VanAssistant project is not going that well. Not to much related to technical issues, but more or less relative based. I will not dive deeper into this, but we had some strange weeks going on.
When starting the project VanAssistant I wanted to have everything running over MQTT. It is the reason to change my router to the Teltonika.
I must admit, it was heavy to get things working. But i managed being able to communicate with the router… But not how it should be…
Is making MQTT working difficult? Not in the end of course, but it started very difficult. I used various sources to find out what i needed to get MQTT working from the Teltonika RUTX50 towards home assistant. Below I describe how to enable MQTT on the Teltonika and how to setup Home Assistant for MQTT.
Configure MQTT on Home Assistant
The configuration exists out of multiple steps. I like security, so I started with creating a user account, then the Mosquitto Broker add on and next to that I configured the MQTT integration
Setup the User
First of all I created a user in home Assistant that has no additional permissions, so that you do not grant to much permisssions ot your systems. This is the configuration in used.
Install the Mosquitto Add On
Then I installed the Mosquitto MQTT option in Home assistant. For that I went to Settings -=> Add Ons and added Mosquitto Broker to the Configuration. I installed and enabled it on boot and enabled the watchdog.
When the installation is done, you get a MQTT integration under “Devices and Integrations”.
Configure the MQTT integration
To make sure you run the MQTT integration under the created user, you should go to “Devices and Services”, then you can click on the MQTT integration. When done, you need to press the “Configure” link.
On top, you will find the option “Re-configure MQTT”
At this spot, it is time to change some settings:
I think I only changed the Username and the password.
At this moment, MQTT is configured on the Home Assistant side.
MQTT-Explorer
Because this whole experience with MQTT is new to me, but very interesting, I also decided to download MQTT explorer (portable package) and configure it to see what happens on the Mosquitto instance. I read about it on some blogs, that it helped in identifying issues on MQTT. Therefore it might be interesting to show the configuration side of MQTT explorer. I will not discuss the working of it, because the site of MQTT explorer is sufficient in that.
I have marked the settings that are importent: - IP address of your Home Assistant server - Port of where Mosquitto is running on (1883 is default) - User and its passwords
Configuring the Teltonika RUTX50 router
You can setup MQTT in various ways on the Teltonika Routers. I opted to go with only setting up the Publisher. Reason is that i do not need to have previous information. I am only interested in current information. This means I am currently not using the MQTT Broker in the RUTX50 Router.
So how does my config look like?
As you see I filled in the IP address of my Home Assistant server, Port number, the Client ID (which you can get from the MQTT integration configuration page), username and password. All these values are allready known. Probably the most important items on this page are the ClientID and for sure the Publish Topic Prefix which points to the serial of your router. You can find this page Status -=> System in your router.
At this moment I have no idea if I need to add something to the Subscribe part. Therefore leaving empty will be the best option.
When pressed Save and Apply technically your installtion will work. (it did with me)
Testing and Trouble shooting options
There are various ways on how to test if your MQTT configuration is working. I normally start with logging. So that is what I did, Wen logging is examined, i would know if there are topices available in MQTT.
Below I have written how you can investigate the configuration
Mosquitto Log
You find this one under “Settings” -=> “System” -=> “logs” In the Right top there is an option to select a log. If you click you will find your Mosquitto Log:
Logs Will look like the picture below. You will see my router logging in, but also the internal connections from Mosquitto or HomeAssistant
MQTT Integration
When you have seen the login messages I think it is a good point in time to start looking in HomeAssistant to find out if something exciting is happening. Therefore go to the MQTT integration and select configure. I have choosen to take the temparature, but i also tested with Signal. Both are working excellent.
If you want to identify all topics, please look at the link to “Monitoring via MQTT” on the teltonika website
MQTT Explorer
If you have configured as I described above, you will see some items in MQTT Explorer also.
Can we use MQTT in Home Assistant?
Unfortunately I have tried a ton of possibilities to get a MQTT data from the RUTX50 straight into Home Assistant. Working is only the sample as shown above. The router does not send messages automatically.
I setup the RUTX50 as a broker, tried Data to Server options to push data, installed Mosquitto publisher and subscriber on the RUTX50 but nothing is working as I would expect.
So how could it work?
It looks like the Router only publishes a message when you make a request. Unfortunatley this means, in my humble opinion, that I should make a script that requests those messages and that it should save them in the Mosquitto Broker. That means I need to create a HA module for it. At this moment, my time is limited for this solution. If you have another solution, please contact me. A DM on LI or bksky is sufficient :)
Switching to ModBUS
The cool thing about searching the internet about these topics is that you find a million of other solutions. One of them is the ModBUS implementation. I looked at the page of Hannes Schleeh and implemented it the way he descrided. That is easy.
In your router go to ModBUS and then to “ModBUS TCP Server”. Toggle the Enable to On and you are done. I think I do not need an image for that. If you want an image, go to my github repository VanAssistant
The repository also describes how you need to install it. It is fairly simple:
1) Create a folder in your homeassistant folder named scripts
2) Copy the file teltonika-modbus.yaml to the scripts folder
3) Add a line to your configuration.yaml (the line is in the configuration.yaml of my repository)
4) If you want to see my dashboard you can import the dashboard.yaml in a new dashboard page.
Then you are done. If you skip step 4, you need to create something yourself.
Ideas?
If you have any ideas on how to make MQTT work well or add features like disable WiFi, then I would be happy to hear from you.
The next blog will be on installing the Zigbee adapter and set it up using MQTT and at least one quick device.
Sources of information
Tools: Github repository MQTT Explorer
Home assitant blogs topics: Home Assistant over MQTT RUTX50 ModBus and Homeassistant
Teltonika wiki pages: Teltonika Wiki Pages