# Tag turns true whenever gateway restarts

**URL:** <https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329>\
**Category:** Ignition\
**Created:** [December 7, 2022, 11:19am UTC](https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329 "2022-12-07T11:19:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [December 7, 2022, 11:19am UTC](https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329/1 "2022-12-07T11:19:14Z")

</div>

Hi,

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [December 7, 2022, 1:19pm UTC](https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329/2 "2022-12-07T13:19:44Z")

</div>

Try the initialChange flag.

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

```

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [December 14, 2022, 4:22am UTC](https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329/3 "2022-12-14T04:22:12Z")

</div>

No it is not working as expected.  
I have two flags as below,  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/8/986c061fcd028c51bbb3f42cd6bf9082f057244a.png)  
Script in Shift Id:

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

```

Whenever the tag restarts, Shift change tag still becomes true

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 14, 2022, 2:18pm UTC](https://forum.inductiveautomation.com/t/tag-turns-true-whenever-gateway-restarts/67329/4 "2022-12-14T14:18:38Z")

</div>

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.
