Alert rule on series of timestamps

Trying to create an alert, when one of 5 temperature-sensors are not responding any more.
For this i use Infinity-Datasource and i grab the timestamps of the last update of all 5 objects in iobroker.


Picture shows the result: One single value per sensor, so i have a series of 5 values.

Now i want to compare to actual time… the only way to get the time was to use a MySQL datasource and to ask for unix_timestamp.
grafik
Picture shows the result: One single value with actual time.

Now i create a math expresion to compare the 5 sensorvalues against actual time and define a threshold.


This picture shows that all 5 values of the sensors show no alert, everything seems to be ok.

But when saving and waiting for some reaction, it turns into an error message:

Where is my problem?
I assume it is the mismatch that i have 5 values in $D and only one in $A ???

Any way out of that?

Hi,

if you’re using Infinity Plugin datasource, is your data coming from an rest API? If it returns JSON and you could use Backend parser, you could get current time from JSONata (using $now()) and subtract the returned timestamps from $now. That way you would do everything in one datasource.

I was searching for a list of functions in jsonata yesterday but did not find any… so thanks for information.
But how to use it?

If i put only “ts” in that Rows/Root window, then i see 5 ts-values below.
If i add the $now() i get a errormessage:

[sse.dataQueryError] failed to execute query [A]: error while performing the infinity query. error converting json data to frame: error evaluating JSONata expression left side of the “-” operator must evaluate to a number, position:7, arguments:

Am i doing something wrong ???

ah… got it… i do not need $now() i need $millis()…

Hmmm…
still issue with generating the correct output:

“$millis() - ts” works if the URL delivers one ts-value.
The result of the URL is with one value:
[{“id”:“mqtt.0.arduino.esp-c9cdb7.283C7734000000F4”,“val”:38.69,“ts”:1739451235918,“ack”:true}]

When selecting more values, for all 5 sensors, the result is:
[{“id”:“mqtt.0.arduino.esp-c9cdb7.283C7734000000F4”,“val”:39.06,“ts”:1739444929078,“ack”:true},{“id”:“mqtt.0.arduino.esp-c9cdb7.28A2EE370000009A”,“val”:33.44,“ts”:1739444929087,“ack”:true},{“id”:“mqtt.0.arduino.esp-c9cdb7.28FF64182188D2EF”,“val”:37,“ts”:1739444929129,“ack”:true},{“id”:“mqtt.0.arduino.esp-c9cdb7.28FF6418218C247D”,“val”:28.38,“ts”:1739444929129,“ack”:true},{“id”:“mqtt.0.arduino.esp-c9cdb7.28FF641821B04985”,“val”:36.12,“ts”:1739444929129,“ack”:true}]

and then the
“$millis() - ts”
does not work any more, it creates the errormessage
[sse.dataQueryError] failed to execute query [A]: error while performing the infinity query. error converting json data to frame: error evaluating JSONata expression right side of the “-” operator must evaluate to a number, position:0, arguments:

I have similar effect when using https://try.jsonata.org/… so i assume i need to modify syntax but can’t find the solution so far…

still not able to use a series of timestamps as source of alert in addition with using JSONata.
Reading one timestamp and using “$millis() - ts” works perfect.
Reading a series of timestamps works as well, but then i cannot use JSONata any more.

Seems as if i do not understand something with combining these 2 methods.

Any help would be very appreciated.

I think it’s because of the array. You need to point jsonata to iterate over the array, like this:

$.{"time": $millis() - ts}

https://try.jsonata.org/FdCIMG0Rl

You are right… but i am obviously not able to come around the iteration syntax.
Have modified your example to exactly show which structure i receive from the query… in my case the blocks have a name and are enclosed by another {}
https://try.jsonata.org/3cjOtpHSO

I even do not find an area in the docs of jsonata which seem to describe this situation…

Ok, so that shows some light - I thought the api returned array, but in this example it’s object of objects. Something like this https://try.jsonata.org/3cjOtpHSO should work then

Thanks for assisting me… but the link does show my example, it is the same hyperlink. You probably need to create a new share-link ? Or simply paste the code in here ?

Sorry, I thought I did create a new link, werid. Anyway, here’s the code:

$each($, function($value, $key) {
    {
        "sensor": $key,
        "value": $millis() - $value.ts
    }
})

and it produces something like:

[
  {
    "sensor": "mqtt.0",
    "value": 113425057
  },
  {
    "sensor": "mqtt.1",
    "value": 113425057
  },
  {
    "sensor": "mqtt.2",
    "value": 113425056
  }
]

First of all: Thanks, it works !!!

Reviewing this thread, i have to admit that at some point i did mix up things.
So here is my wrap-up:
https://try.jsonata.org/4zuMXXDS2

  • this is generated by simple-api from iobroker using the “states”-command and generates an object of objects
  • This one works well with your last proposal and also works within the alert rule

https://try.jsonata.org/899emmzs3

  • this is generated by simple-api from iobroker using the “getBulk”-command and generates an array of objects…
  • This one works well with your first proposal within JSONata Exerciser… but not within the alert rule. The table says “No data”.

While i am happy that i have reached my goal to set up altering on 6 temperatur-sensors in one rule… i still would like to learn.

  • is that correct that the last syntax is a array of objects ?
  • any idea why it works in JSONata Exerciser but not in grafana alering ?

Feel free to not further investigate in this case if your schedule if fully packed. Again, thanks so far :+1: