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.