How to get latest node metric value from the neDB database? [solution]

Started by Kylix, October 20, 2016, 06:35:10 PM

Kylix

How could I get the last values inserted into the DB for a certain node? (some sort of a query).

Thanks

Felix

Last record of what?
Last inserted record?
Is this while the app runs (inside the app)? Or just query the db from the pi console?

For last inserted record you'd just query for your node Id, which gives you the last entry automatically.
Regardless, you can look at the neDB API for query options or see the many examples in gateway.js.

Kylix

Practically, I'd like to create a Telegram Bot that answers to a few commands like:
/temperature .../humidity ....

For doing this, I'd need to query the database for the last temperature value of a node and so on ...

I'll search in the gateway.js file and hope to understand how do you pass the query result into a variable.
By the way, I thought you don't use neDB anymore in the last version of the Gateway.


Felix

neDB is used for storing the latest status and metric values of each node. So simply searching by Id will return the last value received from that node.
The binary files database stores logged historical data for each node-metrics where this feature was turned ON.

So to get the latest values of a node just query the neDB database.
You could create an HTTP endpoint that responds to REST queries, which in turn searches the neDB object like this:

    var searchId = 123;
    var metricKey = "T";
    db.find({ _id : searchId }, function (err, results) {
      if (results.length == 1)
      {
        var dbNode = results[0];
        console.log('Temperature is: ' + dbNode.metrics[metricKey]);
      }
    });


Other available node metric properties:

            dbNode.metrics[metricKey].label (string)
            dbNode.metrics[metricKey].value (decimal)
            dbNode.metrics[metricKey].unit (string, optional)
            dbNode.metrics[metricKey].updated (unix timestamp, secconds)
            dbNode.metrics[metricKey].pin (bool, optional)
            dbNode.metrics[metricKey].graph (bool, optional)




Kylix

Thanks! I have now the Telegram Bot up and running 8)