How to check if date is Null in expression language

Hello all,

I am trying to apply color to a component based on the returned value of a named query (in this case date) I wrote my script as is suggested by the ignition manual however I get a syntax error. This script should work but I cannot figure out why it is not working. I wrote this script on the expression binding of a color property. My “test.DateQuery” is also a property.

if isNull(({test.DateQuery}),
	color(255,0,0)
	)

A couple things:

  1. You need parentheses for your if expression. if(isNull(…))
  2. You need an “else” value for your if expression. In other words, what color do you return for your text if the {test.DateQuery} is NOT null?

Thank you!
I modified this to:

if (isNull({test.DateQuery}), color(255,0,0,0), color(255,230,30,0)
	)

I do not get errors but it is not also working the way I was expecting it.

So if my date query returns 2 dates instead of single date, would this work?

Your expression will treat 2 dates the same as 1 date, or 1000. ANY value returned from the query ( including 0 rows!) is non-null.

Not exactly sure what you are trying to capture, but if you are looking to differentiate between 1 result and multiple results then you may need to check the length of the query result; ie if(len({test.DateQuery})!=1,color(255,0,0),<other color>)

1 Like

Thank you so much Dustin!
That seems to be working. However, the color changed to red but it remains unchanged if the date is null or not null.

I did see a difference only. When the date is not null I get this f(x) at the top!

image

What I am trying to do is that I am trying to change the color property of the component based on a returned value of the named query (my date value). The returned date can be one value or multiple values.

The f(x) overlay is because your query is null, and indicates bad quality. If you want to avoid this, you can check “overlay opt-out” on the binding of the component value. This will remove that bad quality overlay when the query result is null.

If you want to change the color of the component based on the value returned, regardless of the number of values returned, then you will need that logic in your if() expression. However, if you have multiple potential values from your query, then you might need a script transform to parse through your values for the one you want to compare against.

1 Like

Thank you very much, Dustin. Appreciate your help.
The thing is when my query returns no row I get an error.

“Named Query returned no rows on component”

Even though I turned on that overlayOPT option, I do still get the error.

If I want to be specific:

I have a named query with two parameters. I get those two parameters from two tags. If my query returns no row I want a color property to be red otherwise if it returns at least one row I want the color to be yellow.

Currently I am using this:

if(isNull({test.DateQuery}),color(255,0,0),color(212,230,30))

It returns the color yellow if we have rows but the red color comes in with an error since it returns no row.

Use len(). A named query returns a dataset (or a json object in Perspective). No rows is still a valid dataset, just not valid to pull out a result.

Thank you!
I used len() too but it did not even change the color:
if(len({test.DateQuery}) !=1,color(255,0,0),(212,230,30))

You are missing the color function name for the else value. Are you getting errors when you try to apply these expressions?

1 Like

Sorry that was a mistake from my side when I tried to copy. I do have the color function:

if(len({test.DateQuery}) !=1,color(255,0,0),color(212,230,30))

By having this expression I do not see any color change and also I get the error that:
Named Query returned no rows on component

I applied this code instead:
if(isNull({test.DateQuery}),color(255,0,0),color(212,230,30))

I do see the color changes however I still get that named query error and this on my component:

image

Show your property inspector where {test.DateQuery} is bound. Can you open the dataset viewer there to see the output from your named query?

1 Like

This is what I am doing. I have a template called “test” and I have a named query with two parameters and I get the value of the parameters from two memory tags.

The template has an internal property called DateQuery and its data is bound to that named query.

I have the expression on the background color of my template. The DateQuery can return multiple rows or no rows.

I did not quite understand what you mean by dataset viewer.

Your DateQuery property is a date–I was expecting it to be a dataset. Show your binding for DateQuery.

1 Like

you are correct, I changed the date to be dataset and I user len() in my expression instead of isNull but it did not work.

This is the binding on the named query.
I get the correct value on my named query. However, my problem is with when the named query returns no row.

I got it working. It seems that I had a syntax error in my code. but it is fixed now. Thank you everyone

3 Likes

Ps you can also compare the value to None too.
E. G.

{test.date} = None
1 Like

Thank you!

Yes, This Works but I am getting a result where length of the Dataset is 1 but it doesnt holds any value, so here how can we check? If I am checking with isNull its not giving the proper result as well

You could add indexing to check the length of specific elements in the dataset

Example:


len({Root Container.Power Table.data}) will produce a value of 1 because the length of the dataset is one element, but if I spot check the first row of the first column by adding brackets to the end of the expression, I will get a value of zero because there is no actual value at [row 0, column 0]: `len({Root Container.Power Table.data}[0,0])