# Expression code isn't working but should be

**URL:** <https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572>\
**Category:** Ignition\
**Created:** [September 26, 2019, 11:34am UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572 "2019-09-26T11:34:43Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmerola](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mmerola](https://forum.inductiveautomation.com/u/mmerola)\
**Post date:** [September 26, 2019, 11:34am UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/1 "2019-09-26T11:34:43Z")

</div>

I have an expression tag that is toggling a bit and holding it on for a few seconds using the following code: (getHour24(now())) = 6 && (getMinute(now())) = 0 && (getSecond(now())) \<= 5  
As far as I understand this it should be turning on my bit for 5 seconds at 6am, which is what I’m trying to do, but while testing with different values I haven’t been able to get the bit to change state. When I remove the gethour24 function it triggers no problem every time but its not ideal because its every hour instead of just at 6am. I am hoping that someone with more ignition experience can point out something that I may be missing or a syntax error.

---

<div class="post-metadata">

**Author:** ![grietveld](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grietveld/32/6984_2.png) [@grietveld](https://forum.inductiveautomation.com/u/grietveld)\
**Post date:** [September 26, 2019, 12:03pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/2 "2019-09-26T12:03:36Z")

</div>

> [@mmerola](#):
>
> (getHour24(now())) = 6 && (getMinute(now())) = 0 && (getSecond(now())) \<= 5

I would take a look at your Execution Mode, and probably set it to a fixed rate.

The expression code seems to be valid since I copied it into my system and only tweaked the number values to get a trigger that would work.

---

<div class="post-metadata">

**Author:** ![mmerola](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mmerola](https://forum.inductiveautomation.com/u/mmerola)\
**Post date:** [September 26, 2019, 12:27pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/3 "2019-09-26T12:27:02Z")

</div>

Ok thanks for your help, I knew my code should work.  
But I’m afraid I don’t know what you mean by Execution Mode, I’ve never heard that term before and a quick google search didn’t yield much.  
Also here is some more info that might be helpful. The reason I am toggling a bit is so that I can run a tag event script on it’s value changing. Not sure if that would impact the execution of the tag’s expression though.

---

<div class="post-metadata">

**Author:** ![grietveld](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/grietveld/32/6984_2.png) [@grietveld](https://forum.inductiveautomation.com/u/grietveld)\
**Post date:** [September 26, 2019, 12:40pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/4 "2019-09-26T12:40:14Z")

</div>

This is from 8.0, ~~but there should be a setting similar in 7.9~~. In theory, I would think event driven would work, but Fixed Rate will re-evaluate at the interval you choose.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/a09b5fa48a1ba52cb065317987f34359cd538408.png)

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 26, 2019, 12:47pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/5 "2019-09-26T12:47:20Z")

</div>

I tried your expression as well, it worked fine for me on 7.9.12. Check the scan class on the tag, it should be a second or less.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 26, 2019, 12:54pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/6 "2019-09-26T12:54:50Z")

</div>

For completeness, 7.9 does not have Execution mode. All tags are assigned to a scan class.

For this type of application, I use an expression tag that returns the time as a string. I call mine CurrentTime:

```auto
dateFormat(now(), "HH:mm:ss")

```

Then, for every place I need a trigger based on a time, it expression would look like:

```auto
timeBetween({[~]CurrentTime}, "6:00:00", "6:00:05")

```

This makes it easier to read, because I’m old, and I don’t want to use too many brain cells decoding what I wrote. 🙂

---

<div class="post-metadata">

**Author:** ![mmerola](https://avatars.discourse-cdn.com/v4/letter/m/b5ac83/32.png) [@mmerola](https://forum.inductiveautomation.com/u/mmerola)\
**Post date:** [September 26, 2019, 1:04pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/7 "2019-09-26T13:04:00Z")

</div>

Thank you, I figured out what my problem was and why it wasn’t working thanks to your current time example. Turns out the time on the tag servers is not the same as my local desktop which is why it wouldn’t work as expected when I expected it. Thanks again everyone for your help.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 26, 2019, 1:18pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/8 "2019-09-26T13:18:38Z")

</div>

I was curious about your solution, so I tried it. It doesn’t work in the timeBetween tag. It says cannot coerce {[~]CurrentTime} to java.util.date. The only way the current time tag works is configured as string though.

---

<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:** [September 26, 2019, 1:39pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/9 "2019-09-26T13:39:27Z")

</div>

Every time I encounter this sort of thing, I ask: _ **why** _ do you want that boolean to turn on for a few seconds at that time of day? The answer is almost always: I/We want to run X every day at that time, using a tag change event.

Getting this to work this way requires the sun, the moon, and the stars to all align (funky expression, scan class timing, and event script that checks for initial conditions and anti-repeats). A better solution is to use a gateway timer event paired with a Date memory tag. The memory tag holds the last execution timestamp. The timer script checks current timestamp against the last execution (to prevent double runs) and checks for the appropriate hour/minute/second. When it decides to run again, it starts by updating the memory tag. This approach can be used for once-a-day, once-a-shift, once-an-hour, once-a-minute, or any other “once per” you can conceive. And this approach keeps all relevant logic and timing criteria in a single, easily maintained place.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [September 26, 2019, 1:47pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/10 "2019-09-26T13:47:14Z")

</div>

Seems like I remember this coming up before, here was some script written by @pturmel. Personally, I wouldn’t mind ignition having a linux type crontab for these kinds of things

```auto
from java.util import Date
lastTrigPath = '[default]some/path/to/datetime/tag'

# Simple function to convert any timestamp to the trigger point on the same day
def truncateToTrigger(ts):
	return Date(ts.year, ts.month, ts.date, 16, 0, 0)

# Check if time to run the triggered event. Call from timer script
def checkTrigger():
	last = system.tag.read(lastTrigPath).value
	nextTrig = truncateToTrigger(last)
	if nextTrig.time < last.time:
		nextTrig = system.date.addDays(nextTrig, 1)
	now = Date()
	if nextTrig.time <= now.time:
		system.tag.write(lastTrigPath, now)
		# Do the trigger stuff here

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 26, 2019, 1:49pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/11 "2019-09-26T13:49:19Z")

</div>

Well, yes, because timeBetween expects a string. Good catch, though. 🙂

---

<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:** [September 26, 2019, 1:57pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/12 "2019-09-26T13:57:49Z")

</div>

> [@dkhayes117](#):
>
> linux type crontab

I still can't log in to the ideas portal, or I'd make a formal suggestion there. I agree that timer events ought to have this as an option.

(After logging in to the ideas portal via the shared account page, the redirect back to [canny.io](http://canny.io) still shows not logged in. Further attempts to login are just flipped back to [canny.io](http://canny.io), still not logged in. I suspect it is a cross-domain scripting issue.)

---

<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:** [September 26, 2019, 2:06pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/13 "2019-09-26T14:06:22Z")

</div>

Turns out there is a planned feature:

[https://inductiveautomation.canny.io/ignition-features-and-ideas/p/gateway-script-type-triggered-at-specific-time](https://inductiveautomation.canny.io/ignition-features-and-ideas/p/gateway-script-type-triggered-at-specific-time)

I even commented on it back when I could still log in.

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [September 26, 2019, 2:16pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/14 "2019-09-26T14:16:25Z")

</div>

You also can schedule a blank report with a script execute for things too if you have the reporting module,

---

<div class="post-metadata">

**Author:** ![bfuson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bfuson/32/4597_2.png) [@bfuson](https://forum.inductiveautomation.com/u/bfuson)\
**Post date:** [September 26, 2019, 2:37pm UTC](https://forum.inductiveautomation.com/t/expression-code-isnt-working-but-should-be/28572/15 "2019-09-26T14:37:31Z")

</div>

Having done this before: if you do go this route, be sure to document it!

It’s not obvious to the next guy, or yourself at a later date, that scripts are being triggered by report schedules.
