Dashboard custom block - Event query by dashboard time

Hello,

I’m trying to create a dashboard Custom HTML block with the Event Query using the dashboard time properties to get only the events within the current dashboard duration, as follows:

{
  "$and": [
    {
      "deviceId": {
        "$eq": "{{ctx.deviceId}}"
      }
    },
    {
      "creationDate": {
        "$lte": {{formatDate dashboard.time 'x'}}
      }
    },
    {
      "creationDate": {
        "$gt": {{formatDate (subtract dashboard.time dashboard.duration) 'x'}}
      }
    }
  ]
}

The query is ok, but the problem is the {{dashboard.time}} property returns null. To check this, I used the whole dashboard object in the Sort Field to inspect it:

How can i achieve this query? I already read some similar topics in the forum, but i found only topics asking about queries relative to the current time.

Thanks.

We only set the dashboard.time property when viewing a past dashboard state. So for your use case, we can use a combination of the {{defaultTo}} and {{currentDateTime}} helpers to get around this.

{
  "$and": [
    {
      "deviceId": {
        "$eq": "{{ctx.deviceId}}"
      }
    },
    {
      "creationDate": {
        "$lte": {{formatDate (defaultTo dashboard.time (currentDateTime)) 'x'}}
      }
    },
    {
      "creationDate": {
        "$gt": {{subtract (formatDate (defaultTo dashboard.time (currentDateTime)) 'x') dashboard.duration}}
      }
    }
  ]
}

However, while verifying this, I realized that we are only ever using the default duration set on the dashboard - not the value the user has currently selected in the Losant UI. So I have the team working on a fix for that currently.

1 Like

It works perfectly, thank you!

Tuesday’s platform update includes a fix for the issue described here, where the value of dashboard.duration and dashboard.resolution now resolves to the user’s currently selected value, and not just the default value set on the dashboard.

1 Like