My attempt to actually control anything with my setup. Actually a lamp. So what I had is exactly this lamp: SmartWise Lamp and the seller was claiming that it is compatible with zigbee2mqtt.
So I enabled my z2m to allow parties to join by setting permit_join
to true
.
Then turned on the bulb.
Nothing happened, so looked at the instruction manual, and realised, that the lamp when is in pairing mode will blink. And in order to put it in pairing mode I need to turn it on and off 3 times. So did that. And nothing. And faster, and nothing.
And suddenly some device appeared, that wasn't supported. So I got a bit frustrated, tried to remove the device that got registered. Learned these commands along the way:
mosquitto_pub -t "zigbee2mqtt/bridge/config/remove" -m 'lamp-0'
mosquitto_pub -t "zigbee2mqtt/bridge/config/force_remove" -m 'lamp-0'
At the end it turned out:
- There was an IKEA Tradfri LED driver, that got half-registered, I suspect because it was turned off mid-way, or maybe I had some issues with where my coordinator was. I still have no clue.
- The lamp was not in pairing mode. It turned out, timing is king, so when you turn it
on or off, you need to wait some time. So:
- Lamp turned on
- Turn it off, leave 2 secs
- Turn on, leave 1 sec
- Then you repeat, and on the fourth turn on, it will be in pairing mode
- The Lamp is this model
- The Tradfri is this model
Finally, I ended up with having 3 devices in my network!
So wrote this code to control both of them with a single button:
import paho.mqtt.client as mqtt
import json
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("zigbee2mqtt/button-0")
# The callback for when a PUBLISH message is received from the server.
STATE = "OFF"
def on_message(client, userdata, msg):
data = json.loads(msg.payload.decode())
try:
action = data["action"]
except KeyError:
return
global STATE
print(action)
if str(action) == "single":
if STATE == "OFF":
STATE = "ON"
else:
STATE = "OFF"
client.publish("zigbee2mqtt/light-0/set", json.dumps({"state": STATE}))
client.publish("zigbee2mqtt/kitchen-0/set", json.dumps({"state": STATE}))
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connect("localhost", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
mqttc.loop_forever()