Communication between Rpi zero and RPi 3 with MQTT and a PIR sensor

Dear all,

Currently I have a raspberry pi zero and and RPi3. HA is installed on RPi3 and the PIR sensor is connected to the zero. I’m able to communicate between the 2 Pi’s and I can read the status of the PIR ass well. I would like to turn off my lights for a x amount of time but for some reason the status of the PIR is not read in the automation config.

This is it what I have so far:

- alias: Turn on voorrraad kelder light when there is movement
  trigger:
    platform: state
    entity_id: sensor.voorraad_pir
    to: "ON"
  action:
    service: mqtt.publish
    data:
      topic: 'home/voorraad/switch1/set'
      payload: 'ON'
- alias: Turn off voorraad kelder x minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.voorraad_pir
    to: 'OFF'
    for:
      minutes: 1
  action:
    service: mqtt.publish
    data:
      topic: 'home/voorraad/switch1/set'
      payload: 'OFF'

the topic:

home/voorraad/switch1/set

is where the status of my PIR will be send to and based on that info my lights will turn on or off.

sensor.yaml


- platform: mqtt
    name: "voorraad pir"
    state_topic: "home/voorraad/detection"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

and my python script also located in the RPi zero:

#!/usr/bin/env python3

import logging
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)


# This is the Subscriber

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))
  client.subscribe("home/voorraad/switch1/set")

def on_message(client, userdata, msg):
  if msg.payload.decode() == "ON":
    GPIO.output(12,1)
    print("licht voorraad kelder is aan")
  if msg.payload.decode() == "OFF":
    GPIO.output(12,0)
    print("licht voorraad is uit")

client = mqtt.Client()
client.connect("xx.xx.xx.xxx",xxxx,60)
client.username_pw_set(username="xxxxxxx",password="xxxxxx")
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

Can someone please help me with the automation because I don’t want to write a script for every light I have, I prefer to use the automation that is available in HA.

if you just display the value of the sensor it seems to work properly ? Turning on and off when expected ?

Does any of the two automation work or both are the problem ? If you try to run the automation manually using the dev-service tool does it work ?

Hi,

I can see the change of the sensor and it works like expected. And yes both of the automation are having issues I did not use the dev-service tool.

If I just use mqtt in a terminal I can send and receive the data but when I want to send the data with the sensor and read the status on my main pi in a terminal I do not receive any values.

Mqtt switches are working fine but I want to get rid off the switches and want to use sensors instead.

Kind regards,

Glenn

I do not really understand where the problem is. Correct me if I am wrong :

  • Your PIR sensor (hardware) is working
  • You can send manually MQTT data from one the PiZero to the RPi3 using a terminal
  • The PIR sensor does not send MQTT value at all and you can not see anything in terminal.

If all these are right, then the code on the PiZero is the problem, and not the HA side.

Check this post, it might help you with the solution