# Trouble with Timer Script combined with Alarm Timer

**URL:** <https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [June 2, 2026, 9:53pm UTC](https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814 "2026-06-02T21:53:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jennifer.chang](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jennifer.chang](https://forum.inductiveautomation.com/u/jennifer.chang)\
**Post date:** [June 2, 2026, 9:53pm UTC](https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814/1 "2026-06-02T21:53:02Z")

</div>

Hello all,

I'm hoping someone can help me with this. I have a Gateway Timer script that runs at a fixed rate (every 500 ms). The idea is that it is a heartbeat monitor so every 500 ms, it should read a new value from the PLC (Heartbeat\_from\_PLC), compare it to a previously saved value (Prev\_Heartbeat) and if the value is unchanged, it indicates some sort of communication error and it will set a tag (Heartbeat\_Alarm\_Flag) that will alarm after 10 s and the alarm then triggers another tag (PLC\_Comm\_Err) to turn on. After that, it saves Heartbeat\_from\_PLC to Prev\_Heartbeat and then sets a different heartbeat value, which is sent to the PLC and used only by the PLC (Heartbeat\_to\_PLC).

The code works fine except for one thing - after the 10 s alarm, it takes around 25 s for the PLC\_Comm\_Err tag to turn on and I cannot figure out why! The Gateway Timer script is below:

```python
def handleTimerEvent():
	#===-HEARTBEAT ALARM====#

	# Read current values
	Heartbeat = system.tag.readBlocking(['[MTCE]Heartbeat_from_PLC'])[0].value
	Prev_Heartbeat = system.tag.readBlocking(['[MTCE]Prev_Heartbeat'])[0].value
	
	# Compare current Heartbeat value to previous value
	if Heartbeat <> Prev_Heartbeat:
		# Reset the alarm flag
		system.tag.writeBlocking(['[MTCE]Heartbeat_Alarm_Flag'], 0)
	else:
		# Set the alarm flag (alarm flag has 15 s alarm delay)
		system.tag.writeBlocking(['[MTCE]Heartbeat_Alarm_Flag'], 1)
		
		# Set the PLC_Comm_Err if heartbeat alarm flag has been on for 10 s
		if system.tag.readBlocking(['[MTCE]Heartbeat_Alarm_Flag/Alarms/Timer.IsActive'])[0].value == 1:
			system.tag.writeBlocking(['[MTCE]PLC_Comm_Err'], 1)
		else:
			system.tag.writeBlocking(['[MTCE]PLC_Comm_Err'], 0)

	# Save the heartbeat value to the Prev_Heartbeat variable
	system.tag.writeBlocking(['[MTCE]Prev_Heartbeat'], Heartbeat)

	# Check if heartbeat value sent to PLC should be reset
	if Heartbeat > 99:
		# Reset heartbeat counter
		system.tag.writeBlocking(['[MTCE]Heartbeat_to_PLC'], 0)
	else:
		# Set new value
		system.tag.writeBlocking(['[MTCE]Heartbeat_to_PLC'], Heartbeat + 1)

```

Some things to note:

1. The PLC increases the value of Heartbeat\_from\_PLC as long as it gets a new value of Heartbeat\_to\_PLC from Ignition.

2. Heartbeat\_Alarm\_Flag alarm settings are shown below:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/c/bc31173de22bacd389c199442313c6f5520abdd7.png)

If anyone could shed light on why turning on PLC\_Comm\_Err takes so much more than 10 s after Heartbeat\_Alarm\_Flag turns on, I'd be very grateful!

Thanks!

---

<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 2, 2026, 9:58pm UTC](https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814/2 "2026-06-02T21:58:09Z")

</div>

I would blame the `system.tag.writeBlocking()` operations, if to OPC tags. That, combined with comms overload to the PLC, will stall those operations. Meaning your timer is probably not actually running as often as you think it should be. Consider adding a logging statement to the start of the event script.

---

<div class="post-metadata">

**Author:** ![jennifer.chang](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jennifer.chang](https://forum.inductiveautomation.com/u/jennifer.chang)\
**Post date:** [June 2, 2026, 10:01pm UTC](https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814/3 "2026-06-02T22:01:45Z")

</div>

Thank you! I will try that the next time I am able and see if that's what's going on!

---

<div class="post-metadata">

**Author:** ![jennifer.chang](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jennifer.chang](https://forum.inductiveautomation.com/u/jennifer.chang)\
**Post date:** [June 3, 2026, 8:04pm UTC](https://forum.inductiveautomation.com/t/trouble-with-timer-script-combined-with-alarm-timer/115814/4 "2026-06-03T20:04:34Z")

</div>

Changing writeBlocking() to writeAsync() did the trick! Thank you so much @pturmel!
