mqtt - how to?

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 - how to?

#1 Post by pw444 » 04 Sep 2020, 00:15

Hya!

Espeasy

- controller protocol - home assistante (openhab) mqtt
- controller client id: %sysname%_%unit%
- Controller Subscribe: %sysname%_%unit%/#
- Controller Publish: %sysname%_%unit%/%tskname%/%valname%
- Controller LWT Topic: home-assistant/window/availability
- LWT Connect Message: payload_available
- LWT Disconnect Message: payload_not_available

MQTT Explorer shows: rl1c_0/relay/onoff = 1

Code: Select all

http://rl1c_0/control?cmd=gpio,0,0

{
"log": "GPIO 0 Set to 0",
"plugin": 1,
"pin": 0,
"mode": "output",
"state": 1
}

Ok
MQTT Explorer shows: rl1c_0/relay/onoff = 0

Code: Select all

http://rl1c_0/control?cmd=gpio,0,1

{
"log": "GPIO 0 Set to 1",
"plugin": 1,
"pin": 0,
"mode": "output",
"state": 0
}

Ok
MQTT Explorer shows: rl1c_0/relay/onoff = 1

So, it works perfect, in theory, because when a mqtt client changes the onoff value, MQTT explorer shows the change, but Espeasy device simple does nothing. Change only happens on broker.

May i ask for help in being educated in how to make Espeasy device also read and behave as expected?

Thx in advance.
Last edited by pw444 on 04 Sep 2020, 19:11, edited 1 time in total.

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

Re: mqtt - how to?

#2 Post by Ath » 04 Sep 2020, 07:42

What type of ESP are you using? (ESP8266, ESP8285 or ESP32, any specific hardware like a Sonoff or Shelly?)
How is the hardware on the ESP device configured? I.e., what GPIO's are the relay and button(s), if any, connected to?
What other hardware is connected to the ESP, like temperature sensors?

Edit: What version of ESPEasy are you using?
/Ton (PayPal.me)

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

Re: mqtt - how to?

#3 Post by pw444 » 04 Sep 2020, 19:17

Ath wrote: 04 Sep 2020, 07:42 What type of ESP are you using? (ESP8266, ESP8285 or ESP32, any specific hardware like a Sonoff or Shelly?)
How is the hardware on the ESP device configured? I.e., what GPIO's are the relay and button(s), if any, connected to?
What other hardware is connected to the ESP, like temperature sensors?

Edit: What version of ESPEasy are you using?
- esp8266-01 - relay to gpio0
- devices -switch input swtich
name - relay
switch type - switch
switch button type - normal switch

Binary: ESP_Easy_mega_20200829_normal_ESP8266_1M

http://rl1c_0.fqdn/control?cmd=gpio,0,0 - works

question is: how to read from mqtt broker and perform the action. publishing is being done.

Thx!

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

Re: mqtt - how to?

#4 Post by Ath » 05 Sep 2020, 10:13

An implicit question, that maybe wasn't quite so obvious, hasn't been answered clearly: Does the relay switch on when you send the gpio,0,1 command from the tools page?, because if that isn't working you have electrical issues that need to be solved first.
Next question will be if the state-change of the switch is detected by ESPEasy.

If you can show a schematic drawing here, that would help too. We have a lot of imagination, but that doesn't always help to understand your setup.
/Ton (PayPal.me)

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

Re: mqtt - how to?

#5 Post by pw444 » 05 Sep 2020, 18:05

Ath wrote: 05 Sep 2020, 10:13 An implicit question, that maybe wasn't quite so obvious, hasn't been answered clearly: Does the relay switch on when you send the gpio,0,1 command from the tools page?
Yes, relay is triggered by sending gpio,0,1 and release by sending gpio,0,0.

The question is how to make espeasy read the status change by mqtt (sent from homeassistamt)

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

Re: mqtt - how to?

#6 Post by pw444 » 07 Sep 2020, 13:41

any comments? thx in advance.

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

Re: mqtt - how to?

#7 Post by TD-er » 07 Sep 2020, 15:08

Have you tried MQTTimport?

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

Re: mqtt - how to?

#8 Post by pw444 » 07 Sep 2020, 18:16

How does it work?

from my understanding, once you subscribe the topic, the same you publish, action should be performed by change, or am i wrong?

espeasy pubish the state, by mqtt broker you see the change, mosquitto_pub from command line makes the change at the broker, no problems. but espeasy does not reads the change.

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

Re: mqtt - how to?

#9 Post by TD-er » 07 Sep 2020, 20:58

ESPEasy has a very simple structure:
- Plugin: Interact with something (usually hardware) and may result in 0 .. 4 values (e.g. temp/hum/etc.)
- Task: An instance of a plugin, may send data to 0 .. 3 controllers.
- Controller: Receive data from a plugin (or rules) and send it to some service. (e.g. MQTT broker)

Now you may already see the problem here, as the controller does only send out data.
Some controllers have support for some plugins to also receive data.
For example Domoticz MQTT can also handle state changes for plugins like the switch plugin.
But this is very specific for the platform and also very specific for only a few plugins.

MQTT import is a special plugin (thus may yield values) which can subscribe to a topic and receive values.
It is rather limited in that it can only handle very simple formatted published messages. So don't expect to parse JSON data structures for example.

N.B. the MQTT import plugin does use the credentials and broker settings from the first enabled MQTT controller.

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: mqtt - how to?

#10 Post by fnees » 07 Sep 2020, 22:25

pw444 wrote: 04 Sep 2020, 19:17
question is: how to read from mqtt broker and perform the action. publishing is being done.
The obvious answer would be:
Just set the command topic on the broker to rl1c_0.fqdn/gpio/0 and use payload 0 or 1.

Sorry if I did not get the point though...

Regards,
Fab

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

Re: mqtt - how to?

#11 Post by pw444 » 07 Sep 2020, 23:01

fnees wrote: 07 Sep 2020, 22:25
pw444 wrote: 04 Sep 2020, 19:17
question is: how to read from mqtt broker and perform the action. publishing is being done.
The obvious answer would be:
Just set the command topic on the broker to rl1c_0.fqdn/gpio/0 and use payload 0 or 1.

Sorry if I did not get the point though...

Regards,
Fab
Thank you! right to the point. Worked as expected.


So, to make it simple, i renamed by devices:

from relay to gpio and also from onoff to 0 and so wgen i look the mqtt explorer, i see only one topic for the device - gpio

Thx again.

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

Re: mqtt - how to?

#12 Post by pw444 » 08 Sep 2020, 22:25

TD-er wrote: 07 Sep 2020, 20:58 ESPEasy has a very simple structure:
- Plugin: Interact with something (usually hardware) and may result in 0 .. 4 values (e.g. temp/hum/etc.)
- Task: An instance of a plugin, may send data to 0 .. 3 controllers.
- Controller: Receive data from a plugin (or rules) and send it to some service. (e.g. MQTT broker)

Now you may already see the problem here, as the controller does only send out data.
Some controllers have support for some plugins to also receive data.
For example Domoticz MQTT can also handle state changes for plugins like the switch plugin.
But this is very specific for the platform and also very specific for only a few plugins.

MQTT import is a special plugin (thus may yield values) which can subscribe to a topic and receive values.
It is rather limited in that it can only handle very simple formatted published messages. So don't expect to parse JSON data structures for example.

N.B. the MQTT import plugin does use the credentials and broker settings from the first enabled MQTT controller.

Ok, so i got the answer that explains my doubts. The espeasy ha / openhab controller only publishes, but do not subscribe, so, any change published by others to the espeasy published topic is not received by espeasy.

In this case either the syntax espeasy/gpio/# paylod or mqtt import which will subscribe and monitor the desiderd topic.

So, if one espeasy handles more than one switch, each will be published but to be monitored, mqtt import, if not monitored, the espeasy/gpio/# payload, which means two topics, one being publiched by espeasy, readed by other controllers and the other controllers publish the espeasy/gpio/# syntax to be executed by espeasy.

Is the above understanding correct?


can mqtt topics be subscribed using rules?

fnees
New user
Posts: 6
Joined: 31 Aug 2020, 17:26

Re: mqtt - how to?

#13 Post by fnees » 08 Sep 2020, 23:20

Interesting, maybe the great guys here could help clarify this.
I always thought it does subscribe to whatever you configure here:
Controller Subscribe: %sysname%/#
I guess it just doesnt act on the topics specified on (most) plugins...
It will act when the topic is (eg) %sysname%/%command%
You can see the list of commands here:
https://www.letscontrolit.com/wiki/inde ... _Reference
Ultimately commands and plugins are oriented to the devices connected, you just have to figure your way out.
I have no problem using different topics depending the direction of the mqtt message....

Best,
Fab

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

Re: mqtt - how to?

#14 Post by TD-er » 08 Sep 2020, 23:44

Please use the (bit) more up-to-date reference in ReadTheDocs: https://espeasy.readthedocs.io/en/lates ... mmand.html

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

Re: mqtt - how to?

#15 Post by pw444 » 09 Sep 2020, 13:18

pw444 wrote: 08 Sep 2020, 22:25
Is the above understanding correct?
i just wish to be sure my understanding is correct, that subscribe actions are plugin dependent, and that it does not work for mqtt ha / openhab.

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

Re: mqtt - how to?

#16 Post by TD-er » 09 Sep 2020, 14:16

I think your understanding is somewhat correct.

Like I said, only some MQTT controllers can handle incoming messages for some plugins/tasks.

For all other incoming data from MQTT, you should use the MQTT import plugin.
This plugin can subscribe to a topic and receive only simple messages, like strings that can be read as numericals (int/float) values.

And about MQTT topic subscription, you can also subscribe to a set of topics at once, using the # in a topic.
I strongly suggest to read the tutorials on HiveMQ: https://www.hivemq.com/blog/how-to-get- ... with-mqtt/

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

Re: mqtt - how to?

#17 Post by pw444 » 14 Sep 2020, 15:15

thank you.

if i publich the topic rl1c_0/relay/onoff=1 , how would the mqtt import syntax be to change the state and make the relay (gpio0) trigger on/off?

relay is the device name (gpio0) and onoff the state.

Thx in advance

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

Re: mqtt - how to?

#18 Post by TD-er » 14 Sep 2020, 15:26

The MQTT import plugin does only yield values.

The controllers themselves do also monitor their subscribe topics appended with "/cmd"
So if you subscribe to "rl1c_0/relay/" you should publish a command to the topic "rl1c_0/relay/cmd/"
Whatever is published there, is picked up by the controller listening to it and thus executed.

Like I said, I don't use this way myself, but that's what I took out of the documentation.

I just checked in the source code and at least "Home Assistant (openHAB) MQTT" does accept commands like this.

For Domoticz I can imagine it would be a bit tricky as all Domoticz connected nodes subscribe to the same topic.

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

Re: mqtt - how to?

#19 Post by pw444 » 14 Sep 2020, 20:00

long story short, mqtt import does not works for HA.


there is a topic: https://community.home-assistant.io/t/e ... a/157415/7

any hint of what is going on and help?

TIA

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

Re: mqtt - how to?

#20 Post by pw444 » 29 Sep 2020, 16:18

any hint?

chemmex
Normal user
Posts: 97
Joined: 15 Feb 2019, 16:18

Re: mqtt - how to?

#21 Post by chemmex » 29 Sep 2020, 23:01

long story short, mqtt import does not works for HA.
in fact, OpenHAB/HA subscribes to the command topic pretty well, you can pass any string payload and then execute it as event in rules

e.g. payload is relay0

Code: Select all

on relay0 do
    gpio,0,0
endon 

martinus
Normal user
Posts: 129
Joined: 15 Feb 2020, 16:57

Re: mqtt - how to?

#22 Post by martinus » 30 Sep 2020, 07:17

At the time of developing support for MQTT it became clear that there was no such thing as a standard syntax for MQTT messages. So soon after the first MQTT controller plugin, additional ones were build to support specific HOME Automation controllers in a limited way. But from there on, it also became clear that this would lead to a large amount of controllers and the initial approach was likely not the best to be flexible enough.

So currently it requires some tweaking and likely some rules to even get a simple light switch working properly.
I had my hopes on this development:
https://homieiot.github.io/specification/
It would lead to a true standard syntax for IOT handling, but that would require all HOME Automation systems to adopt it. Looks like only these systems have some form of support for it:
* openHAB
* HomeAssistant
* Node-RED
And ESPEasy has a controller for it (C014) but is anyone using it?

Until we get a true and widely adopted standard, it looks like getting MQTT to work in some cases could be not so EASY for beginners.

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

Re: mqtt - how to?

#23 Post by pw444 » 01 Oct 2020, 00:57

chemmex wrote: 29 Sep 2020, 23:01
long story short, mqtt import does not works for HA.
in fact, OpenHAB/HA subscribes to the command topic pretty well, you can pass any string payload and then execute it as event in rules

e.g. payload is relay0

Code: Select all

on relay0 do
    gpio,0,0
endon 
Thank you! Can you post an example in how to achieve this? I'm kinda lost.

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

Re: mqtt - how to?

#24 Post by pw444 » 01 Oct 2020, 00:59

martinus wrote: 30 Sep 2020, 07:17 At the time of developing support for MQTT it became clear that there was no such thing as a standard syntax for MQTT messages. So soon after the first MQTT controller plugin, additional ones were build to support specific HOME Automation controllers in a limited way. But from there on, it also became clear that this would lead to a large amount of controllers and the initial approach was likely not the best to be flexible enough.

So currently it requires some tweaking and likely some rules to even get a simple light switch working properly.
I had my hopes on this development:
https://homieiot.github.io/specification/
It would lead to a true standard syntax for IOT handling, but that would require all HOME Automation systems to adopt it. Looks like only these systems have some form of support for it:
* openHAB
* HomeAssistant
* Node-RED
And ESPEasy has a controller for it (C014) but is anyone using it?

Until we get a true and widely adopted standard, it looks like getting MQTT to work in some cases could be not so EASY for beginners.
ESPEasy has a controller for it (C014) - how does it works?

roystervi
New user
Posts: 4
Joined: 12 May 2021, 21:06

Re: mqtt - how to?

#25 Post by roystervi » 12 May 2021, 21:30

Hi all am trying to use RPIEasy to operate some relays for a sprinkler system. The problem am having is getting MQTT over to home assistant.
I tried to use MQTT Explorer to see the topics but I can't seem to PUBLISH messages,
How can I Publish messages in RPIEasy.

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

Re: mqtt - how to?

#26 Post by TD-er » 12 May 2021, 22:39

For RpiEasy, there is a complete subforum: viewforum.php?f=24
Just to make it more clear your question is about RpiEasy and not ESPEasy.

The more generic way of publishing from ESPEasy/RpiEasy is either via a controller, or via the publish command from the rules.
But since it may get a bit specific to which platform you're using to prevent creating a loop (relay state switches, HomeAssistent gets updated, HomeAssistant tries to correct state, etc.) you may be better off asking it in the RpiEasy subforum.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests