Creating Shift tags

there must be a better way than this to generate three bool tags that represent ShiftA ShiftB and ShiftC
What I am doing, I have created a tag “Hour” that represents the Gateway’s clock hour in 00-23 format. I have a timer script running every second to check the hour and set the appropriate bool .
I could have used expressions on each of the three bool tags shiftA, shiftB and shiftC. But my concern is all three expressions may not be evaluated at the exact same CPU scan and there may be an instance where neither 1 of the 3 are true.
So hopefully by putting all the evaluation logic in one script, and three are evaluated at the same time.

Is there a better and more reliable way to set 1 of 3 bits with the guarantee that 1 of the 3 will always be set?


# current Hour 00 - 23 format
hr = system.tag.getTagValue("DownTimes/Hour")

# clear shift bools, and set appropriate shift bool based on Clock hour

shiftA = 0
shiftB = 0
shiftC = 0

# test for Shift A 6:00 - 13:59:59
if hr >= 6:
	if hr <14:
		shiftA = 1

# test for Shift B 14:00 - 21:59:59
if hr >= 14:
	if hr < 22:	
		shiftB = 1

# If Not shiftA AND NOT shiftB MUST be shiftC		
if shiftA == 0:
	if shiftB == 0:
		shiftC = 1		
		
# update shift bools		
system.tag.writeToTag("DownTimes/ShiftA",shiftA)			
system.tag.writeToTag("DownTimes/ShiftB",shiftB)
system.tag.writeToTag("DownTimes/ShiftC",shiftC)

What you have will help, but since there are three separate tags, there will always be the possibility of all three tags being zero (say, if one of the writes becomes pending instead of immediately successful).

Below is a tiny bit more streamlined, but at the end of the day, it’s still at the mercy of the tag writes.

[code]# current Hour 00 - 23 format
hr = system.tag.getTagValue(“DownTimes/Hour”)

clear shift bools, and set appropriate shift bool based on Clock hour

shiftA = shiftB = shiftC = 0

#Set Shift Values
if 6 <= hr < 14: # test for Shift A 6:00 - 13:59:59
shiftA = 1
elif 14 <= hr < 22: # test for Shift B 14:00 - 21:59:59
shiftB = 1
else:
shiftC = 1 # If Not shiftA AND NOT shiftB MUST be shiftC

update shift bools

tags = [“DownTimes/ShiftA”, “DownTimes/ShiftB”, “DownTimes/ShiftC”]
values = [shiftA, shiftB, shiftC]

results = system.tag.writeToTags(tags,values)[/code]

Alternatively, using writeToTagSynchronous would wait for each tag to be written before going to the next line. That would ensure that one of them is at a 1 when the script is finished.

Your project is likely too far along to consider backtracking, but a single value to represent shift (I use an integer for such things) would eliminate the worry.

thanks for the input.
I ended up creating an int called Shift that is 1,2 or 3 as you pointed out.
Normally I do this in a PLC , where it is much easier to do.
This application calls for the Gateway to interface directly to Weigh Scales using Modbus TCP. No PLC .

I messed myself up writing the code as I always get tripped up remember the different syntax for database, expressions and scripts.

[quote=“Curlyandshemp”]
I messed myself up writing the code as I always get tripped up remember the different syntax for database, expressions and scripts.[/quote]

I understand. I think there’s a theory that states “if you make it possible for programmers to write in English, you’ll find out that programmers can’t write in English”… :smiley:

Do you have to have three Booleans? Can you not work with one tag instead? In other words, if you had a string type sql tag named Shift, you could have a script that would assure that the string is always representing one of the three shifts.

On my gateway I use parts of the current time for various functions in my apps, so I have a gateway script running all the time as a Timer script triggered every 1000 ms. I have three integer type sql tags named CurrentHour, CurrentMinute, and CurrentSecond. The gateway timer script is as shown below.

theTime = system.tag.read('[System]Gateway/CurrentDateTime') system.tag.write('CurrentHour',int(system.db.dateFormat(theTime.value, 'HH'))) system.tag.write('CurrentMinute',int(system.db.dateFormat(theTime.value, 'mm'))) system.tag.write('CurrentSecond',int(system.db.dateFormat(theTime.value, 'ss')))

I can always look at these tags to make something happen at a certain hour, minute, second, or combination of any of the above.

So, in your case, I would run a Tag Change type gateway script that watches the CurrentHour tag. When the hour changes, the following script would fire.

qv = system.tag.read('CurrentHour') if qv.value > 5 and qv.value < 15: system.tag.write('Shift', 'A') elif qv.value > 15 and qv.value < 23: system.tag.write('Shift', 'B') else: system.tag.write('Shift', 'C')

The Shift tag will always be either A, B or C.

If you must use the three Booleans, the method Jordan presented with the multiple tag write is, I believe, the best you can do. I would think the three tags would be written in a very short time period, probably less than a microsecond. So I would think it would be rare that another process would find that none of the three were true, or more than one was true. I’m not even sure if another process could access the three tags while they were in the process of being written. That would be a question for one of the developers to clarify.