Announcement

Collapse
No announcement yet.

Making Shellyplus i4 work with MQTT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Making Shellyplus i4 work with MQTT

    I'm trying to write an event that will trigger when button 1 of the shelly i4 is pressed.

    Looking through the mqtt association table I find this event:

    Click image for larger version

Name:	Capture1.jpg
Views:	437
Size:	40.0 KB
ID:	1557679

    So when writing the event I set the trigger to: Click image for larger version

Name:	Capture.jpg
Views:	403
Size:	15.6 KB
ID:	1557680 :
    But this doesnt work. How should I be formating my trigger?

    #2
    When setting up MQTT Receive Event Trigger the Topic needs to be the actual Topic that mcsMQTT is processing and the Payload needs to be simple text match,

    I have a Plus1 that has a switch input that is reported via event MQTT message.
    Code:
    Topic
    shellyplus1pm-a8032ab2dba8/events/rpc
    
    Payload
    {"src":"shellyplus1pm-a8032ab2dba8","dst":"shellyplus1pm-a8032ab2dba8/events","method":"NotifyStatus","params":{"ts":1658073561.29,"input:0":{"id":0,"state":false }}}
    To make the Gen 2 Shelly devices look like the Gen 1 devices the Topic and Payload are reformatted. The reformatted message looks like below. This can be obtained from the mcsMQTT Debug file with a line that starts with ActOnMessageForTrigger

    Code:
    7/17/2022 9:11:44 AM    1263680    | ActOnMessageForTrigger QueueSize=0 ,
    Topic
    shellies/shellyplus1pm-a8032ab2dba8,
    
    Payload
    {"input.0":{"id":0,"state":true}}
    Of particular note. The Topic prepends shellies/ and removes the /events/rpc. The Payload changes input:0 to input.0 (colon to period)..

    The event trigger will be based upon the reformatted MQTT message. Since the Payload is JSON you will need to find a slice of the payload that will uniquely represent the content of interest. For my Shelly Plus 1 the switch closure event trigger is shown below.

    Click image for larger version

Name:	0x.png
Views:	433
Size:	15.8 KB
ID:	1557693
    Code:
    shellies/shellyplus1pm-a8032ab2dba8
    "input.0":{"id":0,"state":true
    In your case I believe the Topic and Payload for the Plus I4 will contain
    Code:
    shellies/shellyplusi4-083af200d958
    "input:0","id":0,"event":"single_push"
    From the Shelly doc the JSON format is shown below and is what I used to provide the payload part of the event trigger above. What I do not know is how Shelly uses the "id" parameter. It could always be 0 when only one event is being reported or it could be the same index as the input. You should be able to determine this by looking at the Association table or a little experimentation.

    Click image for larger version

Name:	1.png
Views:	389
Size:	20.4 KB
ID:	1557694

    Comment


      #3
      Still doesnt trigger.

      I rewrote the event trigger as this:
      Click image for larger version

Name:	Capture2.jpg
Views:	407
Size:	20.7 KB
ID:	1557726
      By the way, the "id:0" tracks with "input:0". So for switch 1 it'd be "input:1","id":1,"event":"single_push"

      MQTT Explorer shows this as the message:
      Click image for larger version

Name:	Capture4.jpg
Views:	398
Size:	20.4 KB
ID:	1557727

      Comment


        #4
        Can you post the message from \data\mcsMQTT\mcsMQTT Debug.txt that shows this message? It looks like spaces may exists in the JSON Payload. The text from the debug file will be the same as mcsMQTT is using for comparison. You can cut/paste from the debug file to assure no typo exists in the event trigger Payload.

        Comment


          #5
          I think this is it.
          7/17/2022 5:28:39 PM 15594 | ActOnMessageForTrigger QueueSize=0 ,Topic shellies/shellyplusi4-083af200d958,Payload {"events":[{"component":"input.0", "id":0, "event":"btn_down", "ts":1658096921.68}]}
          7/17/2022 5:28:39 PM 15938 | ProcessMessage shellyplusi4-083af200d958/events/rpc
          7/17/2022 5:28:39 PM 15959 | ActOnMessageForTrigger QueueSize=0 ,Topic shellies/shellyplusi4-083af200d958,Payload {"events":[{"component":"input.0", "id":0, "event":"btn_up", "ts":1658096922.05}]}
          7/17/2022 5:28:39 PM 16108 | ProcessMessage shellyplusi4-083af200d958/events/rpc
          7/17/2022 5:28:39 PM 16124 | ActOnMessageForTrigger QueueSize=0 ,Topic shellies/shellyplusi4-083af200d958,Payload {"events":[{"component":"input.0", "id":0, "event":"single_push", "ts":1658096922.18}]}


          For each button press it sends 3 messages, a button down, a button up, and then the type of button push (single_push).

          I cut and pasted everything into my event and it still doesnt trigger. I'm flummoxed.

          Click image for larger version

Name:	Capture5.jpg
Views:	405
Size:	20.3 KB
ID:	1557774

          Comment


            #6
            I added a debug line where the comparisons are made in the attached which gets unzipped into \bin\mcsMQTT

            The added debug will start with "Trigger Compare for Topic " and contain the payload being used, the compare string segment, and the result of the compare. The compare result will be non-zero for a match and this match should generate a HS trigger.

            I do note that it looks as if the HS event page has removed the spaces. This could be a browser/HTML thing or it could be an actual removal of spaces. The debug will help answer this question.
            Attached Files

            Comment


              #7
              This may take a while.

              I'm running HS on a rasp pi and my Linux is a tad bit rusty.

              Comment


                #8
                I use WINSCP to transfer files from Windows to Linux. Use a login with admin pro ledges and setup to use SFTP on the connection. I could also put the files on mcsSprinklers.com so you can use get from Linux prompt if that will help.

                Comment


                  #9
                  I think I did it right. I extracted the folder into /usr/local/HomeSeer/bin/'MCSMQTTHS4 (25)' and rebooted just for good measure. 'MCSMQTTHS4 (25)' contains two files: MCSMQTTHS4_2020.dll and MCSMQTTHS4_2020.pdb

                  Still no trigger.

                  Comment


                    #10
                    I spoke too soon! It's working!

                    I'm using this trigger Click image for larger version

Name:	Capture6.jpg
Views:	392
Size:	21.4 KB
ID:	1557854
                    So am I correct in assuming that I dont have to use the entire payload? I can use just a part of it as long as that part is unique to the action I want to trigger?

                    Comment


                      #11
                      You are correct about the payload part of the trigger. Any part that is unique is sufficient. If left blank then just the Topic is used.

                      The files should be unzipped into /usr/local/HomeSeer/bin/mcsMQTT folder. The only difference that I am aware of these is that they have debug information going into the log when the debug checkbox is used from the MQTT Page, General Tab. If they are in the wrong place, and are in the serach path, then a future update of the dll will not be recognized. I do not know if /usr/local/HomeSeer/bin/ is in the mcsMQTT.exe search path.

                      Comment


                        #12
                        Two steps forward, one step back.

                        Its almost working. I can change the "event" from single_push to double_push and long_push and it will change the trigger accordingly but only after a system reboot. So for example, if I change the trigger from single_push to double_push, it will continue to trigger on a single_push but not on a double_push. That is, until I reset the pi. Then it works fine on a double_push.

                        I suspect this may have been the problem all along.

                        Strange.

                        Comment


                          #13
                          mcsMQTT builds its trigger logic based upon the events setup when mcsMQTT started. I am not aware of a mechanism for the plugin to know that a user has added a new event. Since every message is being evaluated vs. HS event triggers there would be a significant CPU burden to go back and enumerate all HS events each time a message is received.

                          Once you have your events setup for each of the type of button pushes then disable/enable mcsMQTT so it can rebuild the list of messages that it needs to monitor.

                          Does this explain the behavior?

                          Comment


                            #14
                            It could explain things. Its just that I've never had to reboot when changing an event trigger before.

                            Thanks for your help.

                            Comment


                              #15
                              I looked into rebuilding the trigger and the logic exists, but not getting the result expected. First problem is that HS notifies the plugin when an event is starting to be created, but does not provide any indication when the trigger has been created. This can be overcome by starting to edit another condition so mcsMQTT can initialize for the one that was just completed. There is also the button at the bottom of the General tab to Enumerate Non-Plugin devices. I changed the label to be Devices and Event Triggers. It was doing the emumeration of Triggers. After all of this, however, the only way I could get the desired result was to restart the plugin. I need to look further.

                              Comment

                              Working...
                              X