# Data aggregation and propagation across multi-level system device hierarchy

**URL:** <https://forums.losant.com/t/data-aggregation-and-propagation-across-multi-level-system-device-hierarchy/3882>\
**Category:** Help\
**Tags:** devices, system\
**Created:** [June 25, 2021, 5:06pm UTC](https://forums.losant.com/t/data-aggregation-and-propagation-across-multi-level-system-device-hierarchy/3882 "2021-06-25T17:06:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Mascardi](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nicolas\_Mascardi](https://forums.losant.com/u/Nicolas_Mascardi)\
**Post date:** [June 25, 2021, 5:06pm UTC](https://forums.losant.com/t/data-aggregation-and-propagation-across-multi-level-system-device-hierarchy/3882/1 "2021-06-25T17:06:33Z")

</div>

Hi all,

Our team is working on an application that needs to support the following structure:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/0/08e0646d89f6b8f73b5d49d56c14fb29f5334d61.png)

The leaf nodes of the tree are regular devices that report one attribute “color” that can change over time.  
While the none-leaf nodes represent the physical organization of those devices.  
Keep in mind that the depth of the tree can vary as well as the number of nodes per level.

One of the requirements of the application is to be able to aggregate the data of the devices at any level of the tree structure. For instance, a user may like to know how many devices were reported as “green” two days ago for State 2. Another user could ask how many devices were in “red” last week in City 4.

Our first approach was to implement a System Device for each none-leaf node in the tree hierarchy. Each System Device would automatically calculate the aggregations mentioned above based on the selected query interval.  
We realized that there is no aggregation that allows you to count the number of devices that were in a given color. So we replaced the color attribute of the devices with three booleans (red, blue, green). Depending on the color of the device is, one boolean will be true, the rest will set to false. So in the System Device we declared three attributes (red, blue, green) that would automatically calculate the sum of devices for each color accordingly.

Next thing we started to simulate data in order to test how aggregations work with several Devices and  
System Devices. But, when we tried to simulate data sent in the past we realize that System Devices do not track when the original state (from the Devices) was sent. So, if a Device (let’s say Device 1 in the picture) reports that yesterday it was “blue” the next System Device in the hierarchy (City 1) will assume that Device 1 is “blue” today.  
Sending data in the past is not only a simulation requirement. In a real scenario, we could also get Device state updates from previous days. What alternative do we have here? Should we use regular devices for the none-leaf nodes in the hierarchy too? If so, how should we trigger the aggregations now that is something that we should manage internally? Would the aggregation calculations scale if the number of devices and none-leaf nodes increase? Which is the best way of associating devices in a tree structure now that the child-parent functionality provided by the System Devices is not an option? Maybe using tags?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Dylan\_Schuster](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/dylan_schuster/32/2850_2.png) [@Dylan\_Schuster](https://forums.losant.com/u/Dylan_Schuster)\
**Post date:** [June 25, 2021, 6:16pm UTC](https://forums.losant.com/t/data-aggregation-and-propagation-across-multi-level-system-device-hierarchy/3882/3 "2021-06-25T18:16:14Z")

</div>

Systems would have been perfect for this use case if not for the requirement to recalculate the system aggregation for devices reported with a past timestamp.

My next suggestion was going to be device tags, where “Device 1” would get tagged with “city=City 1”, "state=“State 1”, etc., as you could then perform your data queries by passing in "state=“State 1” to get all devices under it. But this falls apart with your requirement of “Keep in mind that the depth of the tree can vary”, especially if the names of the depth nodes varies in different parts of the tree.

So, how about [experience groups](https://docs.losant.com/experiences/groups/)? They have a hierarchical structure and the depth can vary from group to group. Furthermore, devices can also be assigned to multiple groups at multiple levels, which is not something you have asked for but would be possible to expand to in the future.

The downside of using experience groups is not all parts of the Losant system support querying by [advanced device query](https://docs.losant.com/devices/device-queries/#advanced-queries) (and thus experience group) yet; so you could …

- Retrieve a list of devices and their meta info by experience group ID in the UI, the API, or the workflow editor
- Use the [Data: Gauge Query](https://docs.losant.com/workflows/data/gauge-query/) and [Data: Time Series](https://docs.losant.com/workflows/data/time-series/) workflow nodes or request a data export to your email using the Losant API’s [data.export](https://docs.losant.com/rest-api/data/#export) endpoint, but not use the Losant API’s [data.lastValueQuery](https://docs.losant.com/rest-api/data/#last-value-query) or [data.timeSeriesQuery](https://docs.losant.com/rest-api/data/#last-value-query) endpoints.

Please let us know if that would work for your use case.

---

<div class="post-metadata">

**Author:** ![Nicolas\_Mascardi](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@Nicolas\_Mascardi](https://forums.losant.com/u/Nicolas_Mascardi)\
**Post date:** [June 25, 2021, 6:47pm UTC](https://forums.losant.com/t/data-aggregation-and-propagation-across-multi-level-system-device-hierarchy/3882/4 "2021-06-25T18:47:59Z")

</div>

Thanks Dylan for your quick reply.  
We thought about Experience Groups, but there is a limitation about the number of child groups that a given parent group can have. For instance. in the example above if the number of cities down State 1 is greater than 100 and I try to get all the aggregated data for State 1 using either the Gauge Query or the Time Series nodes, this error occurs:

> [@experienceUserId: Cannot resolve to more than 100 device query components](https://forums.losant.com/t/experienceuserid-cannot-resolve-to-more-than-100-device-query-components/3060):
>
> Hi, We are developing a multi tenant application, and the following error is occurring for users who have access to many groups, when I use the filter by experienceUserId. This error means that it is not possible to load users’ devices, as they have access to more than 100 experienceGroups (root or parent groups)? So I should limit the membership of users to new groups in order not to go beyond that limit, right? Failed to load. experienceUserId: Cannot resolve to more than 100 device query co…

That’s why we were looking to use System Devices, which we think there is no limitation about the number of nodes within the tree structure.
