Page 1 of 1

mqtt - how to?

Posted: 04 Sep 2020, 00:15
by pw444
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.

Re: mqtt - how to?

Posted: 04 Sep 2020, 07:42
by Ath
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?

Re: mqtt - how to?

Posted: 04 Sep 2020, 19:17
by pw444
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!

Re: mqtt - how to?

Posted: 05 Sep 2020, 10:13
by Ath
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.

Re: mqtt - how to?

Posted: 05 Sep 2020, 18:05
by pw444
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)

Re: mqtt - how to?

Posted: 07 Sep 2020, 13:41
by pw444
any comments? thx in advance.

Re: mqtt - how to?

Posted: 07 Sep 2020, 15:08
by TD-er
Have you tried MQTTimport?

Re: mqtt - how to?

Posted: 07 Sep 2020, 18:16
by pw444
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.

Re: mqtt - how to?

Posted: 07 Sep 2020, 20:58
by TD-er
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.

Re: mqtt - how to?

Posted: 07 Sep 2020, 22:25
by fnees
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

Re: mqtt - how to?

Posted: 07 Sep 2020, 23:01
by pw444
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.

Re: mqtt - how to?

Posted: 08 Sep 2020, 22:25
by pw444
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?

Re: mqtt - how to?

Posted: 08 Sep 2020, 23:20
by fnees
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

Re: mqtt - how to?

Posted: 08 Sep 2020, 23:44
by TD-er
Please use the (bit) more up-to-date reference in ReadTheDocs: https://espeasy.readthedocs.io/en/lates ... mmand.html

Re: mqtt - how to?

Posted: 09 Sep 2020, 13:18
by pw444
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.

Re: mqtt - how to?

Posted: 09 Sep 2020, 14:16
by TD-er
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/

Re: mqtt - how to?

Posted: 14 Sep 2020, 15:15
by pw444
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

Re: mqtt - how to?

Posted: 14 Sep 2020, 15:26
by TD-er
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.

Re: mqtt - how to?

Posted: 14 Sep 2020, 20:00
by pw444
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

Re: mqtt - how to?

Posted: 29 Sep 2020, 16:18
by pw444
any hint?

Re: mqtt - how to?

Posted: 29 Sep 2020, 23:01
by chemmex
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 

Re: mqtt - how to?

Posted: 30 Sep 2020, 07:17
by martinus
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.

Re: mqtt - how to?

Posted: 01 Oct 2020, 00:57
by pw444
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.

Re: mqtt - how to?

Posted: 01 Oct 2020, 00:59
by pw444
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?

Re: mqtt - how to?

Posted: 12 May 2021, 21:30
by roystervi
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.

Re: mqtt - how to?

Posted: 12 May 2021, 22:39
by TD-er
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.