{"id":1269,"date":"2020-12-04T10:20:57","date_gmt":"2020-12-04T09:20:57","guid":{"rendered":"https:\/\/rent-a-pilot.nl\/?p=1269"},"modified":"2021-05-02T13:18:57","modified_gmt":"2021-05-02T11:18:57","slug":"node-red-and-infuxdb-tags-for-counting-aircraft-or-anything-else","status":"publish","type":"post","link":"https:\/\/rent-a-pilot.nl\/nl\/node-red-and-infuxdb-tags-for-counting-aircraft-or-anything-else\/","title":{"rendered":"Dingen tellen in Grafana - Hoe Node-Red gebruiken om de \"Group by TAG\" nuttig te maken in Influxdb"},"content":{"rendered":"\n\n\n\n\n\n\n\n\n<h2>ADS-B Grafana: Make lists of \u00a0types, calsigns and more<\/h2>\n<p>The other day I ran into a limitation of Influxdb with Grafana. I have an Influxdb that I feed with data from my ADS-B receiver. This feed contains data like<br \/> Reg: PH-AOA<br \/> Type: A332<br \/> What I wanted to do is count the number of UNIQUE Airbus A330-203&#8217;s.<\/p>\n<h3>The end goal<\/h3>\n<p><strong>TEN hits<\/strong> on <strong>PH-AOE<\/strong> A332 must be counted as\u00a0<strong>ONE A320-203<\/strong> and <strong>NOT ten!<\/strong><\/p>\n<p><span style=\"font-size: 14px;\">To do that you need to create a query with &#8220;GROUP BY&#8221;. Easy enough. Now here comes the problem: You cannot do a &#8220;GROUP BY&#8221; in Influxdb that connect two values. You can only do that with a TAG.<\/span><\/p>\n<h3>Influxdb query &#8211; group by tag: actype<\/h3>\n<p>The query looks like this:<\/p>\n<p>SELECT count(distinct(&#8220;Reg&#8221;)) FROM &#8220;mesg&#8221; WHERE $timeFilter GROUP BY time(1d), &#8220;actype&#8221;<\/p>\n<h3>We need a TAG to group these entries<\/h3>\n<p>This tag &#8220;actype&#8221; does not exist in the datafeed so it must be created. I already use node red to collect and organise the feed so it makes sense to to it there.<\/p><h2>Node-Red to the rescue<\/h2>\n<p>The ADS-B receiver on the Pi produces the above JSON-feed that after a bit of tinkering looks like this:<\/p><h3>How to add a TAG-value for Influxdb with a Node-Red function node<\/h3>\n<p>&nbsp;<\/p><p>msg.payload=[msg.payload,{tag:value}];<\/p>\n<p>&nbsp;<\/p>\n<h4>Example<\/h4>\n<p>To add two tags:<\/p>\n<ul>\n<li>Add this tag &#8220;icao24&#8221; from value &#8220;msg.payload.Icao&#8221;<\/li>\n<li>Add this tag &#8220;actype&#8221; from value \u00a0&#8220;msg.payload.Mdl&#8221;<\/li>\n<\/ul>\n<p>Do this:<\/p>\n<p>msg.payload=[msg.payload,{icao24:msg.payload.Icao},{actype:msg.payload.Mdl}];<br \/> return msg<\/p>\n<p>&nbsp;<\/p>\n<h3>You can now do this query in Grafana:<\/h3>\n<p>SELECT count(distinct(&#8220;Reg&#8221;)) FROM &#8220;mesg&#8221; WHERE $timeFilter GROUP BY time(1d), &#8220;actype&#8221;<\/p><p>The result is an aggregated list of aircraft types from unique registrations over a period of 1 day.<\/p>\n\n\n\n\n\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>Dit maakt het mogelijk om voorkomens van gecombineerde gegevens in Grafana weer te geven. Een vitale stap voor het tellen van de vliegtuigtypes in het mode-S project.<\/p>","protected":false},"author":1,"featured_media":1313,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[34,9,33,20,28],"tags":[42,44,43],"class_list":["post-1269","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-adsb","category-airplanes","category-avionics","category-internet","category-radio","tag-grafana","tag-influxdb","tag-nodered"],"_links":{"self":[{"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/posts\/1269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/comments?post=1269"}],"version-history":[{"count":33,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/posts\/1269\/revisions"}],"predecessor-version":[{"id":2064,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/posts\/1269\/revisions\/2064"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/media\/1313"}],"wp:attachment":[{"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/media?parent=1269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/categories?post=1269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rent-a-pilot.nl\/nl\/wp-json\/wp\/v2\/tags?post=1269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}