4050

Low-power sensors can use a MySensors network to integrate smoothly into Home Assistant. Here is how.

MySensors for home automation and IoT

Wi-Fi is great for quickly connecting devices to a network, but it is a bit power-hungry. It is therefore not the best solution for low-power sensor nodes that must run many years from a single button cell or that live from power harvesting.

A great solution for this type of device is MySensors, an open-source home automation and IoT project based on ISM-band radios, notably the nRF24 by Nordic Semiconductor and HopeRF’s RFM69. The more recent nRF5 platform as found on the BBC micro:bit can be used too.


 
 
MySensors uses mainly Arduino as microcontroller platform, and it builds and maintains a tree network all by itself. It integrates smoothly with Home Assistant but not as smoothly as ESPHome.

The project comes as an Arduino library included in the Arduino IDE’s library manager. After installing it, you can build your application on one of the examples. Often this only means changing the pin number(s) of the connected peripheral(s).

Build a MySensors Wi-Fi gateway

A gateway is required to build a MySensors network and you also need it to connect to other networks like a Wi-Fi network. For this I used an ESP8266-based NodeMCU because it exposes an SPI Port which we are going to need. Using an ESP32 module is another option. Actually, there are many options, but here we are going for Wi-Fi.

Connect the NodeMCU's SPI port to an nRF24L01+ module (see schematic in download section). It is recommended to also add an electrolytic capacitor together with a ceramic one (100◦nF or so) between the VCC and GND pins of the nRF24 module. That’s all the electronics you'll need.

On the software side a computer with the Arduino IDE installed is required. Add to the IDE the ESP8266 (or ESP32) core for Arduino and the MySensors library (‘Sketch’◦→ ‘Include Library’◦→ ‘Manage Libraries…’). Load the example ’GatewayESP8266’ (‘File’◦→ ‘Examples’◦→ ‘MySensors’) and enter the SSID and password for your network, and choose a static IP address.

Upload the sketch to your device, and your gateway is ready.

Please keep in mind that a gateway must always be powered and may never sleep. Sensor nodes can do whatever they want.

My first MySensors node

Creating a MySensors node is quite like building a gateway, except that now the ESP module is replaced by an Arduino-compatible board to which you add on or more sensors (temperature, hulidity, light, etc.) and/or actuators (LED, relay, etc.).

Connect an nRF24L01+ module to the Arduino’s SPI port, and add the capacitors mentioned above (see schematic in download section).

Load a sketch from the MySensors example library, preferably one that resembles what you are trying to achieve. There are more examples on the MySensors website. The sketch must be adapted to fit your peripheral(s) pin number(s), but basically that is all there is to do.

Upload the sketch to your device and let it boot; it will join the MySensors network automatically (if the gateway is on).

Integrating MySensors

The next step is to integrate MySensors into Home Assistant. You do this by adding a few lines to Home Assistant's configuration.yaml file.
 
mysensors:
  gateways:
  - device: '192.168.1.100'
    persistence_file: 'mysensors/mysensors.json'
    tcp_port: 5003
  optimistic: false
  persistence: true
  retain: true
  version: '2.0'

Here you must enter the same IP address as you used in the MySensors gateway sketch.

Choose also a name and location for the JSON file where Home Assistant can store MySensors network information.

Please keep in mind that every time the Home Assistant’s configuration.yaml file is modified, the system must be restarted for the changes to take effect. This can be done from the Supervisor’s ‘System’ tab.

Beware of the protocol version

At this point I ran into the problem that my device, a remote relay, did not show up in Home Assistant.
Eventually I discovered that this had something to do with the version of the MySensors API being used.
By inspecting the MySensors persistence JSON file (see above), I noticed that the gateway was listed as using API or protocol version 2.3.2. The webpage explaining the integration of MySensors into Home Assistant has an example intended for use with API versions 2 or higher. When I tried that example, my node appeared in the ‘Entities’ list on Home Assistant’s ‘Configuration’ menu and I could create UI cards for it.

According to this page, to make a V2-based node work in Home Assistant, it must send an initial value from the function loop(). The example sketches do not do this. However, delving deeper I came to suspect that an actuator node must request an initial value from Home Assistant in order to be recognised. (Maybe it must send one too, I don’t know.)

A sensor node is reported as soon as it starts sending data.

The request doesn't have to be made from loop(), but must be done during the boot process.

Special functions before() and presentation()

To distinguish V2 sketches from older types, look for the function presentation(). If the sketch has one, it is V2 or higher. However, the other way around (i.e. the absence of presentation() doesn't make it a v1.x sketch) is not true as presentation() may be left out. The present instructions usually executed in this function may not be omitted and must be moved to another function, for instance to setup().

MySensors sketches can also have a function before(). Both before() and presentation() are called before setup(), and in this order:
 
before();
presentation();
setup();
loop();
 

Related tutorials

More on Home Assistant and ESPHome in these projects and videos: