Hello!
I have a toggle switch that records the start and end time of a “batch”. I have the times stored on a historized tags. I wanted to set up a table where you could select which start and end time you wanted and then that would select a report to print. However I keep getting weird double entries when I populate the table with the binding (I believe this has something to do with the time stamp). This is not good because it sets the start time after the end time and that shouldn’t work like that.
I could do the data editing in a script, but where should I attach the script to is my question. There’s a 100 different ways you could do this, but I figured I should ask the professionals before I make someone else’s life harder.
Let me know what you guys think!
This is a use case for a dropdown rather than a table, but let's work with the table for now.
Please show
- How the toggle switch is being recorded.
- The table data binding.
Please see Wiki - how to post code on this forum but post a screengrab as well if you think context would be helpful.
Welcome to the forum.
I didn’t think of a drop down. Thanks. I would still like to see if this could work as intended though.
I have this script running as change script on the selected property of a toggle. Here’s the code:
if self.props.selected == True:
tag1 = system.tag.getConfiguration("[default]PressureTestV2/SimulatedCurrentPressure",0)
tag2 = system.tag.getConfiguration("[default]PressureTestV2/SimulatedMaxPressure",0)
tag1[0][u'historyEnabled'] = True
tag2[0][u'historyEnabled'] = True
system.tag.configure("[default]PressureTestV2",tag1) #could use merge overwrite
system.tag.configure("[default]PressureTestV2",tag2)
system.tag.writeBlocking("[default]PressureTestV2/BatchStarted",1)
system.tag.writeBlocking("[default]PressureTestV2/StartTime",system.date.now()) # Forum! this line
if self.props.selected == False:
tag1 = system.tag.getConfiguration("[default]PressureTestV2/SimulatedCurrentPressure",0)
tag2 = system.tag.getConfiguration("[default]PressureTestV2/SimulatedMaxPressure",0)
tag1[0][u'historyEnabled'] = False
tag2[0][u'historyEnabled'] = False
system.tag.configure("[default]PressureTestV2",tag1)
system.tag.configure("[default]PressureTestV2",tag2)
system.tag.writeBlocking("[default]PressureTestV2/BatchStarted",0)
system.tag.writeBlocking("[default]PressureTestV2/EndTime",system.date.now()) # Forum! this line too
There’s some other stuff in here where I turn tags’ history on an off, and I do admit this might not be the best way to do this. For the stuff relevant to this post, I used the last lines of the if and else statement to write to the tags. I added comments saying which lines.
The tags (EndTime and StartTime) are just standard memory tags. Tags also might not be the best way to this either, but it’s just what I know right now.
Here’s the tag history binding on the table.
In regards to the table, I have also added to the columns property to exclude the time stamp column. The problem is still there when I have the time stamp column.
I'm no Python expert, but a couple of pointers:
if self.props.selected == True:
# do stuff
if self.props.selected == False:
# do other stuff
can be replaced by,
if self.props.selected:
# do stuff
else:
# do other stuff
writeBlocking accepts lists (and even single values should be supplied as a list) so that they can all be done in one call.
system.tag.writeBlocking("[default]PressureTestV2/BatchStarted",0)
system.tag.writeBlocking("[default]PressureTestV2/EndTime",system.date.now())
would become,
tagpaths = ["[default]PressureTestV2/BatchStarted", "[default]PressureTestV2/EndTime"]
values = [0, system.date.now()]
system.tag.writeBlocking(tagpaths, values)
I would bind the toggle switch to one boolean tag (make it a bidirectional tag binding) and enable history on that "onChange". Then get the tag history and write a script to loop through the returned data and convert it into start / end columns.
Modify your table's data binding. Make it 'Tall' and just the one tag (being written to by the toggle). Set the Value Format to 'Document' to return the data in JSON format.
Add a script transform and paste in the following code:
def transform(self, value, quality, timestamp):
output = []
dic = {}
for row in value:
if row['running']:
dic['startTime'] = row['t_stamp']
else:
dic['endTime'] = row['t_stamp']
# Note that the tag history query results may start with an 'off' transition.
# We'll ignore that if we see it. (It will depend on the start date.)
if 'startTime' in dic:
output.append(dic)
dic = {}
return output
Change the table column field names to match (or edit the script). You already know how to format as datetime.
One of the Python guys will be along shortly to reduce my script to two lines!