Announcement

Collapse
No announcement yet.

Trying to do a payload transformation

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

    Trying to do a payload transformation

    I have a Shellyplugus that reports power and am trying to write an event that triggers off the power draw and turns on a lamp when power is drawn and turns off the lamp when power is 0. It doesnt work too well because the event keeps triggering every time the power changes very minutely. It seems like once a second the event is triggered because the power drain has changed by .01 watts.

    Reading the MQTT manual, it looks like there's a numeric transformation function that can solve my problem, the IfDelta function. I interpret it to mean it outputs the previous value if a new value is less than a threshold, and outputs the new value if its greater than a threshold. Setting the threshold at 3 watts would effectively kill all the power fluctuations.

    I put this in the Expression field of the plugin's subscription window:

    IfDelta($$Value:,$$Previous:,3)

    and all it reports is 0.00 watts in the device feature regardless of the magnitude of the power change.

    Click image for larger version

Name:	image.png
Views:	116
Size:	36.7 KB
ID:	1570055

    Obviously I'm doing something wrong, but what?

    #2
    I would need to run a little test case to confirm, but I think what you want is IFCHANGE rather than IFDELTA. It may also be necessary to put quotes around the 3 as the function is expecting a string so it is able to accept the % option.

    Another idea is that with IFDELTA or IFCHANGE you may want to be comparing with current HS DeviceValue (i.e. $$DVR: (dvRef): ) rather the $$PREVIOUS: since the later is based upon context of a HS Event and the Edit tab Expression is executed in the context of a received MQTT message.

    I will run the test later and better document in mcsMQTT.pdf. You can try yourself. Pay attention to the debug log feedback as parsing mixture of numbers and strings is sometimes awkward.

    Comment


      #3
      I interpreted the manual to say that IFCHANGE would output <null> if less than the delta and the IFDELTA would output the last value if less than the delta.

      I was wondering what $$DVR was. I'll try it too and report back.

      Comment


        #4
        You were correct about IFDELTA being the function to use for the Expression textbox. IFCHANGE returns null if the delta was not sufficient.

        $$VALUE: is HS-oriented for DeviceValue. $$PAYLOAD: is MQTT-oriented for the JSON key value or payload value.

        The desired expression is shown below where 1234 is an example of the HS Device Ref. There is no space between DVR: and (. The 3 can be either number or string "3". There are other variants of DVA and DVC if Ref is not desired to identify the HS Device.

        Code:
        IfDelta($$Payload:,$$DVR: (1234):,3)

        Comment


          #5
          That did the trick! Thanks.

          Comment


            #6
            I'm back again. I'm getting myself deeper and deeper into trouble.

            Suppose I wanted to only publish 2 binary values, say 1 and 100.

            I'm thinking the IFGT function would work: IFGT (<input value>, 50,100,1) would work.

            If input value is greater than 50, then value is 100, else value is 1.

            What do I use for <input value>? $$DVR: (13):" doesnt seem to work.​

            Comment


              #7
              The Expression text box is for received MQTT messages that are being store in HS Devices.
              The Publish Payload text box is for the payload of MQTT messages sent by HS/mcsMQTT. If expressions are used in this case then need to be encased in double less than and double greater than symbols so mcsMQTT knows it is an expression and not literal text.

              When sending MQTT messages the context is usually HS so $$VALUE: or $$LABEL or $$VSP: are the usual substitution variables that apply to the HS context.

              $$DVR will still work, but it will the value currently stored in the HS Device and not a value that is being commanded such as entering a number in the number text box of the Feature on the Devices page.

              The usual scenario when the MQTT Message and HS Device Association is made is to use Value Status Pairs. VSP of 0=Off, 100=100 is typical. There is no need to enter anyting in the Publish Payload Textbox after you have setup the Feature to have these VSP. When you command the Feature to Off or On and there is a Pub topic defined on the Association or Edit tabs then mcsMQTT will send the DeviceValue commanded of 0 or 100, by default. If you did put <<DVR:...>> in the Publish Payload Template then it would use the current DeviceValue and not the commanded value.

              Comment

              Working...
              X