Announcement

Collapse
No announcement yet.

Script Callbacks Available?

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

  • Script Callbacks Available?

    First, thanks for writing mcsMQTT! I've been getting started with it by using it communicate with a few HA Switchplates as noted in the provided link.

    I'm making good use of your script function "SendMqttMessage" to initialize the displays with the correct label text, colors, etc.

    I was wondering if you had thought about providing the ability to register a script/function to be called whenever a specified topic was received.

    Something like...

    hs.PluginFunction("mcsMQTT", "", "RegisterTopicReceivedScript",{"MyScriptName", "MyFunctionName", "MyMQTTTopic"})

    Then, whenever the topic is received, the plugin would call the specified script function and pass it the topic received.

    Having this ability would save me a fair amount of time and effort in creating and maintaining a bunch of events to handle button presses, etc. since they could all be handled in one script quite efficiently.

    What do you think?
    Thanks,
    Yoyo



    LCD touchscreen for Home Automation. Contribute to aderusha/HASwitchPlate development by creating an account on GitHub.

  • #2
    I had not considered it. No problem adding it. Often payloads are JSON. Are you expecting callback on each JSON item or one callback per payload? Since mcsMQTT subscribes to everything should sort of filter be applied to a callback?

    Comment


    • #3
      Originally posted by Michael McSharry View Post
      I had not considered it. No problem adding it. Often payloads are JSON. Are you expecting callback on each JSON item or one callback per payload? Since mcsMQTT subscribes to everything should sort of filter be applied to a callback?
      I guess I was thinking one callback per payload would be fine, as the script could be responsible for parsing the JSON itself.

      As far as the filtering goes, I was thinking the MQTT topic of interest would be passed to the "registration" procedure as shown in the sample above. Then, only those topics would be forwarded to the passed in script function.

      Comment


      • #4
        I have done callbacks when msScriptControl was available. I will need to investigate how it is done in the .Net world.

        Why are you using script now rather than mapping the topics into HS devices? Most users want to have payload attributes visible in Devices do they can put them on their UI or with the capabilities that HS has for devices.

        Comment


        • #5
          >> Why script now rather than mapping...?

          In this case I have a series of navigation buttons on the display. The topic is hasp/backdoor/status/p[x].b[y] where x = the page the button is on, and y = the button number. If I can't use a script, I will have to create events for every single button that is pressed, even though many would be redundant actions. If I ever change an action, I'll need to re-visit every event to make the change.

          If I had the script callback, I would ask for a callback anytime hasp/backdoor/status/# is received, and parse out the page and button text for the action.
          For me it is also just cleaner, no additional devices needed, and a single place to easily manage the actions.

          My other alternative is to grab a .net mqtt library and just use it in the script for receiving the topics, but that seems a waste when your plugin is so close.

          I can share some of my own plug-in code where this type of callback functionality has been implemented if you'd like...

          Comment


          • #6
            Yes sharing your code segment would make it much easier for me. Email is mcsSolutions at CenturyTel dot net if you want to email it.

            Comment


            • #7
              Michael, I just sent an email to you with an example attached...

              Thanks for at least looking into it!

              Comment


              • #8
                This is fantastic, thanks!!

                One question. What would be the best way to have my callback procedure registered after hs or plugin restart. Is there a way to detect if it is already registered?

                Comment


                • #9
                  There is no plugin function that returns true/false if a given (script, function, topic) has been registered. Trying to register multiple times will not affect behavior other than the message in the HS Log about having tried without success because of it being a duplicate.

                  I have not used this capability in production so the following suggestions are unproven. The HS startup.vb is invoked after all the plugins have been registered according to the HS Log. Adding the register with mcsMQTT somewhere in this script should produce the desired result. If one manually restarts mcsMQTT then the script registration would not be performed using this method. For this scenario I would either just restart HS rather than restart mcsMQTT or if done often enough then use an event with manual trigger and then invoke the event to perform the registration.

                  Comment


                  • #10
                    Originally posted by Michael McSharry View Post
                    There is no plugin function that returns true/false if a given (script, function, topic) has been registered. Trying to register multiple times will not affect behavior other than the message in the HS Log about having tried without success because of it being a duplicate.
                    .
                    Ok thanks. In that case i'm probably going to use a timer and re-register if the callback has not run for some time.

                    Comment


                    • #11
                      Isn't it more likely that there is something amiss with MQTT network rather than lack of registration if messages are not received? You could look at one or more of the statistics devices to know if MQTT traffic is happening and then only register when there is traffic, but script not receiving it.

                      Comment

                      Working...
                      X