Announcement

Collapse
No announcement yet.

Convert inbound hex string to number before passing to associated HS device

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

    Convert inbound hex string to number before passing to associated HS device

    I have a subscription to a topic that provides a hex string. I need to convert the hex string to a number before passing it to the associated HS device (status only).
    Example: "00010203" ==> 66051
    What is the best/easiest way to perform this translation?

    #2
    The plugin does not currently have ToHex or FromHex functons as part of the Expression capability. I will add those.

    Comment


      #3
      I added the functions ToHex and FromHex in 5.11.2.0. http://mcsSprinklers.com/HSPI_mcsMQTT_5_11_2_0.zip (HS4) and http://mcsSprinklers.com/mcsMQTT_5_11_2_0.zip (HS3/HS4)

      To install on HS4 download zip to HS4 folder. Unzip to same folder updater_override.json. From HS4 browser Plugins, Add and only HS4 plugin will be mcsMQTT. Select it. When done remove updater_override.json to restore normal Updater functionality.

      To use the update for the topic/device of interest, from the mcsMQTT Association tab click on the Ref column box of the topic row. In the Expression text box enter
      Code:
      FromHex("$$PAYLOAD:")
      This tell mcsMQTT to take whatever is in the payload, convert it to a string (with encasing quotes) and then treat it as a hex value and covert to decimal Double. When it stores the result in HS DeviceValue it will then be the Double value that you are expecting.

      My upload are having a problem this evening. You can also upgrade if you have a relatively recent version from the Update by just unzipping the attached into the \data\mcsMQTT folder when mcsMQTT has been disabled from the Plugin menu. Reenable mcsMQTT
      Attached Files

      Comment


        #4
        I am unable to get this to work.
        I have upgraded the plugin to 5.11.2.0. (HS4)
        I am using Node-Red for testing to keep it simple
        I have tried to use both functions ToHex and FromHex
        He is an example of what is happening...
        1. Node-Red is publishing "0000fe45" to the topic test/hex
        2. The Association screen show the payload received on this topic is "0000fe45"
        3. This topic is associated with device 545 in HS
        4. The detail page for this topic (and associated device 545) has the following settings
          1. Expression: FromHex("$$PAYLOAD:")
          2. Store Payload: In HS Device Value
          3. HS Device Control/Status US: Number
          4. HS Device MISC Properties: Show Values & Status Only
        5. The value show on the Devices page for 545 is 0.0
        Am I missing something??

        Comment


          #5
          What you describe looks correct to me. I did an evaluation again and I obtained a desired result. I did notice that if the expression is not properly formed then a 0 is stored. Next step is to enable debug on the General tab. There will be a few lines around the expression evaluation recorded to the file \data\mcsMQTT\mcsMQTT Debug.txt.

          My screenshots are below. I tried with the default MISC settings and with what I think you setup. Both worked.

          Click image for larger version

Name:	Capture1.PNG
Views:	123
Size:	9.4 KB
ID:	1440433Click image for larger version

Name:	Capture2.PNG
Views:	124
Size:	23.7 KB
ID:	1440434Click image for larger version

Name:	Capture3.PNG
Views:	123
Size:	366.4 KB
ID:	1440435
          Click image for larger version

Name:	Capture4.PNG
Views:	115
Size:	16.9 KB
ID:	1440436

          Comment


            #6
            From debug file:

            12/13/2020 2:37:15 PM 107662 | HSEvent VALUE_SET| 2048| BLStat025| 0| 0| 144
            12/13/2020 2:37:16 PM 108084 | HSEvent VALUE_CHANGE| 1024| | 2110| 2109| 536
            12/13/2020 2:37:16 PM 108681 | Update Accepted 545 to 0000FE45 StatusType=2 Payload=0000FE45 RegExValue=0000FE45
            12/13/2020 2:37:16 PM 108688 | Updating Device from 0 PayloadNumeric=False
            12/13/2020 2:37:16 PM 108717 | HSEvent VALUE_SET| 2048| test| 0| 0| 545
            12/13/2020 2:37:16 PM 108717 | ManageNewMessage test/hex
            12/13/2020 2:37:16 PM 108717 | ActoOnMessageForTrigger Topic test/hex,Payload=0000FE45
            12/13/2020 2:37:17 PM 109286 | HSEvent VALUE_CHANGE| 1024| | 2111| 2110| 536
            12/13/2020 2:37:18 PM 110087 | HSEvent VALUE_CHANGE| 1024| | 2112| 2111| 536

            Comment


              #7
              Just tried dropping and recreating association. Same results. 0 retruned

              Comment


                #8
                I added additional debug. Unzip the file to \bin\mcsMQTT folder; restart the plugin. Post debug segment similar to what is in post #6.
                Attached Files

                Comment


                  #9
                  12/13/2020 4:32:48 PM 76535 | HSEvent VALUE_CHANGE| 1024| | 2031| 2030| 536
                  12/13/2020 4:32:49 PM 77736 | HSEvent VALUE_CHANGE| 1024| | 2032| 2031| 536
                  12/13/2020 4:32:49 PM 77760 | Update Accepted 547 to 0000FE45 StatusType=2 Payload=0000FE45 RegExValue=0000FE45
                  12/13/2020 4:32:49 PM 77769 | Updating Device from 0 PayloadNumeric=False
                  12/13/2020 4:32:49 PM 77770 | PayloadSubstitution value 0000FE45, Payload 0000FE45
                  12/13/2020 4:32:49 PM 77837 | SetDeviceValueString Number newValue , inputValue 0000FE45
                  12/13/2020 4:32:49 PM 77842 | ManageNewMessage test/hex
                  12/13/2020 4:32:49 PM 77842 | ActoOnMessageForTrigger Topic test/hex,Payload=0000FE45
                  12/13/2020 4:32:49 PM 77842 | HSEvent VALUE_SET| 2048| test| 0| 0| 547
                  12/13/2020 4:32:49 PM 77933 | HSEvent VALUE_SET| 2048| BLStat023| 0| 0| 142

                  Comment


                    #10
                    It is acting as if there is something about 0000FE45 is not a hex number. I added debug at the FromHex function itself and also trapped an invalid hex input to ignore non-hex characters. Works or doesn't work I still want to see the debug.
                    Attached Files

                    Comment


                      #11
                      That worked!

                      As mentioned before the source is from Node-Red. I'm using the Inject node and sending the hex as a string.

                      12/13/2020 5:37:26 PM 71130 | HSEvent VALUE_CHANGE| 1024| | 105| 104| 536
                      12/13/2020 5:37:26 PM 71826 | Update Accepted 547 to 0000FE45 StatusType=2 Payload=0000FE45 RegExValue=0000FE45
                      12/13/2020 5:37:26 PM 71835 | Updating Device from 65093 PayloadNumeric=False
                      12/13/2020 5:37:26 PM 71835 | PayloadSubstitution value 0000FE45, Payload 0000FE45, Expression FromHex("$$PAYLOAD:")
                      12/13/2020 5:37:26 PM 71835 | FromHex 0000FE45
                      12/13/2020 5:37:26 PM 71835 | ApplyExpression FromHex("0000FE45") Result=65093
                      12/13/2020 5:37:26 PM 71836 | SetDeviceValueString Number newValue 65093, inputValue 0000FE45
                      12/13/2020 5:37:26 PM 71840 | ManageNewMessage test/hex
                      12/13/2020 5:37:26 PM 71840 | ActoOnMessageForTrigger Topic test/hex,Payload=0000FE45
                      12/13/2020 5:37:26 PM 71841 | HSEvent VALUE_SET| 2048| test| 65093| 65093| 547

                      Thanks for all the help on this!!

                      Comment


                        #12
                        What is interesting about this is that the error branch was never taken so there was effectively no change from the prior implementation that generated an error. Glad it is now working.

                        Comment


                          #13
                          Can you clarify the result type from the FromHex() function?
                          It looks like it is a UINT32 or an INT32 4 byte integer (Big Endian).
                          If that is the case then I don't understand your statement from above ... "This tells mcsMQTT to take whatever is in the payload, convert it to a string (with encasing quotes) and then treat it as a hex value and covert to decimal Double".
                          What did you mean by decimal Double?

                          Comment


                            #14
                            The data type within .NET is a Double which I believe is a 64 bit values for most implementations separated into a exponent part and a precision component. Decimal means it is base 10.

                            Comment


                              #15
                              This is why I thought the result is an UINT32.
                              I don't think negative int, float, or double float are support by the function. I may be wrong.

                              Please try this web conversion tool and use the hex value: 0000FE45

                              Current functionality for the FromHex will work for me but having support for double and negatives would be a nice to have.

                              https://www.scadacore.com/tools/prog...hex-converter/

                              Comment

                              Working...
                              X