Announcement

Collapse
No announcement yet.

Variable replacement API

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

    Variable replacement API

    Do you know of an existing API that takes an input string, performs variable ($date, $$DV:, etc) replacement and returns the resulting string?

    The API must exist internally as the functionality is available for email messages for example, but was wondering if it is exposed and be used in scritps.

    #2
    The essential script function is REPLACE
    It can be used from a HS event in a one liner. For example to change the contents of all periods in device A1 device string to semicolons it would be
    &hs.SetDeviceString "A1",REPLACE(hs.DeviceString("A1"),".",";")
    The REPLACE function has other options and you can do a google or similiar search to learn about them.

    Comment


      #3
      Thank you Michael. I am familiar with the VB replace. What I am looking for is a HomeSeer API that would automatically do the variables replacement (The HomeSeer variables $date, $DVaddress), etc ) similar to the substitution HomeSeer can do in emails and text to be spoken ( (see webhelp/tipstricks/using_replacement_variables.htm in the HomeSeer documentation).

      For example if I have the string "the current date is $date. The value of R20 is $$DV:R20 and its status is $$DS:R20", is there an HomeSeer API that would return a string replacing the variables. I could write my own of course but would prefer to use an existing one if this functionality has been exposed and not have to maintain my own.

      Comment


        #4
        I think you are on your own. To my knowledge HS only does this when you have no opportunity to do it before using the target function. Email and speech are the only cases that I know of.

        Comment


          #5
          Yes. Unfortunately "Public Function ReplaceVariables(StrIn As String) As String" available as a plugin callback is not available in scripts.

          I wrote one in VB.Net and code is in attached zip file (I may not have defined all homeseer special house codes). The API to call is HSReplaceVariables. Below is an example that will return a string with information for device R30:

          str = HSReplaceVariables("The current date and time is $date, $time. Testing device R30, name: $$DN:R30, value:$$DV:R30, status: $$DS:R30, string: $$Ds:R30, string without html: $$Dx:R30, location: $$Dx:L30, Type: $$DT:R30")
          Attached Files

          Comment


            #6
            I am not exactly sure why we only made it available for plug-ins, but I could see no reason for it not being available to scripts to make things easier for them, so in our next release hs.ReplaceVariables will be there!
            Regards,

            Rick Tinker (a.k.a. "Tink")

            Comment


              #7
              Thank you Rick!

              Similar to others (seehttp://board.homeseer.com/showthread.php?t=128624) a few additional replacement variables would be very welcome and some critical in my mind. For example in many cases I would like to use the device string (as is or stripped from any html tags) instead of the value or status as well as the device name. If you could support additional variables you would make it even more useful and simplify event configurations and readability. Currently I have complicate the implementation and lower readability in the events page; I have to write a small script to perform the variable replacement before doing the action (email, speak, etc) instead of using the email, speak, etc actions from within the event.

              Lately and using my custom API I also perform value replacement with values fetched from an INI file; this is quite useful to extract the forecast from the weather xml or weather warning in files for example and include it in emails.

              Attached is the latest version of my custom replacement API. Feel free to use the code or ideas. It supports the following additional variables:

              $SDATE - Get short date
              $STIME - Get short time
              $LDATE - Get long date
              $LTIME - Get long time
              $INI:{ini filename without .ini}:{section}:{key} - Get a value from an ini file
              $$DN:{address} - Get the device name
              $$Ds:{address} - Get the device string
              $$Dx:{address} - Get the device string removing html tags (i.e. plain text)
              $$DL:{address} - Get the device location
              $$Dl:{address} - Get the device location 2
              $$DT:{address} - Get the device type string
              Attached Files

              Comment


                #8
                The purpose of the replacement variables is to provide replacements of text in a text string that you normally do not have access to in a script - e.g. text in an event action that you tell HomeSeer to speak, or text in an Email message that you tell HomeSeer to send.

                Many of the ones you have here are accessible easily, in some cases with a single command, in a script, and so it makes no sense adding them to the replacement variables script command. Some of the others are esoteric enough that they would not have much utility for speak text or email messages generated in an event.

                Looking for example at $$DN: - after the colon, you have to provide the device address. If you know the device address, don't you also already know the name and location? I cannot see me putting this in a string:
                The status of $$DL:B5 $$DN:B5 is $$DS:B5
                Rather than
                The status of the Family Room Light is $$DS:B5

                Also, be aware that the HomeSeer replacement variables are case insensitive, so if you use your $$Ds:B5 but run it through the HomeSeer one first, then you will get the HomeSeer one rather than yours.
                Regards,

                Rick Tinker (a.k.a. "Tink")

                Comment


                  #9
                  I agree that some are more esoteric than others and the variable names are only ideas ($$DS versus $$Ds for example). In my case and for multiple events, I would prefer to directly use the speak, send email, etc actions in events instead of using a very short script (i.e. a few lines to replace variables and call the desired action such as email, speak, etc).

                  As I look at my usage, the most relevant are $$Dx (get device string removing any HTML), $$Ds (get device string), $$DN (get device name) and $INI (get value from ini file) as I access many non X10 related devices (weather, UPS, network monitored devices, etc) that have custom/non X10 device strings.

                  If I select the device, I agree I should know its location and name, but for ease of maintenance (in case location name or device name changes) I would refer to use " The status of $$DL:B5 $$DN:B5 is $$DS:B5" instead "The status of the Family Room Light is $$DS:B5".

                  Thank you for pointing out the case insensitive of the Homeseer API and your interest in this subject.

                  Comment


                    #10
                    Where exacly do you place this script so you can use it?

                    Comment


                      #11
                      Originally posted by dsabot View Post
                      Where exacly do you place this script so you can use it?
                      In my case, I have it part of my script (i.e. in the same script file where I needed this functionality). Separate from scripts, I also use it as part of plugins I wrote and in this case it is part of my source code reposity/library.

                      Comment


                        #12
                        Rick, thank you for exposing the API for scripts in the latest Beta. I downloaded the beta but did not have a chance to upgrade yet.

                        Has the built in API been enhanced to support replacement variables for at least the string of a device (ideally removing any HTML tags)? I could not find any reference in the posts.

                        Comment


                          #13
                          Rick,

                          I agree with you. It makes no sense to provide this functionality when it is already available through other means. The scripting interface should be kept as simple as can it can be. Anything the "replacement" function might provide, I can easily do (and already do) with already available functions.

                          Bill

                          Comment

                          Working...
                          X