Integrating TheThingsNetwork and HomeAssistant: A Practical DIY IoT Example

Tim Evdokimov
5 min readJan 22, 2023

TheThingsNetwork (TTN) is a service that runs IoT solutions based on ultra-low-energy LoRa radio. HomeAssistant (HA) is a platform with thousands of community-supported integrations, to bring them all into a single system to facilitate monitoring and management.

I’m a long-term user and great fan of both projects, as they both offer free and open-source options, attracting vibrant communities of users that also contribute to the development. The cases I implement are centered around my house, and the case I’d like to tell you about isn’t an exception, either.

The heart of my home automation runs on HA — climate management, lights, window shades control and door/windows safety monitoring. With NodeRed low-code programming, it makes developing control heuristics quite easy.

As communication media, I use both ZWave and ZigBee networks, mostly by historical reasons, and both with its own pains. These networks are assuming to form a mesh of device nodes, using a controller (a USB stick with antenna) to connect to the NUC microserver where HomeAssistant application runs.

Alas, while the hardware vendors for ZWave and ZigBee generally succeed complying with the standards, still, combining them all together in a network when may they depend on each other — makes the whole thing less stable. One incompatible or buggy device misbehaviour disrupts the whole network.

Having to choose between these two, ZigBee is preferred to ZWave at any time.

TheThingsNetwork uses LoRa radio standard, covering much wider range (around 1 km from single antenna; no meshes, just gateways and the things) — it can be used not only inside a home, but e.g. in a rural area, a parcel of several hectares to support a high-tech agricultural process, environment monitoring, perimeter security etc. There is no replacement for HA platform in TTN ecosystem, or one-click integration for HA to integrate TTN, but the idea of combining TTN with all other devices in the same flows controlled by HA looked tempting.

Meow

My Chartreux named Escobar, already a seasoned IoT tester, 6 kilo of soft grey fur and amber eyes, roams outside and comes back whenever he wants. To enter the house without bothering anyone, the cat passes a tunnel with two hatches, to prevent draft and warmth loss from the house.

The tunnel with two hatches

There is an internal door between the entry hall and main hall, which is normally kept closed (entry hall is not warmed). But when Escobar is out, the door is kept slightly open so that he won’t scream from the entry hall.

Some problems experienced with this setup:

  • Sometimes, it is not clear whether he is back and somewhere in the house, or still outside.
  • Sometimes, the door is closed when Escobar is back, and he starts making devilish screams from the entry hall
  • Sometimes, the door is not closed after he’s back, and the precious warmth is getting lost.

My HA already uses Telegram API to notify everyone subscribed to our private house channel and adding notifications for key events around cat’s tunnel looked like an MVP solution to this problem.

And to motivate me even further, that looked like a case to test end-to-end integration TTN with HA. For that, I used LoRa-enabled IoT sensors kindly supplied by MerryIoT for evaluation — a door state sensor and a motion sensor.

MerryIoT sensor in the tunnel
MerryIoT door open/close sensor

Solution architecture, in a nutshell

When Escobar comes back, the cat triggers motion sensor installed in the tunnel - the event comes via TTN to HA, and the flow in HA will start. It will broadcast “cat is back” — and then check the door state. If the door is closed, the message will be “…and now please open door for him!”. If the door remains open 5 minutes after cat is supposedly back, the message will be “…now, please, close the door, the cat is back already”.

The journey:

  • Add devices to TTN
  • Configure payload formatters
  • Integration of TTN with HA
  • Implement flow in HA

Add LoRa devices to TTN

TTN did a great rework of their console to version 3 recently and it does support easy ways of adding devices “Scanning QR” or “Select device from repository”, but unfortunately these MerryIoT devices were not supported.

Adding device manually wasn’t too hard, however, using IDs supplied by the vendor (DevEUI, AppEUI, AppKey) and these pre-set parameters.

TTN console params

Payload formatters

This is a snippet that needs to be added at the application level (that is applies to all sensors together)

Payload decoder

Integration of TTN with HA

That part contained most unknown unknowns and took the most effort - trial and error, but in the end it works and runs for some weeks quite reliably.

Each application, configured in TTN, runs a unique MQTT server — in TTN Console, go under Integrations/MQTT.

HA runs its own local copy of MQTT broker (called Mosquitto), and it is used for Zigbee device integration. The solution was to add the external TTN MQTT and merge the traffic from external topics to the existing internal Mosquitto.

Configure HA Mosquitto to bridge topics from remote MQTT (the credentials taken from the application page in TTN console):

/config/mosquitto.conf

And then configure some of the MQTT topics to appear as sensors objects in HA:

/config/configuration.yaml

Implement flow in HA

NodeRed is a very handy low-code solution that lets you program just connecting the blocks with wires. The light blue blocks are the conditions, the dark blue is action, orange are messages and the purple are timers.

And the final result in Telegram.

Hope you’ve enjoyed reading this far!

Tim Evdokimov is a pragmatic engineer, with passion to real-time stateful event processing. Practical home automation and IoT is his long-time hobby and important source of fun.

--

--