Mqtt commands list for ESPeasy

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Mqtt commands list for ESPeasy

#1 Post by hoeby » 16 Oct 2022, 19:37

Maybe i haven't searched good enough.
But with the wiki i can't find the answer.
Also with the search function on this forum, i don't find the answer.

Is it possible to send a mqtt message to espeasy and get a mqtt message back with information?
What has the topic to be (the one i put in subscribe?) with which payload?
I am looking for sending a message and get the device and/or gpio information back?

Also with switching a gpio.
I now do this by nodered and setup the espeasy with domoticz helper.
Sending a domoticz style mqtt message, lets the espeasy switch the output.
But is it possible to send a message, without using the third party setup?

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

Re: Mqtt commands list for ESPeasy

#2 Post by Ath » 16 Oct 2022, 19:54

That's not how MQTT is supposed to work. MQTT works by sending out the data/event by the device where that event is available, to the broker, and anyone that has subscribed to that event (topic) will receive it. These free tutorials have taught me a lot on how MQTT works.

From an ESPEasy perspective, you can configure a MQTT Controller, and on each Device configuration (aka Task) you can enable that controller for the value to be sent out, for everyone to consume.
When subscribing to MQTT topics is desired, there is the MQTT Import plugin (P037) that is quite versatile in receiving any topic, for you to respond to in Rules.

When using Domoticz and a MQTT broker (often mosquitto) there is also P029 to directly control a GPIO pin from there.
When using the HA/OpenHAB MQTT controller, you can use any topic ending in /cmd to send commands to ESPEasy, as documented here

The entire list of available plugins is documented here
/Ton (PayPal.me)

hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Re: Mqtt commands list for ESPeasy

#3 Post by hoeby » 16 Oct 2022, 20:24

I think my question was wrong written.
I think i know how mqtt works. But couldn't find the command for espeasy.

I helped to switch from domoticz to openHab.
This is what i have know in a nodered function node and works. It switches gpio 0

Code: Select all

msg.topic = "Sonoff_indoor-plug/cmd";
msg.payload = "gpio,0,1";
return msg;
The only thing left what i am looking for is a mqtt command, that if the espeasy receives it, it publish the status of gpio 0 back to mqtt.
Just like changing the gpio, i get a reply with topic Sonoff_indoor-plug/Dummy/state
how to get the state of gpio 0 (or Dummy) without switching it, is that possible?

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

Re: Mqtt commands list for ESPeasy

#4 Post by Ath » 16 Oct 2022, 20:39

Ok, if you want it 'on demand' you could add a rule that publishes the current state for gpio 0, or you could respond to every state-change of gpio 0 to publish that:

1) On demand:

Code: Select all

on getGpioState do
  let 200,%eventvalue1|-1%
  if %v200%>-1
    publish,Sonoff_indoor-plug/gpio[int#200#D2]/state,[plugin#gpio#pinstate#%v200%]
  endif
endon
to get that rule to publish the state for gpio 0 you should send this command:

Code: Select all

msg.topic = "Sonoff_indoor-plug/cmd";
msg.payload = "event,getGpioState=0";
return msg;
By replacing the 0 by any other valid gpio number you can get the state for any of them. The topic includes the gpio number (%v200%)
Edit: Formatted gpio to 2 digits, with 0 prefilled.

-----------------------

2) Pinstate driven:

Code: Select all

on system#boot do
  monitor gpio,0
endon
on gpio#0 do
  publish,Sonoff_indoor-plug/gpio0/state,%eventvalue1%
endon
NB: Untested!
/Ton (PayPal.me)

hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Re: Mqtt commands list for ESPeasy

#5 Post by hoeby » 16 Oct 2022, 22:27

Good to know that i need an event for it, and that there is not a standard payload which give a status back

I tried your version, but it always gives a 1 back.
Modified some things and this works
If i no problem that i put 3 times %eventvalue1% in the code.
Or is it better to make a variable in it, which is the %eventvalue1%, and replace the 3x %eventvalue1% by the variable?

Code: Select all

on getGpioState do
    if %eventvalue1%>-1
      publish,Sonoff_indoor-plug/gpio[%eventvalue1%]/state,[plugin#gpio#pinstate#%eventvalue1%]
    endif
endon

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

Re: Mqtt commands list for ESPeasy

#6 Post by TD-er » 16 Oct 2022, 23:32

[] are used to refer to taskvalues and [var#XXX] or [int#XXX].
%...% are used to access system variables
N.B. sometimes you cannot use [] notation so for these we also have the same variables as [var#XXX} made accessible via %vXXX%

Anyway, you should not use [] wrapping %eventvalue1%

hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Re: Mqtt commands list for ESPeasy

#7 Post by hoeby » 18 Oct 2022, 07:10

Tried some thing, but i can't get it to work with variable.

The problems is in this part

Code: Select all

[plugin#gpio#pinstate#%v200%]
The variable in that part isn't filled in, it keeps it as plain text.
Also tried the other variable option, without luck

Code: Select all

[plugin#gpio#pinstate#%[int#200#d2]]
This one works, but then i have a system variable between the [ ]

Code: Select all

[plugin#gpio#pinstate#%eventvalue1%]
are there more options, to get the variable in the [plugin#gpio#pinstate]?

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

Re: Mqtt commands list for ESPeasy

#8 Post by Ath » 18 Oct 2022, 07:42

hoeby wrote: 18 Oct 2022, 07:10 The problems is in this part

Code: Select all

[plugin#gpio#pinstate#%v200%]
The variable in that part isn't filled in, it keeps it as plain text.
That is supposed to work, used that many times, as the system variable is processed before the [] enclosed task variable
hoeby wrote: 18 Oct 2022, 07:10 Also tried the other variable option, without luck

Code: Select all

[plugin#gpio#pinstate#%[int#200#d2]]
That won't work, having nested [] task variables. And the % shouldn't be there.
hoeby wrote: 18 Oct 2022, 07:10 This one works, but then i have a system variable between the [ ]

Code: Select all

[plugin#gpio#pinstate#%eventvalue1%]
If this works, then the first option should work as well, as it is essentially the same.

This needs some more investigation, though today I'm a bit short on available time.
/Ton (PayPal.me)

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

Re: Mqtt commands list for ESPeasy

#9 Post by TD-er » 18 Oct 2022, 11:33

Just a few debugging tips:
Sometimes storing stuff in a variable first may be useful instead of writing long expressions.
N.B. This is a must for things that need to be computed.

Another things which can be very useful is to send it to a logentry.

Both "in action":

Code: Select all

let,123,[plugin#gpio#pinstate#%v200%]
logentry,"pinstate: [plugin#gpio#pinstate#%v200%] via let: [int#123]"

hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Re: Mqtt commands list for ESPeasy

#10 Post by hoeby » 18 Oct 2022, 17:29

Edit: have to give it a new try with the info TD-er has giving
Last edited by hoeby on 18 Oct 2022, 21:25, edited 1 time in total.

hoeby
Normal user
Posts: 33
Joined: 19 Sep 2018, 21:03

Re: Mqtt commands list for ESPeasy

#11 Post by hoeby » 18 Oct 2022, 20:31

After trying and trying i have it working.

What did i change:
The variable didn't worked like in the examples.
I noticed that i only can use variable with 1 digit length
When i set this to the 3 digit example, nothing is stored
Maybe this has something to do with the older firmware in the esp (ESP_Easy_mega_20200516)

And the eventvalue was something that gives me errors
This was in one of the first examples, as Ath said, not tested.

Code: Select all

 let 2,%eventvalue1|-1%
I changed it to this, to get the errors out

Code: Select all

 let 2,%eventvalue%
The working code:

Code: Select all

on getGpioState do
    Let,2,%eventvalue%
    if %v2%>-1
        //logentry,'Pin:[int#2#D2], has state:[plugin#gpio#pinstate#%v2%]'
        publish,Sonoff_indoor-plug/gpio[int#2#D2]/state,[plugin#gpio#pinstate#%v2%]
    endif
endon

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

Re: Mqtt commands list for ESPeasy

#12 Post by TD-er » 18 Oct 2022, 20:43

Maybe this has something to do with the older firmware in the esp (ESP_Easy_mega_20200516)
Very likely, as it previously had a limit of upto 16 (??) variables.
Not sure if I already removed that limit about 2.5 years ago.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests