MQTT Publishing by boot

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 Publishing by boot

#1 Post by pw444 » 10 May 2019, 14:55

Hia,

I'm trying to publish the state of the device, as

Code: Select all


On System#Boot do
 let,1,0
 // gpio 2 -> relay state high = relay off
 GPIO,2,1
 Publish,%sysname%/status,Light Off
endon

on pb01#value do
   gpiotoggle,2
   if [VAR#1]=0
    let,1,1 // state on
    Publish,%sysname%/status,Light On
   else
    let,1,0 // state off
    Publish,%sysname%/status,Light Off
   endif
endon

on pb01on do
  GPIO,2,0
  let,1,1
  Publish,%sysname%/status,Light On
endon

on pb01off do
  GPIO,2,1
  let,1,0
  Publish,%sysname%/status,Light Off
endon

on pb01sw do
  gpiotoggle,2  
  if [VAR#1]=0
    let,1,1 // state on
    Publish,%sysname%/status,Light On
   else
    let,1,0 // state off
    Publish,%sysname%/status,Light Off
   endif   
endon
,

but by mqtt-explorer, the publish of on system#boot is not presented, showing:
192.168.1.6 > > Lamp-0101> status LWT = Connected

By Controller, i have:
openhab mqtt
ip: 192.168.1.6
Controller Subscribe: /%sysname%/#
Controller Publish: /%sysname%/%tskname%/%valname%
enabled: yes

But none of those is published either.

Any hint?

THX in advance

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#2 Post by grovkillen » 10 May 2019, 15:22

At boot the connection to your network is not ready. Use the event MQTT#Connected instead.

https://espeasy.readthedocs.io/en/lates ... vents.html
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#3 Post by pw444 » 10 May 2019, 15:45

Thank you.

By MQTT#Connected will it perforn once after boot or every time when connecting, i.e, a netorwork disconnection, but no reboot and change of state?

And regarding controllers:

By Controller, i have:
openhab mqtt
ip: 192.168.1.6
Controller Subscribe: /%sysname%/#
Controller Publish: /%sysname%/%tskname%/%valname%
enabled: yes

But none of those is published either

Thx!

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#4 Post by grovkillen » 10 May 2019, 16:45

Yeah that is correct. But then you should add a timer at boot and when that one trigger publish.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#5 Post by pw444 » 10 May 2019, 23:45

Thx!

i did the following:

Code: Select all

// pb01   - gpio 0 - D5
// output - gpio 2 - D3
// boots off

On System#Boot do
 let,1,0
 // gpio 2 -> relay state high = relay off
 GPIO,2,1 
endon

on MQTT#Connected do
 event,publishstatus
endon

on pb01#onoff do
   gpiotoggle,2
   if [VAR#1]=0
    let,1,1 // state on
    event,publishstatus
   else
    let,1,0 // state off
    event,publishstatus
   endif
endon

on pb01on do
  GPIO,2,0
  let,1,1
  event,publishstatus
endon

on pb01off do
  GPIO,2,1
  let,1,0
  event,publishstatus
endon

on pb01sw do
  gpiotoggle,2  
  if [VAR#1]=0
    let,1,1 // state on
    event,publishstatus
   else
    let,1,0 // state off
    event,publishstatus
   endif   
endon

On publishstatus do
   Publish,%sysname%/pb01/onoff,%v1%
endon
so, by network reconnection, i have the actual state and by reboot, the default state, it's working fine.

by mqtt-explorer, all ok, except that the var is showed as:
onoff = 0.00

Is there a way to cut the decimals, so i have onoff = 0, instead of onoff = 0.00?

TIA

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#6 Post by grovkillen » 11 May 2019, 10:10

You can try the [VAR#1] syntax since the %v1% will convert the float value to a string. If that still add the decimals you could do it like this:

Code: Select all

On publishstatus do
  If [VAR#1]=1
    Publish,%sysname%/pb01/onoff,1
  Else
    Publish,%sysname%/pb01/onoff,0
  Endif
endon
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#7 Post by pw444 » 11 May 2019, 18:31

Ok, [VAR#1] also ouputs 1.00 or 0.00, so accepted your suggesttion ;-) Thx a lot.

BTW, by Controller, i have:
openhab mqtt
ip: 192.168.1.6
Controller Subscribe: /%sysname%/#
Controller Publish: /%sysname%/%tskname%/%valname%
enabled: yes

Why does my mqtt server does not sows the %sysname% by subscription?
and what is the controller publish is for?

TIA

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#8 Post by grovkillen » 11 May 2019, 19:18

That's the syntax used by the task devices when the "send to controller is checked". And please remember that the first / is also part of the topic. But it's considered bad practice to use a leading front slash.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#9 Post by pw444 » 14 May 2019, 16:47

thx!

just one stupid question.

using my browser, i can control it with i.e. http://192.168.1.5/control?cmd=event,pb01sw

how could you do the same using the mqtt broker (mosquito)?

TIA

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#10 Post by grovkillen » 14 May 2019, 17:06

You post to the topic of your device and add the extra level "CMD" and send the command as the payload. Please read the docs: https://espeasy.readthedocs.io/en/lates ... mmand.html
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#11 Post by pw444 » 14 May 2019, 19:12

Yes, i did read it, but could not find how, that's why i asked for an example based on the rules, so i can figure out.

MQTT <MQTT subscribe template>/cmd with payload: <command>

i was able so far to figure out:

mosquitto_pub -h 192.168.1.6 -t "/Lamp-0101/gpio/2" -m "1"

what i'm asking is how to use the:

on pb01#onoff
on pb01on do
on pb01off do
on pb01sw do

i'm not finding out how...

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#12 Post by grovkillen » 14 May 2019, 19:35

The command would then be

Event,pb01#onoff

mosquitto_pub -h 192.168.1.6 -t "/Lamp-0101/cmd" -m "Event,pb01#onoff"
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT Publishing by boot

#13 Post by pw444 » 14 May 2019, 19:40

thx!!!

waspie
Normal user
Posts: 127
Joined: 09 Feb 2017, 19:35

Re: MQTT Publishing by boot

#14 Post by waspie » 15 May 2019, 21:20

grovkillen wrote: 14 May 2019, 19:35 The command would then be

Event,pb01#onoff

mosquitto_pub -h 192.168.1.6 -t "/Lamp-0101/cmd" -m "Event,pb01#onoff"
i don't have to do this by mqtt import plugin?

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT Publishing by boot

#15 Post by grovkillen » 15 May 2019, 21:31

No, the MQTT import it's only used to import float values (numbers). You can of course use values to trigger rules but that is not necessary in this case.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests