# Tag Expression Question

**URL:** <https://forum.inductiveautomation.com/t/tag-expression-question/28020>\
**Category:** Ignition\
**Created:** [September 4, 2019, 11:33pm UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020 "2019-09-04T23:33:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryanastiefes](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ryanastiefes](https://forum.inductiveautomation.com/u/ryanastiefes)\
**Post date:** [September 4, 2019, 11:33pm UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/1 "2019-09-04T23:33:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [September 5, 2019, 1:13am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/2 "2019-09-05T01:13:44Z")

</div>

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.

> [@ryanastiefes](#):
>
> 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)

---

<div class="post-metadata">

**Author:** ![ryanastiefes](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ryanastiefes](https://forum.inductiveautomation.com/u/ryanastiefes)\
**Post date:** [September 5, 2019, 1:22am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/3 "2019-09-05T01:22:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [September 5, 2019, 1:28am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/4 "2019-09-05T01:28:19Z")

</div>

> [@ryanastiefes](#):
>
> 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)

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

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [September 5, 2019, 1:29am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/5 "2019-09-05T01:29:35Z")

</div>

You could make this easier to read like this:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![ryanastiefes](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ryanastiefes](https://forum.inductiveautomation.com/u/ryanastiefes)\
**Post date:** [September 5, 2019, 2:21am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/6 "2019-09-05T02:21:37Z")

</div>

I understand, I appreciate the quick response. Thank you

---

<div class="post-metadata">

**Author:** ![ryanastiefes](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@ryanastiefes](https://forum.inductiveautomation.com/u/ryanastiefes)\
**Post date:** [September 5, 2019, 2:22am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/7 "2019-09-05T02:22:53Z")

</div>

Do you know of any good websites for learning expressions?

---

<div class="post-metadata">

**Author:** ![witman](https://avatars.discourse-cdn.com/v4/letter/w/d2c977/32.png) [@witman](https://forum.inductiveautomation.com/u/witman)\
**Post date:** [September 5, 2019, 5:47am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/8 "2019-09-05T05:47:27Z")

</div>

See [Ignition Expression Overview and Syntax](https://docs.inductiveautomation.com/display/DOC80/Expression+Overview+and+Syntax) and refer to [Ignition Expression Functions](https://docs.inductiveautomation.com/display/DOC80/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](https://docs.inductiveautomation.com/display/DOC80/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.

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [September 5, 2019, 5:57am UTC](https://forum.inductiveautomation.com/t/tag-expression-question/28020/9 "2019-09-05T05:57:47Z")

</div>

> [@witman](#):
>
> If something doesn’t work, comment out everything except the first part and test that. If it works, add in one more piece and test again.

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.
