# Timer Component

**URL:** <https://forum.inductiveautomation.com/t/timer-component/17125>\
**Category:** Ignition\
**Created:** [February 25, 2018, 10:13pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125 "2018-02-25T22:13:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 25, 2018, 10:13pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/1 "2018-02-25T22:13:12Z")

</div>

Is there a way to have the timer count in Hours/Min/seconds?

---

<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:** [February 25, 2018, 11:09pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/2 "2018-02-25T23:09:06Z")

</div>

What exactly are you trying to do? The timer component doesn’t display anything itself, and doesn’t offer a fixed interval, just a fixed delay (a time-consuming actionPerformed operation will delay the next). If you need a reference to real time, you should be building upon the [now()](https://docs.inductiveautomation.com/display/DOC79/now) expression function, the [system.date.now()](https://docs.inductiveautomation.com/display/DOC79/system.date.now) scripting function, and the [system.date.\*Between()](https://docs.inductiveautomation.com/display/DOC79/system.date.*Between) scripting functions.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 25, 2018, 11:16pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/3 "2018-02-25T23:16:07Z")

</div>

I am wanting to keep track of hours when a button is pressed for a month. example press button, start counting, 1 hour 20min elapsed, stop counter. Do that off and on for a month. Subtract that time elasped from the monthly hours. Clear total a the beginning of the next month

---

<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:** [February 25, 2018, 11:42pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/4 "2018-02-25T23:42:25Z")

</div>

OK, a manual OEE adjustment.  
So, I would put the button status into a boolean memory tag (unless there’s a PLC bit that has it). I would make a dataset memory tag to hold the most recent down and up timestamps and use a gateway tag change event script to compute and populate the dataset. I would generate a [now(), NULL] when starting a down period (inserting that in the DB), then changing to [previous, now()] when ending a down period (updating the previous DB record). Your monthly analysis would sum those recorded intervals.

---

<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:** [February 25, 2018, 11:46pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/5 "2018-02-25T23:46:19Z")

</div>

Oh, and the “initialChange” event would retrieve the most recent DB record before running the rest of the logic to make sure gateway restarts are handled correctly.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 25, 2018, 11:51pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/6 "2018-02-25T23:51:30Z")

</div>

There is no PLC, It is for Mobile equipment. I have a memory tag setup for each. But I am unsure on how to do the scripting to get the time in hours that I need to to my memory tag.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [February 25, 2018, 11:57pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/7 "2018-02-25T23:57:08Z")

</div>

I have the scripting set up to calculate the monthly hours based on the month and have it writing to a tag.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [March 2, 2018, 2:08pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/8 "2018-03-02T14:08:47Z")

</div>

> [@pturmel](#):
>
> So, I would put the button status into a boolean memory tag (unless there's a PLC bit that has it). I would make a dataset memory tag to hold the most recent down and up timestamps and use a gateway tag change event script to compute and populate the dataset. I would generate a [now(), NULL] when starting a down period (inserting that in the DB), then changing to [previous, now()] when ending a down period (updating the previous DB record). Your monthly analysis would sum those recorded intervals.

How would I go about this?

---

<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:** [March 2, 2018, 4:20pm UTC](https://forum.inductiveautomation.com/t/timer-component/17125/9 "2018-03-02T16:20:19Z")

</div>

> [@jshaffer09](#):
>
> How would I go about this?

Start with a table containing two timestamp columns (or datetime columns, depending on your preferred platform). Something like this:

```SQL
CREATE TABLE public.downtime
(
    down_ts timestamp(3) with time zone NOT NULL,
    up_ts timestamp(3) with time zone,
    CONSTRAINT downtime_pkey PRIMARY KEY (down_ts)
);

CREATE INDEX downtime_end_ndx
    ON public.downtime (up_ts);

```

Then create the memory tags, in my case the boolean {Forum/Down} and the dataset {Forum/DownTS}.  
Then under Project =\> Scripts =\> Gateway Event Scripts =\> Tag Change, create a new entry with your boolean tag in the Tag Path(s) list, and the following code:

```auto
if initialChange:
	# Establish initial conditions from the database
	pyDS = system.db.runQuery("""Select * From downtime Order By down_ts Desc Limit 1""", "Forum")
	ds = pyDS.underlyingDataset
else:
	# Use the existing memory tag's timestamps for efficiency
	ds = system.tag.read("[default]Forum/DownTS").value

if newValue.value:
	# Down now. Need to start a downtime event if not already in one.
	if ds.rowCount<1 or ds.getValueAt(0, "up_ts"):
		ts = system.date.now()
		ds = system.dataset.toDataSet(['down_ts', 'up_ts'], [[ts, None]])
		system.db.runPrepUpdate("""Insert Into downtime (down_ts) Values (?)""", [ts], "Forum")
else:
	# Not down now. Need to finish a downtime event if in one.
	if ds.rowCount and not ds.getValueAt(0, "up_ts"):
		ts = system.date.now()
		ds = system.dataset.setValue(ds, 0, "up_ts", ts)
		system.db.runPrepUpdate("""Update downtime Set up_ts=? Where down_ts=?""", [ts, ds.getValueAt(0, "down_ts")], "Forum")

# Whatever ends up in ds is saved to the memory tag for the next event
system.tag.write("[default]Forum/DownTS", ds)

```
