Tag turns true whenever gateway restarts


I have a memory tag ieg, Shift which can A/B/C. Whenever the shift changes, i am triggering below script.

if  currentValue.value !=previousValue.value:
	system.tag.writeBlocking("[.]ShiftChange", [1])

And i am switching off the shift change script after few mins of shift starts via gateway event.
But whenever the gateway restarts, tags getting restarted and hence ShiftChange is changing as true unless it is changed manually.
Is there way to handle the issue in restarting event anywhere?

1 Like

Try the initialChange flag.

if not initialChange and currentValue.value != previousValue.value:

No it is not working as expected.
I have two flags as below,
Script in Shift Id:

def valueChanged(tag, tagPath, previousValue, currentValue, initialChange, missedEvents):
	if not initialChange and currentValue.value != previousValue.value:
		logger = system.util.getLogger("BASE:Shift Change")
		#Enabling the Shift Change flag
		system.tag.writeBlocking("[.]ShiftChange", [1])

Whenever the tag restarts, Shift change tag still becomes true

This whole approach seems fraught with race conditions. How are you using the ShiftChange boolean? Are you simply using a tag change on that to run shift-end scripts? If so, don't use a boolean at all. Consider using just two tags and one event script:

  • A memory tag ShiftStartTS of type datetime.
  • An expression tag CurrentHour with expression getHour(now(5000))
  • A tag change event (either kind) attached to CurrentHour that:
  1. Uses system.date.now() to acquire the current time, then uses that to compute what the ShiftStartTS should be at that moment.
  2. Uses system.tag.readBlocking() to get the stored value of ShiftStartTS.
  3. Compares them. If different, writes the new value back, and also executes your shift change code.

The above approach does not need to check initialChange or compare any other values. It will run your shift change code once and only once even if you had many start/stops/edits around shift change, or the gateway was completely down for a few hours at the normal shift change time.

1 Like