MQTT Import

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

MQTT Import

#1 Post by Eightyeight » 14 Dec 2022, 21:14

Hi

I am having some issue's with relays not doing what I would like them to do. Domoticz controls the setting of some valves and ESPeasy carries out what domoticz would want via http commands. Unfortunately when this command fails or the esp reboots domoticz can assume the wrong state and everything fails so I would like to switch to mqtt.
Now I am having some issues with mqtt imports, I just can't get the plugin working.
Can somebody help with the settings?

this is the example switch butten I would like to implement;
Capture2.PNG
Capture2.PNG (15.75 KiB) Viewed 1627 times
into this
Capture.PNG
Capture.PNG (53.04 KiB) Viewed 1627 times

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

Re: MQTT Import

#2 Post by TD-er » 14 Dec 2022, 21:38

Have you also looked at the Domoticz MQTT Helper plugin?
See: https://espeasy.readthedocs.io/en/lates ... #p029-page

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

Re: MQTT Import

#3 Post by Ath » 14 Dec 2022, 21:44

When you are not switching to the Domoticz MQTT Helper plugin: (that is much easier to set up and 'lighter' in use)

If you click the 'i' button, next to the Device name (and right from the question mark button), that will take you to the documentation, where is explained how to use these options... :)

In short:
- Set "Parse JSON messages" and "Apply filters" to Yes (the page will reload when changing such option)
- Set the JSON Attribute for Topic 'domoticz/out' to 'nvalue'
- Set the filter for topic 1: Name = 'idx', Operand = 'Equals', Value = '363'
- Optionally: Change the 'Value1' entry at Values, near the bottom, to 'testknop'
- You can remove the second topic 'testknop', that's not needed/used

NB: Don't copy the quotes for names or values, they are added here for readability.
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#4 Post by Eightyeight » 15 Dec 2022, 18:10

thanks for the help, much appreciated.
I would like to give the mqtt import a try first because I assume it can do 4 devices at one? I have one ESP32 controlling ~20 relays via PCF8574 so with 1 mqtt import per relay that would fill up taskt quite fast?


Now, I've tried setting it up as per instructions above but unfortunately without success
No matter how many times I flip the "testknop" nothing happens in espeasy.
Any hints on what I did wrong?
Capture3.PNG
Capture3.PNG (59.81 KiB) Viewed 1579 times

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

Re: MQTT Import

#5 Post by Ath » 15 Dec 2022, 19:05

You have configured the Domoticz MQTT Controller on the Controllers tab, right? (That's not explicitly mentioned...)
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#6 Post by Eightyeight » 15 Dec 2022, 19:46

sure
Capture4.PNG
Capture4.PNG (17.75 KiB) Viewed 1568 times
and this is tested and working for sensor data into domoticz

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

Re: MQTT Import

#7 Post by Ath » 15 Dec 2022, 20:28

Ah, now I see I misread the screenshot of your MQTT Import configuration you shared yesterday. "testknop" should indeed be part of the topic, like you initially had it, but I got the impression you entered 2 separate topics. Apologies for the mixup :oops:
Alternative would be to use the wildcard suffix "#", but then the ESP will be quite busy processing and filtering all (and that's quite a bit) domoticz outgoing topics.

With that explicit topic you shouldn't need the filtering either.
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#8 Post by Eightyeight » 15 Dec 2022, 22:43

nice, this is working.
Now I see how "easy" it could be I would have tought I tried this setup already :oops:
thanks again for your help!

for future reference and anybody with the same sort of question;
Attachments
Capture5.PNG
Capture5.PNG (47.43 KiB) Viewed 1549 times

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

Re: MQTT Import

#9 Post by Ath » 15 Dec 2022, 22:57

Great that it's solved :)
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#10 Post by Eightyeight » 16 Dec 2022, 14:27

just as sanity check with random strangers from the internet :D
I now have up to 4 inputs per MQTT import device.
The rules set the corresponding GPIO every 20 seconds

Just to be sure, my goal is to always have the ESP follow domoticz. Even after restarting of domoticz, ESP, router, short power failure, etc the ESP should follow (within a minute or so).
With this setup this should work right?

secondly I assume there is no advantage in adding every individual GPIO as a switch device or am I forgetting something?

Code: Select all

On Rules#Timer=4 do
 timerSet,4,20
LogEntry,'start setting' 

if [MQTT2#KlepKachel]=1
pcfgpio,1,0
else
pcfgpio,1,1
endif


if [MQTT2#KlepRadiatoren]=1
pcfgpio,2,0
else
pcfgpio,2,1
endif
 
if [MQTT2#PompRadiatoren]=1
pcfgpio,3,0
else
pcfgpio,3,1
endif

etc etc for an other 8 devices

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

Re: MQTT Import

#11 Post by TD-er » 16 Dec 2022, 14:43

Please have a look at the documentation on how to format variables:
https://espeasy.readthedocs.io/en/lates ... red-values

Code: Select all

if [MQTT2#KlepKachel]=1
pcfgpio,1,0
else
pcfgpio,1,1
endif
Can be reduced to:

Code: Select all

pcfgpio,1,![MQTT2#KlepKachel]
The `!` does "invert" the value.

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

Re: MQTT Import

#12 Post by Ath » 16 Dec 2022, 14:51

And in addition to the above improvement:

Code: Select all

On Rules#Timer=4 do
 timerSet,4,20
...
Can be written as:

Code: Select all

On System#Boot Do
  LoopTimerSet,4,20 // Repeat the timer every 20 seconds, indefinitely
Endon

On Rules#Timer=4 do
  LogEntry,'start setting'
  ...
Endon
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#13 Post by Eightyeight » 16 Dec 2022, 22:30

Thnaks for the tips.

I do seem to have some issue with the inverting;

below is are two smal parts of the log, switch was on during test and in the first few runs I removed the ! sign.
After reverting back to MCPGPIO,8,![MQTT2#testknop] the value does not change and it appears to try to set the value to !1 instead of 0 and does not switch regardless of the MQTT input.

Is there a syntax issue or is this just not supported?

Code: Select all

36133: IMPT : MQTT fetched json attribute: nvalue payload: 1
1436133: IMPT : [MQTT2#nvalue] : 1.00
1436135: EVENT: domoticz/out/testknop#nvalue=1.00
1436145: EVENT: MQTT2#testknop=1.00
1442439: EVENT: Rules#Timer=1,142
1442447: ACT : LogEntry,'start setting'
1442450: start setting
1442455: ACT : MCPGPIO,8,1
1442459: MCP : port#8: set to 1
1452439: EVENT: Rules#Timer=1,143
1452447: ACT : LogEntry,'start setting'
1452449: start setting
1452454: ACT : MCPGPIO,8,1
1452459: MCP : port#8: set to 1
1462440: EVENT: Rules#Timer=1,144
1462448: ACT : LogEntry,'start setting'
1462450: start setting
1462454: ACT : MCPGPIO,8,1
1462459: MCP : port#8: set to 1
1463777: WD : Uptime 24 ConnectFailures 0 FreeMem 201888 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
1469732: Upload: START, filename: rules1.txt
1469807: Upload: WRITE, Bytes: 280
1469878: Upload: END, Size: 280
1469984: Webserver 1 Arguments: 0: 'enctype' length: 19
1469997: CRC : Settings CRC ...OK
1470011: CRC : SecuritySettings CRC ...OK
1472439: EVENT: Rules#Timer=1,145
1472554: ACT : LogEntry,'start setting'
1472556: start setting
1472607: ACT : MCPGPIO,8,!1
1472612: MCP : port#8: set to 1


2102454: ACT : MCPGPIO,8,!1
2102459: MCP : port#8: set to 1
2103871: IMPT : MQTT fetched json attribute: nvalue payload: 0
2103871: IMPT : [MQTT2#nvalue] : 0.00
2103873: EVENT: domoticz/out/testknop#nvalue=0.00
2103883: EVENT: MQTT2#testknop=0.00
2112440: EVENT: Rules#Timer=1,209
2112448: ACT : LogEntry,'start setting'
2112450: start setting
2112455: ACT : MCPGPIO,8,!0
2112460: MCP : port#8: set to 1

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

Re: MQTT Import

#14 Post by TD-er » 16 Dec 2022, 22:54

Hmm indeed.
It seems the invert doesn't work like I hoped it would (not 100% it did before, have to check the code...)

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

Re: MQTT Import

#15 Post by Ath » 16 Dec 2022, 23:18

Does it work if you insert an = before the ! ?
/Ton (PayPal.me)

Eightyeight
New user
Posts: 7
Joined: 14 Dec 2022, 20:50

Re: MQTT Import

#16 Post by Eightyeight » 16 Dec 2022, 23:42

it does! :)

thanks again!

for full reference if anyone might find it usefull;

Code: Select all


On System#Boot do
LoopTimerSet,1,10
Endon



On Rules#Timer=1 do
LogEntry,'start setting' 

MCPGPIO,8,=![MQTT2#testknop]

Endon


Post Reply

Who is online

Users browsing this forum: No registered users and 32 guests