After a pip upgrade the chiller script on our Pi dies with an «Unsupported callback API version» error
Not a programmer, I run a food plant. Our maintenance tech, who has since left, wrote a Python script on a Pi 3 that reads the blast chiller temperature and sends it to Hermetry. It’s been fine for two years. Last week someone ran updates on the Pi (apt upgrade and a pip install –upgrade, I’m told) and now the service won’t start. journalctl shows:
Traceback (most recent call last):
File "/home/pi/chiller/send.py", line 14, in <module>
client = mqtt.Client("chiller-pi")
File ".../paho/mqtt/client.py", line 772, in __init__
raise ValueError(
ValueError: Unsupported callback API version: version 2.0 added a callback_api_version, see docs/migrations.rst for details
The chiller has an alarm rule in Hermetry and right now nobody would get told if it warms up, so I’d like this back today. What do I change?
Which paho version is on there now? pip show paho-mqtt
2.1.0. The tech’s notes say 1.6.1.
Quickest way back: pip install paho-mqtt==1.6.1 and restart the service. Then fix it properly when there’s time.
Pinning works to get the alarm back today. The proper fix is two lines. paho 2.x wants the callback API version as the first argument, and your script passes the client id there, so «chiller-pi» gets read as an API version. Change the constructor and the on_connect signature:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, reason_code, properties):
print("connected:", reason_code)
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="chiller-pi")
client.on_connect = on_connect
client.username_pw_set("<ACCESS_TOKEN>") # token as username, empty password
client.tls_set()
client.connect("<device host>", 8883, keepalive=60)
client.loop_start()
...
client.publish("v1/telemetry", json.dumps({"chiller_temp_c": t}), qos=1)
Don’t switch the protocol to MQTT 5 while you’re in there. Hermetry speaks 3.1.1 and refuses an MQTT 5 CONNECT with return code 1. paho still defaults to 3.1.1, so leaving it alone is right.
If the tech had other callbacks (on_disconnect, on_publish), they changed signature in 2.x as well, check the migration notes for each one.
Changed the line, the on_connect had the old four arguments so that needed fixing too. Service is up, readings back on the board and I tested the alarm by opening the chiller door for a few minutes. Thanks Aoife, and Ciarán for the stopgap.
Worth putting the versions in a requirements.txt next to the script so the next upgrade doesn’t surprise anyone.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

