Timer Component

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

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() expression function, the system.date.now() scripting function, and the system.date.*Between() scripting functions.

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

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.

1 Like

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.

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.

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

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:

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:

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
	# 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")
	# 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)