Rules, cannot get Publish (to mqtt) working

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Rules, cannot get Publish (to mqtt) working

#1 Post by JR01 » 25 Oct 2020, 19:45

Hi, I have an ESP on a gate, I want it to publish directly to my Homebridge server on topic homebridge/to/set to update the status of the garage door as the switches monitoring the door state is switched on / off. The Rules below does not send the MQTT code. I do receive normal mqtt updates from the device on its set Controller topic - thus mqtt is functioning. (homie/%sysname%/%tskname%/%valname%). the registered devices are hooked up to open / close reeds which reads if the door is opened, or closed:
1) Device: GateClosed, Value: Status
2) Device: GateOpened, Value: Status

Code: Select all

// ---- Send status when open or closed ----------
on GateClosed#Status=1.00 do    // door closed 
  event,publishHK='CurrentDoorState',1
  event,publishHK='TargetDoorState',1
endon

on GateClosed#Status=0.00 do    // door just opening from closed
  event,publishHK='CurrentDoorState',2
  event,publishHK='TargetDoorState',0
endon

On publishHK Do
  Publish homebridge/to/set,'{"name":"ESP66.Gate1.relay","service_name":"Garage Door","characteristic":"%eventvalue1%","value":%eventvalue2%}'
EndOn
Can anybody spot issues with above rule?
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

User avatar
Ath
Normal user
Posts: 3517
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Rules, cannot get Publish (to mqtt) working

#2 Post by Ath » 25 Oct 2020, 20:21

You can't pass strings to an event in this way. Strings are not a 'first class citizen' yet, in ESPEasy, you can only pass numeric values to an event.
Within the event you can select the desired subject using the if statement:

Code: Select all

// ---- Send status when open or closed ----------
on GateClosed#Status=1 do    // door closed 
  event,publishHK=1,1  // CurrentDoorState 1
  event,publishHK=2,1  // TargetDoorState 1
endon

on GateClosed#Status=0 do    // door just opening from closed
  event,publishHK=1,2  // CurrentDoorState 2
  event,publishHK=2,0  // TargetDoorState 0
endon

On publishHK Do
  if %eventvalue1%=1
    Publish homebridge/to/set,'{"name":"ESP66.Gate1.relay","service_name":"Garage Door","characteristic":"CurrentDoorState","value":%eventvalue2%}'
  endif
  if %eventvalue1%=2
    Publish homebridge/to/set,'{"name":"ESP66.Gate1.relay","service_name":"Garage Door","characteristic":"TargetDoorState","value":%eventvalue2%}'
  endif
  // add more if needed
EndOn
Assuming the ESP's name is ESP66, you can replace that with %sysname%, to make your script a bit more portable, if desired.
/Ton (PayPal.me)

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Solved: Rules, cannot get Publish (to mqtt) working

#3 Post by JR01 » 25 Oct 2020, 20:44

Just tried it - it works, thank you !
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

JR01
Normal user
Posts: 260
Joined: 14 Feb 2016, 21:04
Location: South Africa

Re: Rules, cannot get Publish (to mqtt) working

#4 Post by JR01 » 25 Oct 2020, 21:53

The above help should be in the Rules documentation, seriously.....

https://espeasy.readthedocs.io/en/lates ... Rules.html
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

Post Reply

Who is online

Users browsing this forum: No registered users and 101 guests