Page 1 of 1

MQTT Publishing by boot

Posted: 10 May 2019, 14:55
by pw444
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

Re: MQTT Publishing by boot

Posted: 10 May 2019, 15:22
by grovkillen
At boot the connection to your network is not ready. Use the event MQTT#Connected instead.

https://espeasy.readthedocs.io/en/lates ... vents.html

Re: MQTT Publishing by boot

Posted: 10 May 2019, 15:45
by pw444
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!

Re: MQTT Publishing by boot

Posted: 10 May 2019, 16:45
by grovkillen
Yeah that is correct. But then you should add a timer at boot and when that one trigger publish.

Re: MQTT Publishing by boot

Posted: 10 May 2019, 23:45
by pw444
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

Re: MQTT Publishing by boot

Posted: 11 May 2019, 10:10
by grovkillen
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

Re: MQTT Publishing by boot

Posted: 11 May 2019, 18:31
by pw444
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

Re: MQTT Publishing by boot

Posted: 11 May 2019, 19:18
by grovkillen
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.

Re: MQTT Publishing by boot

Posted: 14 May 2019, 16:47
by pw444
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

Re: MQTT Publishing by boot

Posted: 14 May 2019, 17:06
by grovkillen
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

Re: MQTT Publishing by boot

Posted: 14 May 2019, 19:12
by pw444
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...

Re: MQTT Publishing by boot

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

Event,pb01#onoff

mosquitto_pub -h 192.168.1.6 -t "/Lamp-0101/cmd" -m "Event,pb01#onoff"

Re: MQTT Publishing by boot

Posted: 14 May 2019, 19:40
by pw444
thx!!!

Re: MQTT Publishing by boot

Posted: 15 May 2019, 21:20
by waspie
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?

Re: MQTT Publishing by boot

Posted: 15 May 2019, 21:31
by grovkillen
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.