Tag Expression Question

I have a boolean memory tag which I use to fire off a script. I am using the dateExtract() function to compare against hours in the day I would like the tag to go high. I finally got the expression to work but by accident, removing the open parenthesis. Here is what I have:

if(dateExtract(now(),“Hour”) = 4 || dateExtract(now(),“Hour”) = 8 || dateExtract(now(),“Hour”) = 12 ||
dateExtract(now(),“Hour”) = 16 || dateExtract(now(),“Hour”) = 20 &&
dateExtract(now(),“Minute”) = 1 && dateExtract(now(),“sec”)= 1,true,false)

That works. I dont understand why removing the open parenthesis allows this to work and when applying it like this:

if(dateExtract(now(),“Hour”) = 4 || (dateExtract(now(),“Hour”) = 8 || (dateExtract(now(),“Hour”) = 12 ||
(dateExtract(now(),“Hour”) = 16 || (dateExtract(now(),“Hour”) = 20 &&
(dateExtract(now(),“Minute”) = 1 && (dateExtract(now(),“sec”)= 1,true,false)

Doesnt work!! Also I wanted to make the expression fire off every three hours, but when I added in another or statement the expression would not fire.

Also if their is a better way of doing this I’m all ears. I wanted to do it by scripting but couldnt figure out how to do it on the memory tag.

Ignition 7.7.10

The reason the second expression you note did not work is you are missing closing parentheses for all the opening parentheses before all but the first dateExtract (highlighted with asterisks below). Every opening parenthesis requires a matching closing parenthesis.

The second expression does have opening parenthesis, and it doesnt work, only by removing the opening parenthesis is when it works??

Correct; the second one has opening parentheses with no matching closing parentheses. Every opening parenthesis requires a closing parenthesis.

You could make this easier to read like this:

dateExtract(now(), 'minute') = 1
&& dateExtract(now(), 'sec') = 1
&& case(dateExtract(now(), 'hour'),
	4, true,
	8, true,
	12, true,
	16, true,
	20, true,
	false
	)

Or possibly:

dateExtract(now(), 'minute') = 1
&& dateExtract(now(), 'sec') = 1
&& dateExtract(now(), 'hour') % 4 = 0

I understand, I appreciate the quick response. Thank you

1 Like

Do you know of any good websites for learning expressions?

See Ignition Expression Overview and Syntax and refer to Ignition Expression Functions for details on specific functions available. If something doesn’t work, comment out everything except the innermost part and test that. If that works, add in another piece and test again. Repeat until you find the problem. It’ll often be a missing comma, parenthesis, or argument.

You can nest expressions in multiple branches to create complex logic when required. Scripting is also an option in expressions (via runScript), but this case was simple enough I don’t see any reason for scripting. Complex expressions are much easier to follow if you organize them with indenting (something like I’ve done above), and–in some cases–break them down into simpler expressions on separate properties or tags that are then used in the final expression.

An alternative way is to create intermediary values in your tags or custom parameters.
In the example given, you could create intermediary values like isFirstSecond, isFirstMinute and is4thHour and use these in your final expression.

This will allow you to check the intermediary values, and see where it’s going wrong. It may be overkill for an expression that only turns out to be 5 lines long, but for more complicated ones, it’s certainly worth it.

PS. you don’t actually need the check on the seconds, and not even on the minutes. If you just check every forth hour, at the start of the hour, the tag will become true and fire the script. But it will remain true for the rest of the hour, so it will not fire any other scripts.

I’d even say that checking the seconds can be dangerous. If there are performance problems on your system, you can get a clock drift of a few seconds, and that will mean a few seconds will be skipped.

1 Like