6 Replies Latest reply on Dec 5, 2017 9:34 PM by kduffy

    Running macro on time stamp

    Jsykes1823

      Hello,

       

      Not really great with macros but i was wondering if its possible to run a macro inbetween time stamps? We have 4 valves that need to be switched throught the day and guys are forgettign to switch them, so i would like to run a macro between X and Y times to check for the valve position and if it is not opened between thoes time it will flash on the overhead screen.

       

      Thanks for the help,

      J

        • Re: Running macro on time stamp
          John Messinger

          Hi Jeremy,

           

          I'm assuming here that you are looking to do this from within PI ProcessBook. Yes, this is possible. Writing VBA code to check the value of a tag (in this case the valve position) and then generating some form of visual alarm on the display is all doable, and there are plenty of examples in the forums of how to perform these tasks programatically. As for the scheduling of this macro, that is a little less straightforward in VBA, but not impossible. I would suggest that you refer to this excellent blog post for some guidance on implementing this functionality.

           

          Perhaps an easier way to give you the same result but with no coding, is to implement an Expression Analysis in AF to do this for you. You could write a simple expression to check the value of the valve position tag using the TagVal() function, and then write an appropriate output value to an 'alarm' tag (mapped as the output for this Expression Analysis). You would schedule the analysis to run at an appropriate interval (every hour, 4 hours, etc). The 'alarm' tag then only needs to be added to your display as another Value object - maybe multi-stated to give some visual priority when in an 'alarm' state. Frankly, this is the option I would follow, as it's much less work to implement, and much easier to maintain. The other advantage of this option is that if you are using PI Vision instead of PI ProcessBook, then this solution will work with that environment whereas a VBA based solution will not.

           

          Regards,

          John

          1 of 1 people found this helpful
            • Re: Running macro on time stamp
              Jsykes1823

              Hey thanks for the respones,

               

              I ended up using an sub that updates on Display_DataUpdate with an IF statment that Will set a text propertie to visible on the certain hour and a multistate condition that will change the color of the text if the valve is not in the correct position. Not sure if that is the best way to do it but it works haha.

               

              One question I tried this:

               

               

              If Hour(Time) = 24 or 1 Then

              Text68.Visible = True

              Else

              Text68.Visible = False

              End If

               

              But it wouldnt work, I ended up using the code below. Just wondering why I couldnt use the "or" statment

               

              Private Sub Display_DataUpdate()
                            
              If Hour(Time) = 24 Then
              Text68.Visible = True
              ElseIf Hour(Time) = 1 Then
              Text68.Visible = True
              Else
              Text68.Visible = False
              End If

              If Hour(Time) = 3 Then
              Text69.Visible = True
              ElseIf Hour(Time) = 4 Then
              Text69.Visible = True
              Else
              Text69.Visible = False....... and so on

                • Re: Running macro on time stamp
                  John Messinger

                  Hi Jeremy,

                   

                  Glad you found a solution that works for you. Regarding your initial code option, try the following:

                   

                  If Hour(Time) = 24 Or Hour(Time) = 1 Then
                  ...
                  

                   

                  You need to be fairly explicit with the condition(s) you are testing in this kind of statement.

                   

                  John

                  • Re: Running macro on time stamp
                    kduffy

                    Hi Jeremy,

                     

                    I don't know of a better way to trigger a specific VBA command to run at an exact time, but I have a few comments and an alternative suggestion for you.

                     

                    First, it looks like this method will make the two text boxes visible for the entire hours of 12:00am - 1:59:59am, the next one from 3:00-4:59:59am, etc. I would suggest some ability to dismiss the alarm, just from a usability standpoint. If that were to be introduced, this method would be somewhat difficult to use. You'd have to implement a boolean variable that's either global or is stored on the display itself, and continually reset it on each timer.

                     

                    Second, I strongly advice against putting code into the Display_DataUpdate event handler as this code is executed every time the data refresh occurs (which by default is every 5 seconds). Since this text box is turning on once a day and turning off once a day, it only NEEDS to run twice per day, but is instead running 17,280 times per day (24*60*60/5), which is 8,640 times as often as it NEEDS to run for each text box. If this were more substantial than a few lines of code, you can hopefully see that this can quickly lead to severe performance hits.

                     

                    What I would suggest you consider doing, as John was mentioning, is to use one or more AF attributes to control whether the value is open and whether the operators need to be alerted. Then you can easily add a multi-state text book that's hidden until the valve needs to be opened/closed, and then it can show. A button, or similar, can be added that allows the operator to write back to the AF attribute that they have closed/opened the valve. The good thing about this is that the multi state can listen for updates to that value and not have to check the time every 5 seconds. It also would allow for the alert to be easily dismissed, and the best part is it would sync across any display that is showing that attribute in any control room, etc. If you were to use a variable in VBA, its value would be confined to the display itself. Lastly, if this display were to ever be moved to PI Vision, it would lose the VBA from it, but the attribute approach would still work perfectly fine.

                     

                    Let me know if you would like assistance with this alternative approach.

                     

                    Kelsey

                      • Re: Running macro on time stamp
                        Jsykes1823

                        I'm not to sure if i have to AF, is it a seperate program or is it within PI process book?  

                          • Re: Running macro on time stamp
                            kduffy

                            Hi Jeremy,

                             

                            PI Asset Framework (AF) is a separate product that is intended to act as a "front end" server to expose your PI Data Archive. Instead of referencing tags by their name, you can create an element hierarchy where you may have an element for each plant that you are operating, then under that an element for each type of equipment, then under that an element for each instance (ie, Houston > Reactors > Reactor_1501), then you can have attributes for things like temperature (which you can map to a PI tag), pressure, name, anything. The structure is very templatized so you can make one reactor element template and quickly create thousands of copies of it to complete the structure. The idea being that "Reactor_1501's temparature" makes more sense than "ti_1501_01.pv".

                             

                            You can find more information about it from this overview page, but AF implementation is frequently handled by the PI admins at a company, so if you are curious about it I would reach out to them to see if you have it or are looking into getting it, and you can contact techsupport at any point about it as well.

                             

                            Kelsey