Tag value automatically update based on Day

Hello.
I would like to have my memory tag value (integer) automatically updated based on the Day
Monday = set tag value to 1
Tuesday = set tag value to 2
Wednesday = set tag value to 3
Thursday = set tag value to 4
Friday = set tag value to 5
Saturday = set tag value to 6
Sunday = set tag value to 7

And it will only take effect after 06.00am.
Is that possible ?

You could do that with an expression tag

The expression:
dateFormat(now(1000), “u”)
returns the day number (today friday it returns 5)

you’ll need some more logic if you want it to only update at 6am.
and maybe adjust the update time (now(1000)) if it doesnt have to update every second

https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax
https://docs.inductiveautomation.com/display/DOC81/Data+Type+Formatting+Reference

thanks victordcq.

Am still figuring out how to return value 6 AFTER saturday 6am and not saturday 12am. :slight_smile:

.

Try: dateFormat(addHours(now(1000), -6), “u”)

perfect WillMT10 , thanks !
one more question if you don’t mind, can I add minutes as well ? if lets say for 5:30am ? :slight_smile:

Yup: add* - Ignition User Manual 8.1 - Ignition Documentation

You can chain them together (or convert hours to minutes and just subtract minutes):

dateFormat(addMinutes(addHours(now(1000), -5), -30), “u”)
1 Like

Hello, would like to find out is that possible and what is the best way to do it ?
I would like to have my only one memory tag value (integer) automatically updated based on the Day and time as below.
Monday (07:00 to 14:59) = set tag value to 1
Monday (14:59 to 22:59) = set tag value to 2
Monday (23:00 to 06:59) = set tag value to 3
Tuesday (07:00 to 14:59) = set tag value to 4
Tuesday (14:59 to 22:59) = set tag value to 5
Tuesday (23:00 to 06:59) = set tag value to 6
and so on till Sunday

Create a timer gateway event, this should run every second:
image

Then add the script (I don’t guarantee this would work) and edit your desired tag path:

#create a class to define a datetime based on a time
class Time:
	def __init__(self,hours,minutes,seconds,nextDay=False):
		self.hours = hours
		self.minutes = minutes
		self.seconds = seconds
		self.nextDay = nextDay
	
	def datetime(self):
		date = system.date.now()
		date = system.date.midnight(date)
		secHours = self.hours * 3600
		secMins = self.minutes * 60
		secCounts = secHours+secMins+self.seconds
		if self.nextDay == True:
			secCounts = secCounts+(3600*24)			
		return system.date.addSeconds(date,secCounts)

#create	shifts
shift1 = [Time(7,0,0).datetime(), Time(14,59,59).datetime(), 1]
shift2 = [Time(15,0,0).datetime(), Time(22,59,59).datetime(), 2]
shift3 = [Time(23,0,0).datetime(), Time(6,59,59,True).datetime(), 3] #has a next day in end of shift

# function to return values based on datime
def getValue(shift):
	def getYesterday(datetime):
		return system.date.addDays(datetime,-1)
	now = system.date.now()
	day = system.date.getDayOfWeek(now)-1
	if now >= shift[0] and now <= shift[1]:
		return 3*day+shift[2]
	#handle limbo time
	else:
		yesterday = getYesterday(now)
		yesterdayDay = system.date.getDayOfWeek(yesterday)-1
		if now >= getYesterday(shift[0]) and now <= getYesterday(shift[1]):
			if yesterdayDay-1 >= 0:
				return 3*(yesterdayDay-1)+shift[2]
			else:
				return 21
		else:
			return None

#write to tag
shifts = [shift1, shift2, shift3]
tagPath = "[default]tag/path" #EDIT*****
for s in shifts:
	value = getValue(s)
	if value is not None:
		system.tag.writeBlocking([tagPath],[value])

Hi jespinmartin1 .

Sorry if I did not mention earlier, but unfortunately I am using Ignition Edge without gateway event script.

So create it on the script library, then change your tag from memory to expression type and call the function with runScript - Ignition User Manual 8.0 - Ignition Documentation (inductiveautomation.com).
Make sure to have a 1s scan class assigned to this tag
Finally, edit the script to return the value instead of writing to tag
or
Create an aux tag to store calls from parent tag and avoid doing mayor edits on the script.

Two expression tags:

Week Start: (Monday at 7am)

addHours(
		 addDays(midnight(now()), -(getDayOfWeek(now())-2)) // Find Monday at midnight
		 , 7)	// Add 7 hours to get to 7am

Shift: Since we have 21 evenly spaced shifts, we can use a modulus on the hours between the start of the work week and now.
hoursBetween({[~]Week Start}, now()) % 21 + 1

(Now tag added for reference)
image

1 Like

hmm, thanks JordanCClark

surprisingly I am getting value 4. anything I did wrong ? isn’t i am suppose to get 11 ?

image

yes xd @JordanCClark code works for me.
Tho it is a shift off because of timezone difference, which is weird sinze i run on a local gateway… yet somehow my now() is 8h wrong xd

Try putting Week Start as a DateTime datatype