martin-ger / esp_mqtt

MQTT Broker/Bridge on the ESP8266
MIT License
297 stars 70 forks source link

Help! How can i prevent ghost switching when using this script #60

Closed jampez77 closed 4 years ago

jampez77 commented 4 years ago

I'm using the script below as both a PIR sensor and an MQTT bridge for the devices on the middle floor. Sometimes however, there is some ghost switching. Shortly after turning on a light it will be turned off.

Is there any way to prevent this from happening?

`% % Script for a fully transparent MQTT bridge %

config speed 160

% The initialization, this is done once after booting on init do

println "Starting the PIR script"

% Bridge topic, all messages from this will be forwarded to the remote broker
setvar $forward_topic="middleFloor/#"
setvar $cmndTopic="cmnd/" | $forward_topic
setvar $statTopic="stat/" | $forward_topic

% Topic for the PIR input, must begin with stat and the same forward_topic,
% followed by room and sensor type
setvar $pir_topic="stat/middleFloor/spareRoom/pir"

println "Starting the bridge script"

setvar $local_pub = ""
subscribe local $cmndTopic
subscribe local $statTopic

% This is done after each MQTT (re)connect on mqttconnect do subscribe remote $cmndTopic subscribe remote $statTopic

% Now the events, checked whenever something happens

% The generic local topic handler on topic local $cmndTopic do println "Got " | $this_topic | " " | $this_data % Publish this remotely if not ($local_pub = $this_topic) then publish remote $this_topic $this_data retained else setvar $local_pub = "" endif

on topic local $statTopic do println "Got " | $this_topic | " " | $this_data % Publish this remotely if not ($local_pub = $this_topic) then publish remote $this_topic $this_data retained else setvar $local_pub = "" endif

% The generic remote topic handler on topic remote $cmndTopic do % Publish this locally setvar $local_pub = $this_topic publish local $this_topic $this_data

on topic remote $statTopic do % Publish this locally setvar $local_pub = $this_topic publish local $this_topic $this_data

% The PIR, D1 pin on Wemos D1 Mini on gpio_interrupt 5 nopullup do setvar $pir_status = $this_gpio if $pir_status = 1 then publish local $pir_topic "on" else publish local $pir_topic "off" endif `