Page 1 of 1

Send MQTT to different topics?

Posted: 19 Mar 2024, 11:23
by PieterS
I have 2 energy-counters with Modbus (Eastron SDM630 and Eastron SDM120) in my garage and want to connect them together in one bus and to one controller (Lolin NodeMcu).
Is it possible to send data to different topics through MQTT from one NodeMcu to the same broker? One topic (for the SDM603) is domoticz/in and the second meter (SDM120M) should send to topic energy/goodwe.
In the software (Build: ESP_Easy_mega_20231130_energy_ESP8266_4M1M Nov 30 2023) I created a second MQTT controller with different topicname but that seems to be not allowed... Error: One one mqtt controller should be active. :?

Is there a solution for this problem? In tab Devices I can select a controller... :evil:

Or build another NodeMcu with another configuration of the topic: energy/goodwe

Re: Send MQTT to different topics?

Posted: 19 Mar 2024, 12:22
by TD-er
You can send whatever you like via rules.
For example act on the event of a new measurement and send all values using the publish command.

See also documentation for formatting domoticz-syntax formatted messages: https://espeasy.readthedocs.io/en/lates ... er-parsing

Assuming your task is called "SDM2" and you have checked the checkbox to send all values in a single event (checkbox in the task config page)

Code: Select all

on sdm2#all do
  publish,"energy/goodwe/1",%eventvalue1%
  publish,"energy/goodwe/2",%eventvalue2%
  publish,"energy/goodwe/3",%eventvalue3%
  publish,"energy/goodwe/4",%eventvalue4%
endon
Or if you need the values all in a single message:

Code: Select all

  publish,"energy/goodwe","%eventvalue1%,%eventvalue2%,%eventvalue3%,%eventvalue4%"

Re: Send MQTT to different topics?

Posted: 19 Mar 2024, 16:24
by PieterS
Thnx Gijs for quick reply and examples.

I forgot to tell that data from the SDM120 must be sent via broker to 2 different destinations. The one to Domoticz is clear to me.

This is the rule:

Code: Select all

On SDM_Goodwe#All Do  
  Publish,energy/goodwe,'{"command":"udevice","idx":2506,"nvalue":0,"svalue":"%eventvalue1%"}' 
  Publish,energy/goodwe,'{"command":"udevice","idx":2507,"nvalue":0,"svalue":"%eventvalue2%"}' 
  Publish,energy/goodwe,'{"command":"udevice","idx":2508,"nvalue":0,"svalue":"%eventvalue3%"}'  
  Publish,energy/goodwe,'{"command":"udevice","idx":2509,"nvalue":0,"svalue":"%eventvalue4%"}'
Endon
The second destination is to Node-Red...
I am able to simulate that proces by sending a message by Publish in MQTT Explorer. The data is picked up by the flow and send correctly to PVoutput.org.. :P

Hopefully tomorrow I recieve the new SDM120M and in the mean time I can figure out how to edit the MQTT-node in NodeRED.. Or create a rule in ESPeasy for NodeRED..

The message I publish is for example:

Code: Select all

{{"pvpowerout": 130, "pvenergytoday": 2400}}


I must rearrange that in the rule I guess with some extra text. That is the goal for this evening. :)

Re: Send MQTT to different topics?

Posted: 19 Mar 2024, 16:40
by TD-er
Just as a tip, you can easily test your strings by sending them to log using the command "logentry".
This also does all string replacements.

Code: Select all

//Publish,energy/goodwe,'{"command":"udevice","idx":2509,"nvalue":0,"svalue":"%eventvalue4%"}'
  logentry,'{"command":"udevice","idx":2509,"nvalue":0,"svalue":"%eventvalue4%"}'

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 14:53
by PieterS
Sorry for delay. I wrote this script in Rules:

Code: Select all

On SDM120_Goodwe#All Do      // Receives 4 arguments, 1st one is already sent to the controller, so can be ignored
  Publish,domoticz/in,'{"command":"udevice","idx":2515,"nvalue":0,"svalue":"%eventvalue2%"}' // Current ESP32
  Publish,domoticz/in,'{"command":"udevice","idx":2521,"nvalue":0,"svalue":"%eventvalue3%"}'  //Power ESP32
  Let,1,[SDM120_Goodwe#W]
  Let,2,([SDM120_Goodwe#Total_kWh]*1000)
  Publish,domoticz/in,'{"command":"udevice","idx":2520,"nvalue":0,"svalue":"[var#2];[var#1]"}' //Total kWh; Power
Endon

On SDM120_Goodwe#All do
  Publish,"energy/goodwe/V",%eventvalue1%
  Publish,"energy/goodwe/A",%eventvalue2%
  Publish,"energy/goodwe/W",%eventvalue3%
  Publish,"energy/goodwe/Total_kWh",%eventvalue4%

//Publish,energy/goodwe,'{"command":"udevice","idx":2509,"nvalue":0,"svalue":"%eventvalue4%"}'
  logentry,'{"%eventvalue3%"}'
Endon
The first part of this Rule to Domoticz works. As long as in the Tab Controllers Protocol MQTT is enabled. Then it updates the sensors in the dashboard. As soon as I uncheck the line with Enabled the data is registered in the log of ESPeasy but not send to Domoticz.. :? The sensors are not updated...

Code: Select all

944490736: EVENT: SDM120_Goodwe#All=230.50,2.25,510.70,410.24
944490745: ACT  : Publish,domoticz/in,'{'command':'udevice','idx':2515,'nvalue':0,'svalue':'2.25'}'
944490750: ACT  : Publish,domoticz/in,'{'command':'udevice','idx':2521,'nvalue':0,'svalue':'510.70'}'
944490757: ACT  : Let,1,510.70
944490764: ACT  : Let,2,(410.24*1000)
944490771: ACT  : Publish,domoticz/in,'{'command':'udevice','idx':2520,'nvalue':0,'svalue':'410240;510.7'}'
944493721: WD   : Uptime 15742  ConnectFailures 0 FreeMem 201720 EthSpeedState Link Up Full Duplex 100Mbps ETH status: Conn. IP Init
I had in mind that there is no need to configure and enable a protocol in the Controller tab.

And my second question:

When I use Rules it should be possible to send to different Topics at once. As I did in the above script. But up till now I did not succeed. Maybe somebody can help me out?
At the moment I am able to send the data to Domoticz and that application sends the data to Node-Red. That is a long way around and a single point of failure.. On top of that the values of sensors are converted from number to string.. :o Not a real problem but not necessary.

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 17:21
by TD-er
The publish command uses the settings of the first enabled MQTT controller.
So if you uncheck the enabled checkbox for that controller, then the publish command will not use those settings and thus not send anything to the MQTT broker defined in the first controller settings.
(if no MQTT controller is enabled, then publish will not do anything)

You can send to different topics just fine, as long as the user credentials you use for connecting to the MQTT broker allows this user to publish to other topics as well.
The default config of Mosquitto allows this if I'm not mistaken, but it can be limited per user account, so worth looking into.
N.B. if the user is not allowed to publish into another topic, then the MQTT broker will disconnect ESPEasy and you will see in the logs ESPEasy tries to reconnect to the MQTT broker.

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 19:34
by PieterS
Thnx Gijs, I am not an expert in MQTT...
The publish command uses the settings of the first enabled MQTT controller.
Ok. I did find that out. Publish command needs a destination like IP and Port for the broker.

Q 1: What about the Topics? I filled that out in the Controller. Do I wipe them because I can send them in the Rule command: Publish,domoticz/in?
You can send to different topics just fine, as long as the user credentials you use for connecting to the MQTT broker allows this user to publish to other topics as well.
I do not use User Credentials in for the broker.

Q 2: Is there something wrong according tot the Publish command in the syntax of the above rule-file?

Q 3: For my knowledge: When I write a command in the Rule-file then the topic after the Publish-command overrules the Publish-setting in the Controller?
like:

Code: Select all

Publish,energy/goodwe,%eventvalue3%
While in the Controller the setting is:
Image

I missed info about send data to different topics in the wiki https://espeasy.readthedocs.io/en/lates ... Rules.html

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 20:33
by TD-er
The subscribe/publish topics in the controller are only used when sending via a task directly to the controller.
So if you link the controller in a task (near the bottom of the page of any task config page), then the task will directly send to the controller and then will use the publish config of the controller.

When using the publish command, the topic as given with that command will be used, nothing else.

About your rules...
Only the first block will be run as they both act on the same event.

So you can simply comment out these 2 lines to test:

Code: Select all

On SDM120_Goodwe#All Do      // Receives 4 arguments, 1st one is already sent to the controller, so can be ignored
  Publish,domoticz/in,'{"command":"udevice","idx":2515,"nvalue":0,"svalue":"%eventvalue2%"}' // Current ESP32
  Publish,domoticz/in,'{"command":"udevice","idx":2521,"nvalue":0,"svalue":"%eventvalue3%"}'  //Power ESP32
  Let,1,[SDM120_Goodwe#W]
  Let,2,([SDM120_Goodwe#Total_kWh]*1000)
  Publish,domoticz/in,'{"command":"udevice","idx":2520,"nvalue":0,"svalue":"[var#2];[var#1]"}' //Total kWh; Power
//Endon

//On SDM120_Goodwe#All do
  Publish,"energy/goodwe/V",%eventvalue1%
  Publish,"energy/goodwe/A",%eventvalue2%
  Publish,"energy/goodwe/W",%eventvalue3%
  Publish,"energy/goodwe/Total_kWh",%eventvalue4%

//Publish,energy/goodwe,'{"command":"udevice","idx":2509,"nvalue":0,"svalue":"%eventvalue4%"}'
  logentry,'{"%eventvalue3%"}'
Endon

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 21:45
by PieterS
Thnx Gijs for your hints!

Removing the eventrigger was the trick!

I removed the quotes around the topicname to make it the same as the first part with domoticz/in

Image

You solved another problem over here! And send data from the ESP directly to Node-Red makes the flow easier. :D

Re: Send MQTT to different topics?

Posted: 02 Jun 2024, 22:24
by TD-er
Great!

N.B. you may want to keep an eye of how many messages are being sent in a burst to the MQTT broker.
As when processing rules on a single event, the publish calls are made in very quick succession.
So the queue depth should be minimal 10 and you can also lower the minimum send interval if you experience delays or lost messages. (only when you actually experience these issues)