Select to view content in your preferred language

Use of Arcade to filter by a "sensor_id" to get right data for pop-ups across many point features

1284
2
Jump to solution
12-15-2023 12:43 PM
DavidFrisbey
Emerging Contributor
Hi Esri Community,
 
Goal is to return the 12 most recent hours per sensor($feature) from another hosted table inside a pop-up on my map.
 
On my map I have about 30 sensors that are point features.
 
The sensors have a unique sensor_id that is common across my map and the other hosted table.
 
The other hosted table is an archive of historical data, it contains many previous sensor values. 
 
The map only contains the most recent hour and is live according to a regularly scheduled task.
 
I can't seem to filter down my variable "last_twelve_hrs" by "sensor_id." I am stuck where I am getting the most recent hourly averages from all sensors in the hosted table instead of per sensor($feature). 
 
The next step, which I am also unsure about is how to present the most recent 12 hours per sensor($feature) so it can fit in a bar or line graph in a pop-up. 
 
Thanks for your help and time Esri Community!
 
 
ARCADE SCRIPT:
var p = Portal("https://arcgis.com");
var table = FeatureSetByPortalItem(
  p, "0749f4597e054caf809b5b55d0b869d4",
  0,
  ['sensor_id','time_stamp','pm25calibrated','avg_'],
  false
)

var last_twelve_hrs = []

//function right_sensor(i) {return i table_twelve['sensor_id'] = '$feature.sensor_id'}

var table_twelve = Filter(table, "avg_ = '12'")

for (var t in table_twelve) { 
//table_twelve = Filter(table_twelve, right_sensor)
last_twelve_hrs = Top(OrderBy(table_twelve,"time_stamp desc"),12)
}
return last_twelve_hrs
 
OUTPUT:
 
featureSet:
OBJECTID sensor_id time_stamp pm25calibrated avg_
281981"ANPZ34P3"Dec 15, 2023, 12:00:00 PM Pacific Standard Time5412
281982"AYF7FWBQ"Dec 15, 2023, 12:00:00 PM Pacific Standard Time8512
281983"AGFL7RGG"Dec 15, 2023, 12:00:00 PM Pacific Standard Time1712
281984"ACCGJ2KB"Dec 15, 2023, 12:00:00 PM Pacific Standard Time3112
281985"APPWHRLG"Dec 15, 2023, 12:00:00 PM Pacific Standard Time3812
281986"AHLN14H6"Dec 15, 2023, 12:00:00 PM Pacific Standard Time6112
281987"AWL3RVGH"Dec 15, 2023, 12:00:00 PM Pacific Standard Time4312
281988"AYW1PRKD"Dec 15, 2023, 12:00:00 PM Pacific Standard Time1912
281989"A4RRSN0B"Dec 15, 2023, 12:00:00 PM Pacific Standard Time3012
281990"A9BHX0FH"Dec 15, 2023, 12:00:00 PM Pacific Standard Time2212
281991"AQQ4M3SG"Dec 15, 2023, 12:00:00 PM Pacific Standard Time3712
281992"A3T32W43"Dec 15, 2023, 12:00:00 PM Pacific Standard Time4712
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Here's how to filter out the records to only show the last twelve readings from a particular sensor.

var p = Portal("https://arcgis.com");
var table = FeatureSetByPortalItem(
  p, "0749f4597e054caf809b5b55d0b869d4",
  0,
  ['sensor_id','time_stamp','pm25calibrated','avg_'],
  false
)
var sensor = $feature.sensor_id;

var table_twelve = Filter(table, "avg_ = '12' AND sensor_id = @sensor")
return Top(OrderBy(table_twelve,"time_stamp desc"),12)

View solution in original post

0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor

Here's how to filter out the records to only show the last twelve readings from a particular sensor.

var p = Portal("https://arcgis.com");
var table = FeatureSetByPortalItem(
  p, "0749f4597e054caf809b5b55d0b869d4",
  0,
  ['sensor_id','time_stamp','pm25calibrated','avg_'],
  false
)
var sensor = $feature.sensor_id;

var table_twelve = Filter(table, "avg_ = '12' AND sensor_id = @sensor")
return Top(OrderBy(table_twelve,"time_stamp desc"),12)
0 Kudos
DavidFrisbey
Emerging Contributor

That solution works, thanks again, Ken!

0 Kudos