MQTT seems slow, unsure what next to troubleshoot

Rather new to home automation, home assistant, Zigbee, MQTT, the lot. I might have a problem with MQTT. Or not.

My setup is a RPi 4B/2Gb/wired ethernet/ Raspbee(=Conbee) with Hass.io. 5 Trådfri bulbs. That’s all working quite nicely.

I have Raspberry Pi Zero (on wired ethernet) with a DHT sensor and a PIR sensor. And I have Mosquitto broker on a separate Raspberry Pi Zero W.

The sensors are working, I can send readings/detect motion and see both appear as MQTT messages on the broker and in HA.
I added an automation to HA (my first) to change the lights when movement is detected. It’s working…but…

It takes 5 seconds between the PIR being triggered and the MQTT msg to arrive at both the broker and HA. At first I suspected the Wifi, but after connecting the broker to wired ethernet, it’s the same. However, when I Publish a message from HA, it arrives at the broker instantly.

I’m unsure if the 5 second delay would be a real problem, although I can imagine 5 seconds is rather long for me, when half asleep stumbling into the bathroom in the middle of the night. For now, however, I just want to understand what’s going on, what’s causing the problem.

Any ideas/suggestions?

Maybe it’s your PIR? Try triggering the input pin on the DHT / movement Pi zero and see how long it takes to arrive at the broker.

I’m using many PIRs connected to a pi3 running this and it’s virtually instant.

I don’t think it’s the PIR; i added a buzzer (should have mentioned that!) as an audible indicator. Between the beep (thus the PIR detected motion) and the msg arriving at the broker (and HA) there’s this 5 seconds delay.
…Just tested by removing the PIR and making the GPIO high by manually connecting it to 3.3V. The beep is instantly, the msg arrives 5 seconds later.

I also changed the GPIO pin, but same result: 5 seconds delay.

Additionally:
The Raspbian version I’m using is the full version; I just tested with the latest light version, but the delay remains the same.

So it’s whatever you are using on the pi to publish to the broker then?

You could try the program I linked to above. Very easy to configure. Just a YAML file.

I’m using Python/paho.mqtt. My code:

# PIR/MQTT test

import sys
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
import time

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# inits
MQTTBroker = "192.168.1.180"
MQTTpTopic = "RPiDevW/Motion"
# BuzzerGPIO
GPIOBuzzer = 21
# PIR GPIO
GPIOPIR = 20

GPIO.setup(GPIOBuzzer, GPIO.OUT)
GPIO.setup(GPIOPIR, GPIO.IN)

# MQTT Publish
def on_publish(client,userdata,result):
    pass

while True:
    time.sleep(2)
    if GPIO.input(GPIOPIR):
        GPIO.output(GPIOBuzzer, True)
        time.sleep(0.1)
        GPIO.output(GPIOBuzzer, False)
        Msg = "(E)Motion!"
        print(Msg)
        time.sleep(5)
        client1= mqtt.Client("control1")
        client1.on_publish = on_publish
        client1.connect(MQTTBroker,1883)
        client1.publish(MQTTpTopic, Msg)
    time.sleep(0.1)

Duh. And a big one at that!

I just pasted the MQTT code into my PIR-test code…and completely missed the time.sleep(5) in there, just before publishing the MQTT message.

Fixed! All is working working as intended now, near instant lights-on when motion is detected.

Thanks for thinking along!