Connected objects developed using the MySensors library are very well supported by Home Assistant. In this tutorial, we will see how to integrate MySensors projects developed using version 2.x of the library. You can connect directly to a MySensors Gateway or an MQTT gateway. We will see how to do with the first solution. This tutorial works whatever platform is used to run the home automation server (Windows, MacOS, Linux and Raspberry).
If you’re new to Arduino-based or ESP8266-based object development using the MySensors library, here are some articles to get you started:
- Use Node-RED to create a gateway between MySensors and Blynk (local server)
- Node-RED (Tutorial): saving MySensors measurements on InfluxDB
- ser2net: Serial USB Gateway to Ethernet. Test on RFLink with Domoticz
- MySensors v2: measurement of temperature and humidity (DHT11/DHT22)
- #Guide, how to increase the range of nRF24L01 radio modules of MySensors v2 projects (hardware, configuration)
- MySensors v2: decode/send messages with Node-RED
- Node-RED: How to integrate a connected object developed with MySensors v2 library
- Homebridge – Domoticz: control MySensors objects with Siri on iOS10
- How to make a MySensors v2.0 WiFi Gateway with an ESP8266. Test with Domoticz
- MySensors v2 – Discover, news, migrate old sketchs (1.4 or 1.5)
For this article, I used the temperature and humidity probe presented in this tutorial. It is about 50m from the gateway. It’s not very far, but there is a lot of vegetation. The gateway presented here is based on an ESP8266 (Wemos d1 mini). To increase reception range, I opted for a 6dBi antenna. The cable is long enough to fix it to the outside. To optimize transmission range, read this tutorial.
Add MySensors objects to the Home configurations
Since the MySensors library is natively supported, there is nothing to install. Integration will only consist of declaring the gateway and integrating the objects into the groups.
Open the configuration.yaml file. If you installed Home-Assistant in a virtual environment (recommended), run this command
sudo nano /home/homeassistant/.homeassistant/configuration.yaml
Otherwise, it is in your user folder
sudo nano /home/UTILISATEUR/.homeassistant/configuration.yaml
The documentation presents all possible configuration types (all details here)
mysensors: gateways: - device: '/dev/ttyUSB0' persistence_file: 'path/mysensors.json' baud_rate: 38400 - device: '/dev/ttyACM0' persistence_file: 'path/mysensors2.json' baud_rate: 115200 - device: '192.168.1.18' persistence_file: 'path/mysensors3.json' tcp_port: 5003 - device: mqtt persistence_file: 'path/mysensors4.json' topic_in_prefix: 'mygateway1-out' topic_out_prefix: 'mygateway1-in' optimistic: false persistence: true retain: true version: 2.0
You can connect directly to a MySensors connected to the USB port (first 2 devices), a standard Gateway and finally an MQTT gateway.
The MySensors component offers several additional options regardless of which gateway you choose:
- Optimistic (optional, false by default). Use this option for devices that do not return the output status. For example a switch. It is considered that it has changed state when pressed.
- Persistence (optional): Enables or disables the recording of data in a local JSON file. It is not advisable to disable this option. If the persistence is disabled, the sensors must present themselves (see this tutorial for more information on the presentation) whenever sending data. That is not very practical.
- Retain (optional): option for the MQTT gateway. Enables or disables the option to retain messages by the MQTT broker.
- Version (optional): version of the library used. The MySensors API has evolved significantly since version 2.0 (technical documentation here). It is better to indicate the version used and if possible to standardize your code.
Here we will configure a TCP / IP gateway. It has the (fixed) IP address 192.168.1.20 on the LAN. By default, the communication port is port 5003.
mysensors: gateways: device: '192.168.1.20' persistence_file: '/home/homeassistant/.homeassistant/mysensors.json' tcp_port: 5003 optimistic: false persistence: true retain: true version: 2.0
During the debugging, it is preferable to activate the recording of messages by the integrated logger. Create a new logger section. It is possible to retrieve the messages from the components as well as the messages exchanged with the gateway.
logger: default: error logs: mysensors: debug homeassistant.components.mysensors: debug
The logger accepts several levels of severity. The debug mode is suitable in most cases:
Restart Home Assistant. I recommend you restart the service from the interface by going to the Configuration panel for example or by executing this command.
sudo systemctl restart home-assistant@homeassistant
Follow the launch to verify that HASS is properly connected to the gateway with this command
sudo journalctl -fu home-assistant@homeassistant
Configure the group
Now that HASS can connect to the gateway, all you have to do is add the devices to an existing group (or a new group). You should have passed the device IDs by following the service execution log. Fortunately, it is very easy to find the device name directly from the web interface.
Open the menu and click the <> icon to open the states panel. The devices start with the prefix sensor
To add the sensors to an existing group, simply add it to the list of entities. Here is an example for moisture sensors.
humidity: name: Sondes d'humidité entities: - sensor.humidity_158d000156cbfa - sensor.box_little_3_1
Change view (customize.yaml file)
As for the previous tutorial, I advise you to separate the configuration in separate files for easier maintenance.
Open the customize.yaml file. For each sensor, create a label and add the friendly_name option to define the label that will be displayed on the screen.
sensor.objet_1_0: friendly_name: Temperature Extérieure sensor.objet_1_1: friendly_name: Humidité Extérieure
Save and restart HASS
So, all MySensors objects have been properly integrated into the interface. You can also enjoy the measurement history.
In the next tutorial we will see how to include radio devices using the Open Source RFLink Gateway.
- Home Assistant. Get started on Raspberry Pi 4. Procedure 2020
- ESP8266 + DHT22 + MQTT: make a connected object IoT and include it in Home Assistant
- How to Include RFLink Radio Home Automation Devices at Home Assistant (HASS)
- Include ESP8266 + DHT22 + Homie MQTT IoT in Home Assistant (HASS)
- How to include MySensors v2 IoT objects in Home Assistant (HASS)
- #Test: installing and Including the Xiaomi Smart Home Kit on Home Assistant (HASS)