Do you remember when you could run Zigbee2mqtt (Z2M) on a Raspberry Pi Zero without encountering memory issues? Or would you just like to run your home automation/Zigbee software without using up 4 GB+ of RAM? I know I do.
I personally love Zigbee2mqtt and the ability to run devices from multiple vendors. However, most of the time it does very little work yet still consumes 120 MB, which makes it difficult to fit into even 1 GB. Home Assistant (HA) now recommends a strict minimum of 2 GB. With memory shortages and prices up by 50–200%, experimenting with Zigbee is becoming increasingly expensive.
Z2M is written in TypeScript, which I personally like, but I don’t believe that one programming language is the best fit for everything. Our home automation usually runs on embedded devices with limited resources, such as the Raspberry Pi, and we simply want to make the most of them. All this has led me on a journey to create a drop-in replacement for Z2M that has all its features, such as dynamic device definitions, MQTT integration and HA integration, while keeping memory consumption as low as possible.
When searching for the optimal language in which to write the improved Z2M, I considered Golang and Rust, as they are modern languages with great tooling and a small memory footprint. I personally find Go quite boring due to the limited expressiveness of the type system. This is a great feature for many projects, but I prefer the functional style of programming in Rust along with maximum memory control. Another decisive factor is the ability to write dynamic device definitions and converters. Although both Go and Rust are compiled languages, I needed this to be dynamic. One option would be to use Lua, which has a completely different syntax to Go or Rust. I would also have to bundle the Lua interpreter and perform inter-language communication, which is far too complicated for my liking. In the past, I have successfully used an interesting Rust crate called Rhai, an embedded scripting language. If you’re not familiar with it, I recommend checking it out. It’s a fantastic way to have a scripting language with Rust’s native performance (you can define functions in Rust and make them available in Rhai).
All of the above led me to the final decision to write it in Rust and create Zigbee2mqtt-rs. What does the final device definition look like? See below:
// New definition Rhai script
device(#{
fingerprint: [#{ model_id: "EM", manufacturer_name: "Shelly" }],
model: "S4EM-002CXCEU",
vendor: "Shelly",
description: "EM Gen4",
extend: [
device_endpoints(#{ endpoints: [["1", 1], ["2", 2], ["3", 3]] }),
on_off(#{ power_on_behavior: false, endpoint_names: ["1"] }),
electricity_meter(#{
endpoint_names: ["2", "3"],
produced_energy: true,
ac_frequency: true
}),
force_power_source("Mains (single phase)"),
shelly::custom_clusters(),
shelly::wifi_setup(),
identify(),
],
});
// Original Zigbee2mqtt in JavaSacript
{
fingerprint: [{modelID: "EM", manufacturerName: "Shelly"}],
model: "S4EM-002CXCEU",
vendor: "Shelly",
description: "EM Gen4",
extend: [
m.deviceEndpoints({endpoints: {"1": 1, "2": 2, "3": 3}}),
m.onOff({powerOnBehavior: false, endpointNames: ["1"]}),
m.electricityMeter({
endpointNames: ["2", "3"],
producedEnergy: true,
acFrequency: true,
}),
m.forcePowerSource({powerSource: "Mains (single phase)"}),
...shellyModernExtend.shellyCustomClusters(),
shellyModernExtend.shellyWiFiSetup(),
m.identify(),
],
}
As you can see, the definitions are almost identical. All the functions are defined in native Rust, enabling maximum performance. For new devices, the logic can be defined directly in the Rhai script and then ported to native Rust if it is to be shared by other definitions in order to improve performance. The Rhai script is optimised and has sufficient performance for this task, even though it does not have a JIT compiler.
What about memory usage? This has been measured in a simulation since I don’t have that many devices. As you can see below, the memory usage stays below 13 MB, even with 100 devices. Compared to the original, which would consume around 200 MB for this number of devices, it uses 15 times less memory.
Press enter or click to view image in full size
Z2M is widely used due to its wide range of supported devices, which currently stands at around 5,000. So what about Zigbee2mqtt-rs and device support? To date, I have ported 2,964 definitions from 297 vendors. Supported coordinators are primarily those based on EmberZNet adapters, such as the Sonoff dongle-e. It has been successfully tested with 7.4.x and 8.x firmware. In theory, coordinators based on Texas Instruments Z-Stack (CC2652P/CC2652R/CC1352P) with firmware 3.x.y are also supported, but they have not been tested as I do not have the necessary hardware.
If you’ve read this far, you’re probably keen to try it out, and that’s great! It has been designed as a drop-in replacement for zigbee2mqtt running via Docker.
- backup you current data as precaution, even though it should just work
- change docker image to “docker-registry.iotdomu.cz/founek2/zigbee2mqtt-rs:latest”
- you all set! It uses identical zigbee2mqtt configuration.yaml file along with coordinator backup, state.json and database.db.
If you encounter any issues or would like to add a device that Z2M supports but Z2M-RS does not yet, please raise an issue on GitHub. Contributions are welcome!


Top comments (0)