# Scheduled Script not working sometimes

**URL:** <https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [June 7, 2023, 12:04pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695 "2023-06-07T12:04:41Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![MC2338](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@MC2338](https://forum.inductiveautomation.com/u/MC2338)\
**Post date:** [June 7, 2023, 12:04pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/1 "2023-06-07T12:04:41Z")

</div>

I have a very simple scheduled gateway script to flip a bit that resets some transaction group hour meters.  
When I manually flip the bit, the hour meters successfully reset.  
When I set the scheduled script to the current time + 2 min and save, It runs successfully, flips the bit, and resets the hour meters.  
When I set the scheduled script to the time I need it to reset (after the shift), seemingly nothing happens. When I check the runtime values (that should be reset by the bit going high) the following morning, they are not reset.

\*This script should run twice, after hours. Once to set the bit high (to reset tx groups), once again to reset the reset bit.

Please help.  
see scheduled gateway script below:

> 

```
def onScheduledEvent():
   resetBit = system.tag.readBlocking("[default]NJSec_Runtime_Reset_All")[0].value

   if resetBit == True:
	   system.tag.writeBlocking("[default]NJSec_Runtime_Reset_All.value", False)
	
   else: 
  	   system.tag.writeBlocking("[default]NJSec_Runtime_Reset_All.value", True)

```

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 7, 2023, 12:31pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/2 "2023-06-07T12:31:50Z")

</div>

I don't think you want or need the `.value` in the tag path. Just `[default]NJSec_Runtime_Reset_All`.

You should throw a logger in there somewhere to verify. Maybe it's just not the value you're expecting it to be? Before changing anything I Would modify this to be like

```auto
def onScheduledEvent():
   logger = system.util.getLogger("Reset logger")
   resetBit = system.tag.readBlocking("[default]NJSec_Runtime_Reset_All")[0].value
   logger.info("Reset bit value %s type %s"%(str(resetBit),str(type(resetBit))))
   if resetBit == True:
       logger.info("In true block")
	   system.tag.writeBlocking("[default]NJSec_Runtime_Reset_All.value", False)
	
   else: 
       logger.info("In else block")
  	   system.tag.writeBlocking("[default]NJSec_Runtime_Reset_All.value", True)

```

Just to verify what exactly is going on before making any changes.

You may also want to examine the results of your `writeBlocking` - you get a code back per right but it does not throw an error so it wouldn't pop up in your logs.  
`results = system.tag.writeBlocking("[default]NJSec_Runtime_Reset_All.value", False)` will give you a list of result codes - maybe its just having an issue writing?

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [June 7, 2023, 12:33pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/3 "2023-06-07T12:33:52Z")

</div>

You don't need .value on the write blocking

Write Blocking can just write to the tag path. This is probably erroring when run so it doesn't work. I would try running the script in the script console, you should get any exceptions appear in there.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 7, 2023, 12:35pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/4 "2023-06-07T12:35:48Z")

</div>

You won't get an exception for bad tag writes. You will get a list back of [QualityCode](https://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.12/com/inductiveautomation/ignition/common/model/values/QualityCode.html) objects that say if the write was successful or bad or what have you.

I do suspect the `.value` is what is throwing it though. I am guessing he is getting a `Bad_NotFound` quality code back or something like that.

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [June 7, 2023, 12:46pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/5 "2023-06-07T12:46:07Z")

</div>

ah yeah that's true, could through a `print(system.tag.writeBlocking.....)` and read off that result in the script console if that is possible.  
system.tag.write() if I remember does through an exception, but that is depreciated now.

---

<div class="post-metadata">

**Author:** ![MC2338](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@MC2338](https://forum.inductiveautomation.com/u/MC2338)\
**Post date:** [June 8, 2023, 5:06pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/6 "2023-06-08T17:06:48Z")

</div>

Thank you both for your input. I tried the script you provided but yielded similar results. See logs attached. Please advise.

It looks like it ran successfully but my hour meters were not reset when I checked this morning.

 ![ignition logs reset script](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/b/fbdfa1e29928745a6f6f7acf324009c009734a42.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 8, 2023, 6:25pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/7 "2023-06-08T18:25:08Z")

</div>

You're going to have to give us more context on these hour meters. Are they an ignition or opc tag? Where is the logic to reset them located? etc. All we know / can see is your scheduled event about the reset bit which does seem to be working as intended now.

---

<div class="post-metadata">

**Author:** ![MC2338](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@MC2338](https://forum.inductiveautomation.com/u/MC2338)\
**Post date:** [June 8, 2023, 6:45pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/8 "2023-06-08T18:45:06Z")

</div>

The logic to reset them is in the transaction group settings. They are ignition memory tags. When I manually trigger the bit, the values reset as they should. See ss of tag config & tx group settings below:

 ![ignition hour meter settings](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/1/314109658c95c061055c07b70ecbaf26d0acc79b.png)  
 ![ignition runtime tag settings](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/b/eb92f6dba17fc342b701b0848cd2679580d11883.png)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 8, 2023, 6:54pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/9 "2023-06-08T18:54:26Z")

</div>

> [@bkarabinchak.psi](#):
>
> I do suspect the `.value` is what is throwing it though

Nah, the `value` is a property and can be specified to be written to or read just like any other tag property.

@MC2338 You should be providing `system.tag.writeblocking()` with a list of values to write.

```python
def onScheduledEvent():
   logger = system.util.getLogger("Reset logger")
   resetBit = system.tag.readBlocking("[default]NJSec_Runtime_Reset_All")[0].value
   logger.info("Reset bit value %s type %s"%(str(resetBit),str(type(resetBit))))
   if resetBit == True:
       logger.info("In true block")
	   system.tag.writeBlocking(["[default]NJSec_Runtime_Reset_All.value"], [False])
	
   else: 
       logger.info("In else block")
  	   system.tag.writeBlocking(["[default]NJSec_Runtime_Reset_All.value"], [True])

```

I modified @bkarabinchak.psi 's script to have the correct syntax for `writeBlocking()` yes I'm aware that it will work with single elements, but the preferred method is to use lists. The script should technically work but it's just one thing to get out of the way. I would get in the habit of always using lists.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 8, 2023, 6:55pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/10 "2023-06-08T18:55:58Z")

</div>

If you only do a single element you don't need to put it in a list for writeBlocking anymore on the newer versions of 8.1.X as I recall. But perhaps that is the issue here.

@MC2338 Did you ever examine the result code of the write?

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 8, 2023, 6:56pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/11 "2023-06-08T18:56:48Z")

</div>

> [@lrose](#):
>
> the correct syntax for `writeBlocking()` yes I'm aware that it will work with single elements, but the preferred method is to use lists

yep, I know.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [June 8, 2023, 6:58pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/12 "2023-06-08T18:58:42Z")

</div>

Ha I should read your whole post before replying 🙃

Everything else seems fine though. Going to need to see what the result code of the write is but if NJSec\_Runtime\_Reset\_All is just a memory tag I don't know why it would be failing.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 8, 2023, 6:59pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/13 "2023-06-08T18:59:53Z")

</div>

I would probably write this script like:

```auto
def onScheduledEvent():
    resetBit = system.tag.readBlocking(['[default]NJSec_Runtime_Reset_All'])[0].value
    system.tag.writeBlocking(['[default]NJSec_Runtime_Reset_All'],[not resetBit])

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [June 8, 2023, 7:00pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/14 "2023-06-08T19:00:56Z")

</div>

I'm on the tail end of this thread, and I don't know the implementation details of the hour meter, but it would be better to handle it in a rollover, odometer style and just subtract current value from last record.

---

<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:** [June 8, 2023, 8:37pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/15 "2023-06-08T20:37:07Z")

</div>

> [@dkhayes117](#):
>
> handle it in a rollover, odometer style

This, for sure.

But I suspect the OP's problem is not holding the reset bit on long enough for the transaction group to execute to see it.

---

<div class="post-metadata">

**Author:** ![MC2338](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@MC2338](https://forum.inductiveautomation.com/u/MC2338)\
**Post date:** [June 15, 2023, 1:51pm UTC](https://forum.inductiveautomation.com/t/scheduled-script-not-working-sometimes/74695/16 "2023-06-15T13:51:10Z")

</div>

We would prefer to have it reset everyday. I think we got it figured out though.

The theory is: the time that the script in question was scheduled to run was a time that the machines (where running bool bit is read from) were offline. Because the machines were offline the transaction groups would not reset. We adjusted the time of execution to a time when we knew all machines should be online. This did lead us to another issue which I will describe in another thread. BUT, since this change we have seen all tx groups reset everyday (minus machines that are still offline).

\*This is after reviewing the issue with IA Tech support who confirmed this is the issue
