MQTT connectivity

Craftbeerpi4 has the possibility to communicate with mqtt brokers/devices.
To activate the mqtt service within cbpi, you need to modify the config.yaml file inside the config folder manually.
index_url: /cbpi_ui/static/index.html
mqtt: True
mqtt_host: IPofYourMQTTServer
mqtt_password: YourMQTTPassword
mqtt_port: 1883
mqtt_username: YourMQTTUser
name: CraftBeerPi
password: 123
- cbpi4ui
- cbpi4-FermenterHysteresis
- cbpi4-BM_PID_SmartBoilWithPump
- cbpi4-PID_AutoTune
port: 8000
username: cbpi
version: 4.0.8
mqtt_host can be set to localhost if the MQTT broker is also running on your pi
You need to set mqtt to True and adapt the other settings to your mqtt infrastructure. Once this has been changed and saved, you need to restart cbpi.
The craftbeerpi server does not provide the mqtt infrastructure. It just can connect to a MQTT broker. If you have not set up a mqtt broker in you home network (e.g. a NAS or other server), you need to install and set up a broker on your pi. The is described here.

Install and configure an MQTT broker on your raspberry pi

First you need to install the MQTT broker:
sudo apt-get install mosquitto
The mosquitto application acts as MQTT broker that can receive messages / instructions from the CraftbeerPi4 server and other devices (e.g. the mqttdevie) can read/receive them. It can also receive sensor data or commands from other devices and the CraftbeerPi4 server can read them. The application basically acts as central communication point between devices that use the MQTT protocol.
Now you need to configure you MQTT broker that CraftbeerPi but also the other devices can send and receive messages via the broker. Two methods are possible. The first one does not require a password and is only recommended during set up for testing. The second method will require passwords and a user for the MQTT broker.
You need to edit the mosquitto config file.
sudo nano /etc/mosquitto/mosquitto.conf
Add the following two lines at the top of the config file:
allow_anonymous true
port 1883
With this method you don't need to enter a user or password in craftbeerpi or other devices for the MQTT broker
Navigate to the mosquitto app folder:
cd /etc/mosquitto
And create password for the user pi:
sudo mosquitto_passwd -c passwdfile pi
Now you need to enter the password you want to use twice.
Afterwards, you need to edit your mosquitto config file.
sudo nano /etc/mosquitto/mosquitto.conf
And add three lines to the top of the file.
allow_anonymous false
port 1883
password_file /etc/mosquitto/passwdfile
Once the file has been saved, you need to restart the MQTT broker:
sudo systemctl restart mosquitto
And adapt the config file of your cbpi server as described here.
The mqtt user should be set tp pi or whatever user you have defined for mosquitto and the password needs to be set to the password you have defined above.
After restart you should see a line in the cbpi log that confirms the connection to the MQTT broker: Jan 25 13:54:10 raspberrypi cbpi[360]: 2022-01-25:13:54:10,513 INFO [] MQTT Connected to
If you want to analyze the MQTT communication in more detail you can also install the MQTT explorer on your PC. This allows you to analyze the MQTT messages in more detail.

Using CraftbeerPi to read / trigger external MQTT devices

When mqtt is enabled and running, you will see also a mqtt sensor in the sensor hardware.
MQTT Sensor is available, when mqtt is activated in cbpi
You need to specify the topic and you can define a payload dictionary/filter. If the mqtt sensor is providing the sensor value e.g. as {"name": "MyMQTTSensor" , "value": 123.4} you should enter value into the dictionary to filter out the sensor value.
In addition to the sensor, a mqtt actor is also available, when mqtt is activated.
MQTT actor is available, when mqtt is activated in cbpi
Switching the actor on will send the following value to mqtt under actor/speidelheat:
"state": "on",
"power": 100
You can also change the power via the actor button actions menu for the mqtt actor which will change the power value of the json string:
"state": "on",
"power": 70
The power of the mqtt actor can be changed while it is on or off. The power value will be always updated via mqtt, but the state of the actor will remain unchanged (on or off).
The existing kettle logic plugins (eg. PIDBoil) will also work with mqtt actors and sensors. They will set the power for the mqtt actor.

Other MQTT Actor types

In case want to switch other types of devices with CraftBeerPi via MQTT that expect a different message format you can choose other actor types that may be a better fit.

MQTT Actor (Tasmota)

To easily switch Tasmota devices on or off, there is a specific actor for this in CraftBeerPi. It just sends on or off to the specified topic. Use the MQTT Actor (Tasmota) if you want this behavior.

MQTT Actor (Generic)

There may be other devices that expect other message formats to be switched on or off. In those cases you can use the MQTT Actor (Generic) to use a custom message format, that contains placeholders that are replaced with actual values before sending the message.
The available placeholders are:
replaced with (on)
replaced with (off)
power level (e.g. 100)
power level (e.g. 100)
The message template uses standard python string formatting to replace the values so if your message template should contain curly braces {} for example to create a json message template, you need to escape them with double braces. So to get one { you need to write {{ in the message template.
Message template
after replace (on)
SET someswitch={switch_onoff}
SET someswitch=on
{{"name": "some json template", "switch_state": "{switch_onoff}"}}
{"name": "some json template", "switch_state": "on"}

Trigger CraftbeerPi actors via MQTT

You also can change the status of CraftbeerPi actors via mqtt. There are currently three different mqtt 'commands' for an actor you can send to Craftbeerpi:
  1. 1.
    Switch an Actor on
  2. 2.
    Switch an Actor off
  3. 3.
    Set the power for an Actor
To switch a CraftbeerPi Actor on, you just need to publish a topic via your mqtt broker:
This will switch actor QD7WjbFHWyGKMCCFai4uSW on. If you want to switch the actor off, you need to replace the on with an off:
If you want to change the power for an actor, the on or off needs to be replaced with power:
You also need to send a power value between 0 and 100 as raw value in the payload. This will set the power for actor QD7WjbFHWyGKMCCFai4uSW to the desired value.

CraftbeerPi is continuously updating all sensors and actors

Craftbeerpi is continuously sending status updates on all sensors and actors via mqtt
Sensor data is updated via:
Each sensor is updated as separate topic where only the sensor ID is varying
This is an example for the cbpi sensor 2Sn46Fc6jRGvES9MuQXHdG. The payload for a sensor update has the following content:
"id": "2Sn46Fc6jRGvES9MuQXHdG",
"value": 19.72
Status of actors are shown in this topic:
Also here, each actor has it's own topic where only the actor id is different. The payload shows the current status of the actor:
"id": "QD7WjbFHWyGKMCCFai4uSW",
"name": "BM_Pump",
"type": "GPIOActor",
"props": {
"GPIO": 25,
"Inverted": "No"
"state2": "HELLO WORLD",
"state": true,
"power": 100
'state' represents the status of the actor. 'true' means that the actor is on, while 'false' represents the off status of an actor. 'power' shows the current power setting of the actor in %
Kettles can be seen under:
for each kettle.
Fermenters are using this topic:
Step information is updated under this topic:
and notifications the the topic:

Craftbeerpi can send Kettle and Fermenter updates on a defined frequency

Some devices (e.g. MQTTDeviceV4) may require more frequent updates on kettle or fermenter. Therefore, the server has the ability to push updates for kettle and fermenter with a defined frequency even if there are no changes in the payload. This frequency can be defined with on the cbpi settings page with the parameter MQTTUpdate

You can also request updates on kettles, fermenters, actors and sensors from CraftbeerPi

If your device or application requires an update on some items from craftbeerpi at a given time, you can also publish various topics and cbpi will respond with an update on the particular topic.
  1. 1.
    cbpi/updatesensor will update cbpi/sensorupdate
  2. 2.
    cbpi/updateactor will update cbpi/actorupdate
  3. 3.
    cbpi/updatekettle will update cbpi/kettleupdate
  4. 4.
    cbpi/updatefermenter will update cbpi/fermenterupdate
The payload can be empty for these topics
Some things had to be changed as the payload size was just too large for smaller devices / libraries. Therefore, each sensor, actor, kettle, fermenter,.... has one topic under a main topic for sensor updates, actor updates, kettle updates,... This has been tested extensively with the so called MQTTDeviceV2 but also V4.