Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
homeautomation:mqtt [2024/10/31 08:47] – willy | homeautomation:mqtt [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== MQTT ====== | ||
- | |||
- | [[https:// | ||
- | |||
- | MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish/ | ||
- | |||
- | MQTT is pretty handy if you need to integrate existing automations or devices connected to computers in your home, because you can quickly and easily write with minimal effort some lines of code to start sending and receiving messages with your Home Assistant. | ||
- | |||
- | === Brokers === | ||
- | |||
- | Without getitng too much involved in how MQTT works, you need a broker. A Broker is a software that the other MQTT paricipants can connect to and which will send/ | ||
- | |||
- | The most common broker is [[https:// | ||
- | |||
- | === Topics === | ||
- | |||
- | Each topic is a message. The name of the topic is in the form of: | ||
- | < | ||
- | one/ | ||
- | </ | ||
- | |||
- | you can specify levels in your topic name, by separating them with a / sign. | ||
- | |||
- | I tend to assign three levels: | ||
- | < | ||
- | floor/ | ||
- | </ | ||
- | |||
- | so for example a temperature sensor locate on the ground floor in the kitchen will publish a topic called **ground/ | ||
- | |||
- | The payload of the topic can be a wide range of formats, i tent to prefer JSON because it's easily parsed in Home Assistant. | ||
- | |||
- | === Subscriber / Publisher === | ||
- | |||
- | Each entity participating in the MQTT network can be a published or a subscriber of a topic. Usually your sensors will be publishers, while more complex devices an be subscribers to receive topics. | ||
- | |||
- | As an example, i have a small Pi device in my kitchen with a 1wire temperature sensor. This sensor can be calibrated from another temperature reading, which is connected via ZigBee. The Pi reads the sensor and publish a topic called **ground/ | ||
- | |||
- | In this case, my Pi acts as a publisher for the first and a subscriber for the second. | ||
- | |||
- | |||
- | |||
- | ==== Home Assistant ==== | ||
- | |||
- | In Home Assistant you can choose more than one MQTT broker. I choose the Mosquitto Broker which can be installed from //Settings -> Devices -> Add Integration// | ||
- | |||
- | Installing is just a few clicks, and you can also send and receive topics directly from the page, for debug purposes. | ||
- | |||
- | === Define MQTT sensors and devices === | ||
- | |||
- | Adding MQTT based entities in Home Assistant is done by directly editing the **configuration.yaml** file. | ||
- | |||
- | This is my example: | ||
- | < | ||
- | mqtt: | ||
- | sensor: | ||
- | - name: " | ||
- | state_topic: | ||
- | device_class: | ||
- | value_template: | ||
- | unique_id: " | ||
- | unit_of_measurement: | ||
- | binary_sensor: | ||
- | - name: "Gas Stove On" | ||
- | state_topic: | ||
- | device_class: | ||
- | value_template: | ||
- | payload_on: 1 | ||
- | payload_off: | ||
- | unique_id: " | ||
- | </ | ||
- | |||
- | ==== Mosquitto on Armbian ==== | ||
- | |||
- | On my Pi i have installed the various Mosquitto packages: | ||
- | <code bash> | ||
- | sudo apt-get install | ||
- | sudo apt-get install mosquitto-dev | ||
- | sudo apt-get install libmosquitto-dev | ||
- | </ | ||
- | |||
- | then you can use both python and C bindings. | ||
- | |||
- | [[https:// | ||
- | |||