MQTT Publish - correct syntax?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

MQTT Publish - correct syntax?

#1 Post by pw444 » 28 Dec 2020, 15:45

Hya,

i have the follwing rule, but something is wrong, as MQTT Explorer does not shows the change of state as published.


Code: Select all

on MQTTsniff#BOFF do
  if [MQTTsniff#BOFF]=1
      Publish,%sysname%/BOFF/status/1
      pulse,13,1,500
      pulse,2,0,500
      Publish,%sysname%/B24/status/0
      Publish,%sysname%/B58/status/0
  endif
endon

on MQTTsniff#B58 do
  if [MQTTsniff#B58]=1
     Publish,%sysname%/B58/status/1
     pulse,14,1,500
     pulse,2,0,500
     let,1,1
     Publish,%sysname%/B24/status/0
     Publish,%sysname%/BOFF/status/0
  endif
endon

on MQTTsniff#B24 do
  if [MQTTsniff#B24]=1
     Publish,%sysname%/B24/status/1
     pulse,12,1,500
     pulse,2,0,500
     let,1,1
     Publish,%sysname%/B58/status/0
     Publish,%sysname%/BOFF/status/0     
  endif
endon

Is the Publish,%sysname%/BOFF/status/0 correct, as i'm not finding a conclusive answer?

Thx in advance.

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT Publish - correct syntax?

#2 Post by TD-er » 28 Dec 2020, 15:55

First thing that comes to mind is that you must check if the topics you're looking at on the broker need a leading /
You're not sending one and that's the way how (I think) it should be, but it is a very common mistake to make that on one system you're using a leading / in the topics and not on the other one.


Another thing is this:

Code: Select all

  if [MQTTsniff#BOFF]=1
As it is also the event, you may want to refer to it as %eventvalue1%
Events are being queued, so the task variable may already have been changed.
But the value as it had when creating the event is stored in the eventvalues. (%eventvalue1% ... %eventvalue4%)

User avatar
dynamicdave
Normal user
Posts: 257
Joined: 30 Jan 2017, 20:25
Location: Hampshire, UK

Re: MQTT Publish - correct syntax?

#3 Post by dynamicdave » 29 Dec 2020, 09:28

I would have thought the publish command should be...

Publish,%sysname%/BOFF/status,1

Publish,%sysname%/BOFF/status,0

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT Publish - correct syntax?

#4 Post by TD-er » 29 Dec 2020, 10:04

Yep you're right, the publish is missing a message as it has only a topic.
Missed that one.

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: MQTT Publish - correct syntax?

#5 Post by pw444 » 29 Dec 2020, 13:22

dynamicdave wrote: 29 Dec 2020, 09:28 I would have thought the publish command should be...

Publish,%sysname%/BOFF/status,1

Publish,%sysname%/BOFF/status,0
Ok, thank you. Will try out and return.

The intend is to have on ESPEasy all the logic for MQTT.

The Controller: HA

The device: 3 push buttons.

On HA i have the card with these three buttons, as slide.

Any of the buttons shall remain ON for 1 second and then OFF again. And when ON, the state of the two other buttons shall become 0, so the button gets OFF. And i want to do it using MQTT.

On HA, MQTT is used to trigger any of these 3 buttons on ESPEasy.

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: MQTT Publish - correct syntax?

#6 Post by pw444 » 29 Dec 2020, 13:28

TD-er wrote: 28 Dec 2020, 15:55 As it is also the event, you may want to refer to it as %eventvalue1%
Events are being queued, so the task variable may already have been changed.
But the value as it had when creating the event is stored in the eventvalues. (%eventvalue1% ... %eventvalue4%)
ok, and how shall i know which is %eventvaue1%, %eventvalue2% and so on , given the following rules?

Code: Select all

// setup
// http://192.168.1.7/control?cmd=event,OffAll_Off
// http://192.168.1.7/control?cmd=event,On24_On
// nothing - status idle - no notification - status working 0
// started manually - set VAR#0  1 - remote set VAR#0 0
// started - mqtt - remotelly - set status working 1 set remote 1
// finnished - status working 1 and remote 1? - notification - status working 0 set remote 0
// finnished - status working 1 and remote 0? NO notification - status working 0 set remote 0
// publish idle, working, remote  --- 000, 010, 011


On System#Boot do
// GPIO,2,0  // status led - inversed - 1 OFF, 0 ON
 GPIO,12,0 // pb 2-4 PB24
 GPIO,14,0 // pb 5-8 PB58
 GPIO,13,0 // pb off PBOFF
// GPIO,4,0 //  ACC sense  // do not set, because it messes up the input - by hardware and devices
  Monitor GPIO,4  // D2
  let,1,0
endon

on MQTT#Connected do
  event,publishstatus
endon

// loop

On B58 do               // B58 - gpio 14 - D5
  pulse,14,1,500
  pulse,2,0,500
  let,1,1
endon

On B24 do               // B24 - gpio 12 - D6
  pulse,12,1,500
  pulse,2,0,500
  let,1,1
endon 

On BOFF do              // BOFF - gpio 13 - D7
  pulse,13,1,500
  pulse,2,0,500
  Publish,%sysname%/BOFF/status,0
endon

on MQTTIN#BOFF do
  if [MQTTIN#BOFF]=1
      pulse,13,1,500
      pulse,2,0,500
     Publish,%sysname%/BOFF/status,0
  endif
endon

on MQTTIN#B58 do
  if [MQTTIN#B58]=1
     pulse,14,1,500
     pulse,2,0,500
     let,1,1
  endif
endon

on MQTTIN#B24 do 
  if [MQTTIN#B24]=1
     pulse,12,1,500
     pulse,2,0,500
     let,1,1
  endif
endon

on VACC#status do
  if %eventvalue%=1  // status = 1
    if %v1%=1        // status = 1 var1 = 1
       logentry,"Working"
    endif
  else               // status = 0
    if %v1%=1        // status = 0 var1 =1
       let,1,0
       logentry,"Finished"
    endif 
  endif
endon

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

Re: MQTT Publish - correct syntax?

#7 Post by Ath » 29 Dec 2020, 13:49

pw444 wrote: 29 Dec 2020, 13:28 ok, and how shall i know which is %eventvaue1%, %eventvalue2% and so on , given the following rules?
Within every such event handler (on ... do ... endon) there are 4 eventvalue variables available, %eventvalue% and eventvalue1% (that's the same), %eventvalue2%, %eventvalue3% and %eventvalue4%

Whether there is any useful data in those variables depends on the event source, but for a 'normal' state-change, the new value is in %eventvalue%, and eventvalue2..4 are 0.

When sending an event command you can provide any values yourself, like 'event,B58=1,2,3,4' and the values 1..4 will correspond with the eventvalue variables 1..4 (as said, %eventvalue% and %eventvalue1% are the same, just a way to avoid a common typo, and keep backward compatibility).
/Ton (PayPal.me)

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: MQTT Publish - correct syntax?

#8 Post by pw444 » 29 Dec 2020, 14:01

ok, so the following rule will work, and for every event on... do ...endon, %eventvalue% will referr to the status of the given event, correct?

Code: Select all

on MQTTIN#BOFF do
  if %eventvalue%=1
      pulse,13,1,500
      pulse,2,0,500
     Publish,%sysname%/BOFF/status,0
  endif
endon

on MQTTIN#B58 do
  if %eventvalue%=1
     pulse,14,1,500
     pulse,2,0,500
     let,1,1
  endif
endon

on MQTTIN#B24 do 
  if %eventvalue%=1
     pulse,12,1,500
     pulse,2,0,500
     let,1,1
  endif
endon

on VACC#status do
  if %eventvalue%=1  // status = 1
    if %v1%=1        // status = 1 var1 = 1
       logentry,"Working"
    endif
  else               // status = 0
    if %v1%=1        // status = 0 var1 =1
       let,1,0
       logentry,"Finished"
    endif 
  endif
endon
Last edited by pw444 on 29 Dec 2020, 15:53, edited 1 time in total.

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

Re: MQTT Publish - correct syntax?

#9 Post by Ath » 29 Dec 2020, 15:20

Correct.
As TD-er already explained, these are the values that get sent with the event into the queue of events to be processed, so are the values of the moment the event trigger was generated.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT Publish - correct syntax?

#10 Post by TD-er » 29 Dec 2020, 16:14

For debugging it is also useful to send the values to log like this:

Code: Select all

logentry,'Event Values: %eventvalue1% %eventvalue2% %eventvalue3% %eventvalue4%'

pw444
Normal user
Posts: 155
Joined: 23 Apr 2019, 15:34
Location: rio de janeiro

Re: MQTT Publish - correct syntax?

#11 Post by pw444 » 29 Dec 2020, 17:45

Thank you! It's working as intended..... MQTT, etc... all working. Nice support guys :)

Now i only have to make sure that user get's notification when finnished.

Code: Select all

// setup
// http://192.168.1.7/control?cmd=event,OffAll_Off
// http://192.168.1.7/control?cmd=event,On24_On
// nothing - status idle - no notification - status working 0
// started manually - set VAR#0  1 - remote set VAR#0 0
// started - mqtt - remotelly - set status working 1 set remote 1

On System#Boot do
// GPIO,2,0  // status led - inversed - 1 OFF, 0 ON
  Monitor GPIO,4  // D2
  let,1,0
endon

on MQTT#Connected do
  event,publishstatus
endon

// loop

// events triggered by http

On B58 do               // B58 - gpio 14 - D5 
  pulse,14,1,500
  pulse,2,0,500
  let,1,1
endon

On B24 do               // B24 - gpio 12 - D6
  pulse,12,1,500
  pulse,2,0,500
  let,1,1
endon 

On BOFF do              // BOFF - gpio 13 - D7
  pulse,13,1,500
  pulse,2,0,500
//  Publish,%sysname%/BOFF/status/0
endon

// events triggered by MQTT

on MQTTIN#BOFF do       // cancelled
  if %eventvalue%=1
     pulse,13,1,500
     pulse,2,0,500
     Publish,%sysname%/BOFF/status,0
  endif
endon

on MQTTIN#B58 do       // brew 5-8
  if %eventvalue%=1
     pulse,14,1,500
     pulse,2,0,500
     Publish,%sysname%/B58/status,0
     let,1,1
  endif
endon

on MQTTIN#B24 do        // brew 2-4
  if %eventvalue%=1
     pulse,12,1,500
     pulse,2,0,500
     Publish,%sysname%/B24/status,0
     let,1,1
  endif
endon

on VACC#status do
  if %eventvalue%=1  // status = 1
    if %v1%=1        // status = 1 var1 = 1
       logentry,"Working"
    endif
  else               // status = 0
    if %v1%=1        // status = 0 var1 =1
       let,1,0
       logentry,"Finished"
       // trigger to notify - finnished
    endif 
  endif
endon

TD-er
Core team member
Posts: 8749
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT Publish - correct syntax?

#12 Post by TD-er » 29 Dec 2020, 21:49

You can send an email, or attach a speaker (and using the buzzer) or use another GPIO pin to trigger a siren :)

Post Reply

Who is online

Users browsing this forum: No registered users and 21 guests