TechSomething

Home Automation

Index: #

Preface: #

With the help of a friend, I wanted to try to see how deep was the rabbit's hole of self-made home automation,
this is the full extent of the installation which now has been reduced:

description

Platform: #

After installing and using Home Assistant for a bit, I decided to decouple the MQTT server, which will be the center of my installation, and the rest.

In this way Home Assistant wil be a "satellite" of MQTT like all the other parts.

In the end we have two main ways to interact with the sensors:

Sensors: #

We are using different types of sensors:

Tasmota: #

Tasmota is an open source firmware for ESP devices that allows you to control the flashed device via MQTT, WebUi or http.

We use Tasmota flashing it's firmware on commercial devices or self-made devices, here is a list of supported tasmota devices.

NB: You could flash Tasmota via Tuya-convert on Tuya devices but unfortunately Tuya updated it's devices so no conversion can take place on-the-fly or worst, they use something different than ESP boards so Tasmota is not supported anymore on those devices.

here we'll see the devices I use with Tasmota:

Sonoff Mini: #

An in-wall switch module I used to take over existing single switches and make them remotely controlled,
this retains the functionality of the physical switch that now is connected to the Sonoff Mini.

https://templates.blakadder.com/sonoff_mini.html

Shelly 1: #

An in-wall switch module like Sonoff Mini but I sued it for double deviators.
https://templates.blakadder.com/shelly_1.html

Koogeek W-DEXI: #

A power-monitoring plug

https://templates.blakadder.com/kogeek_W-DEXI.html

Arilux LC03 RGB Module: #

An RGB controller

https://templates.blakadder.com/arilux_SL-LC_03.html

Ikea Vindriktning: #

I've "Tasmotized" this device adding an ESP-8266 inside it.

https://blakadder.com/vindriktning-tasmota/

Node-MCUs #

These are ESP-8266 self-made devices:

I have some of them, one is connected to a relay board to act as a switch and open a door (otherwise opened with a push-button),
another has a DHT22 to act as a thermometer-hygrometer, another has a IR emitter to send IR messages to interact (blindly, so no messages a received back) with IR-controlled appliances.

Sonoff ZBBridge: #

This device is a Sonoff Zigbee Bridge, reflashed with tasmota.

It allows me to integrate ZigBee devices with the rest of the isntallation (see ZigBee chapter).

https://zigbee.blakadder.com/Sonoff_ZBBridge.html

Zigbee: #

Zigbee is a low-power consumption standard for small meshed local network of wireless devices.

Usually, for home automation, you'll need an hub to integrate ZigBee devices in your installation, I've used a ZBBridge reflashed with Tasmota, this allows me to have a zigbee2tasmota integration, my goal however was just having a zigbee-mqtt compatibility layer.

There many vendors that produce devices compatible with my implementation, I found a compatibility Repository here: Blakadder Zigbee Device Compatibility Repository.

The devices are battery powered, but only send a message when there is a change of state, for example a thermomter-hygrometer will send a message only when temp/hum changes.

This allows to have a long battery life (allegedly).

Right Now I am using:

All the Sonoff devices are well made, compact and work very well.
The Ikea ones are a bit disappointing, the motion sensor for example sends just a message when it detects motion and not when it stops detecting, it just resets after 180 seconds,
the ON/OFF switch worked for 1 day and the disappeared and won't talk with the bridge..

ZB_Repeater #

Some of the always-powered devices (not the battery ones) like the Aqara Smart Plug or the IKEA Tradfri bulb, can act as mesh repeater in the ZigBee network, in this way if your Bridge does not cover all your area you can rely on the Repeater.

NB: in my experience I had to re-join the devices to the bridge after adding the repeaters, otherwise they won't mesh with them.

description

Homekit Native Devices: #

I am using some Meross/Refoss Wifi smart plugs.
They have static DHCP mappings and are denied any network destination via the firewall.

Homebridge: #

Homebridge is a fantastic piece of software that allows you to integrate many non-supported devices in your Homekit installation,
you can use many of the available plugins,
I am using homebridge-mqttthing to use my MQTT devices, some examples: Homebridge examples

Homekit: #

Homekit is Apple devices's native home management software.
Paired with an Hub, which is a device that is always on and connected to your network, it allows you to create automations and use the devices from outsoide of your wifi network.

Home Assistant: #

Does the same as Homekit, in a much more flexible way.

Telegraf, Influx and Grafana: #

or "TIG".
it's what I already use to collect and plot metrics,
so I wanted to add the metrics of the new devices, for example the Sonoff Temp/Hum and Ikea's Vindriktning.
Telegraf has an MQTT Consumer Plugin, the version that is working for me is Telegraf >= 1.21.

These is my /etc/telegraf/telegraf.d/mqtt.conf:

[[inputs.mqtt_consumer]]
servers = ["tcp://192.168.1.110:1883"] #your mqtt server

## Topics that will be subscribed to.
topics = [
"tele/tasmota_ZBBridge/ZB_Sonoff_Temp01/SENSOR", ###Zigbee Temp01
"tele/tasmota_Vindriktning/SENSOR", ###VINDRIKTNING Tasmota
]

qos = 0
connection_timeout = "30s"
username = "user" #influxdb user
password = "pass" #influxdb pass
data_format = "json"


[[processors.regex]]
[[processors.regex.field_rename]]
pattern = '(^ZbReceived_)\w+_'
replacement = "${2}"

[[outputs.influxdb]]
urls = ["http://localhost:8086"]
database = "sensors"

pay particular attention to "processors.regex"
(AFAIK available from Telegraf >= 1.21),
since it's the component that keeps our metric collectioning sane.

Without this block all our measurement would contain the Topic, for example if we receive a message:

tele/tasmota_ZBBridge/ZB_Sonoff_Temp01/SENSOR {"ZbReceived":{"0x1111":{"Device":"0x1111","Name":"ZB_Sonoff_Temp01","Humidity":94.84,"Endpoint":1,"LinkQuality":34}}}

Telegraf would log the metric like this:

ZbReceived_0x1111_Humidity

when we just want "Humidity" because having more than 1 Thermometer would drive us crazy with many different metrics,
after the processor.regex our metric looks like this:

Humidity

This applies particularly to Zigbee devices as you can see from "ZbReceived".

This is the other configfile of telegraf (the main one), /etc/telegraf/telegraf.conf:

[global_tags]

[agent]
logfile = "/var/log/telegraf/telegraf.log"
interval = "10s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "10s"
flush_jitter = "0s"
precision = ""
hostname = ""
omit_hostname = false

the result:

description

as you can see the metrics from the Zigbee devices are not always updated, on the contrary of Tasmota's ones that are updated on a schedule.

Todo: #

Homebridge examples: #

Straight MQTT integration: #

Tasmota WDexi Plug, Sonoff Mini, Shelly 1, : #

        {
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"topics": {
"getOn": "stat/tasmota01/POWER",
"setOn": "cmnd/tasmota01/POWER"
},
"type": "lightbulb",
"name": "Tasmotized-WDEXI",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"onValue": "ON",
"offValue": "OFF"
}

NodeMCU Temp and Hum: #

        {
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"type": "temperatureSensor",
"name": "Tasmota-NodeMCU-01_Temperature",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getCurrentTemperature": {
"topic": "tele/tasmota_Nodemcu01/SENSOR",
"apply": "return JSON.parse(message).AM2301.Temperature;"
}
}
},
{
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"type": "humiditySensor",
"name": "Tasmota-NodeMCU-01_Humidity",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getCurrentRelativeHumidity": {
"topic": "tele/tasmota_Nodemcu01/SENSOR",
"apply": "return JSON.parse(message).AM2301.Humidity;"
}
}
}

RGB Controller: #

NB: it uses the plugin "sonoff-tasmota-mqtt-hsb"

        {
"plugin_map": {
"plugin_name": "homebridge-sonoff-tasmota-mqtt-hsb",
"index": 0
},
"accessory": "sonoff-tasmota-mqtt-hsb",
"name": "STUDIO - Scrivania Led RGB - Tasmota-RGB-01",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"caption": "test_rgb",
"topics": {
"setOn": "cmnd/tasmota_RGB/Power",
"setHsb": "cmnd/tasmota_RGB/HSBColor",
"status": "stat/tasmota_RGB/RESULT",
"getHsb": "stat/tasmota_RGB/HSBColor"
}
}

Ikea Vindriktning: #

        {
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"type": "airQualitySensor",
"name": "Vindriktning",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getAirQuality": {
"topic": "tele/tasmota_Vindriktning/SENSOR",
"apply": "return (JSON.parse(message).VINDRIKTNING['PM2.5'] > 55 ? 'POOR' : JSON.parse(message).VINDRIKTNING['PM2.5'] > 35 ? 'INFERIOR' : JSON.parse(message).VINDRIKTNING['PM2.5'] > 12 ? 'FAIR' : JSON.parse(message).VINDRIKTNING['PM2.5'] > 4 ? 'GOOD' : JSON.parse(message).VINDRIKTNING['PM2.5'] <= 3 ? 'EXCELLENT' : 'UNKNOWN');"
},
"getPM2_5Density": {
"topic": "tele/tasmota_Vindriktning/SENSOR",
"apply": "return JSON.parse(message).VINDRIKTNING['PM2.5'];"
}
},
"airQualityValues": [
"UNKNOWN",
"EXCELLENT",
"GOOD",
"FAIR",
"INFERIOR",
"POOR"
]
}

ZigBee devices: #

this is a bit trickier since when you send a command via MQTT it also needs to be sent via Zigbee,
so you have to add "ZbSend" command and payload.

Sonoff Motion Sensor: #

        {
"accessory": "mqttthing",
"type": "occupancySensor",
"name": "ZB_Sonoff_Move01",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getOccupancyDetected": {
"topic": "tele/Tasmota_ZBBridge/ZB_Sonoff_Move01/SENSOR",
"apply": "return JSON.parse(message).ZbReceived['0x1111'].Occupancy;"
}
},
"integerValue": "true"
}

Sonoff Button: #

        {
"accessory": "mqttthing",
"type": "statelessProgrammableSwitch",
"name": "ZB_Sonoff_Butt01",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getSwitch": {
"topic": "tele/Tasmota_ZBBridge/ZB_Sonoff_Butt01/SENSOR",
"apply": "return JSON.parse(message).ZbReceived['0x2222'].Power;"
}
},
"switchValues": [
2,
1,
0
]
}

Sonoff Temp and Hum sensor: #

        {
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"type": "temperatureSensor",
"name": "ZB_Temp01 - TEMP",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getCurrentTemperature": "tele/Tasmota_ZBBridge/ZB_Sonoff_Temp01/SENSOR$.ZbReceived[*].Temperature"
}
},
{
"accessory": "mqttthing",
"type": "humiditySensor",
"name": "ZB_Temp01 - HUM",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getCurrentRelativeHumidity": "tele/Tasmota_ZBBridge/ZB_Sonoff_Temp01/SENSOR$.ZbReceived[*].Humidity"
}
}

Sonoff Door sensor: #

        {
"accessory": "mqttthing",
"type": "contactSensor",
"name": "ZB_Sonoff_Door01",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getContactSensorState": {
"topic": "tele/Tasmota_ZBBridge/ZB_Sonoff_Door01/SENSOR",
"apply": "return JSON.parse(message).ZbReceived['0x4444'].Contact;"
}
},
"integerValue": "true"
}

Aqara Plug: #

        {
"accessory": "mqttthing",
"type": "outlet",
"name": "ZB_Aqara_Plug01",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getOn": {
"topic": "tele/Tasmota_ZBBridge/ZB_Aqara_Plug01/SENSOR",
"apply": "return JSON.parse(message).ZbReceived['0x5555'].Power;"
},
"setOn": {
"topic": "cmnd/Tasmota_ZBBridge/ZB_Aqara_Plug01/ZbSend",
"apply": "return '{\"device\":\"0x5555\", \"send\":{\"Power\":' + message + '}}'"
}
},
"integerValue": "true"
}

Tradfri Dimmerable Bulb: #

        {
"accessory": "mqttthing",
"plugin_map": {
"plugin_name": "homebridge-mqttthing",
"index": 0
},
"type": "lightbulb",
"name": "ZB_Tradfri_Bulb11",
"url": "mqtt://192.168.1.110:1883",
"username": "username",
"password": "password",
"topics": {
"getOn": {
"topic": "tele/Tasmota_ZBBridge/ZB_Tradfri_Bulb11/SENSOR",
"apply": "return JSON.parse(message).ZbReceived['0x6666'].Power;"
},
"setOn": {
"topic": "cmnd/Tasmota_ZBBridge/ZB_Tradfri_Bulb11/ZbSend",
"apply": "return '{\"device\":\"0x6666\",\"send\":{\"Power\":\"' + message + '\"}}'"
},
"getBrightness": {
"topic": "tele/Tasmota_ZBBridge/ZB_Tradfri_Bulb11/SENSOR",
"apply": "return Math.round(JSON.parse(message).ZbReceived['0x6666'].Dimmer / 2.55)"
},
"setBrightness": {
"topic": "cmnd/Tasmota_ZBBridge/ZB_Tradfri_Bulb11/ZbSend",
"apply": "return '{\"device\":\"0x6666\",\"send\":{\"Dimmer\":' + Math.round(message * 2.55) + '}}'"
}
},
"onValue": "1",
"offValue": "0",
"integerValue": "true"
}