Skip to main content
1-Visitor
January 21, 2021
Solved

How to get average of multiple Thermometers inside a ValueStream

  • January 21, 2021
  • 2 replies
  • 1433 views

Hello, I've been trying to experiment with multiple sensors and averaging their data to activate an AC.

 

Basically, I have a number of "Thermometer" Things with the same Template to which I have assigned a "TemperatureData" ValueStream. Of course, said temperature data is Logged in the Template.

 

For my application, I want to have a Service get the last entry from each individual thing, then average that data and give me a single reading.

 

How do I go about it?

My Thingworx version is 9.0

 

Many thanks in advance!

Best answer by Themis

Here is how I built my experiment.

  1. I created a ThingTemplate called ThermometerTemplate, which contains the important property, Temperature.
  2. I created a ValueStream called TemperatureData, which contains the averageTemperature property.
  3. I then assigned TemperatureData to the ThermometerTemplate.
  4. Then, I created the number of Thermometer_X Things I needed.
  5. Inside TemperatureData, I created a service that pretty much calculates the average of the
    Thing["Thermometer_X"].Temperature​

    and stores that average inside the averageTemperature property of the ValueStream.

  6. Then, for each Thing I created a Subscription that is triggered by a DataChange in each Thermometer's Temperature, and calles the service I made in Step 5.

I wish I was able to trigger the subscriptions internally with a DataChange in the logged data, but in the end, even if this very janky and not at all scalable, it worked just fine for the few Thermometers I needed to simulate.

 

2 replies

16-Pearl
January 22, 2021

Hello @Themis 

 

The average of data stream entries can be calculated easily with a custom service utilizing the built in code snippets. For eg Aggregate snippet under service can be used for the same.

 

Thanks

Om Dukiya

Themis1-VisitorAuthor
1-Visitor
January 23, 2021

Thanks for your answer!

 

I had a bit of trouble finding out how to trigger such a service through every recorded DataChange in all asociated Things' properties. However, I found I could just make as many Subscriptions as I need for each Thing I need to monitor. A bit clunky, but at least the average gets logged in the ValueStream. I'll post a more comprehensive answer and mark it as the solution.

Themis1-VisitorAuthorAnswer
1-Visitor
January 23, 2021

Here is how I built my experiment.

  1. I created a ThingTemplate called ThermometerTemplate, which contains the important property, Temperature.
  2. I created a ValueStream called TemperatureData, which contains the averageTemperature property.
  3. I then assigned TemperatureData to the ThermometerTemplate.
  4. Then, I created the number of Thermometer_X Things I needed.
  5. Inside TemperatureData, I created a service that pretty much calculates the average of the
    Thing["Thermometer_X"].Temperature​

    and stores that average inside the averageTemperature property of the ValueStream.

  6. Then, for each Thing I created a Subscription that is triggered by a DataChange in each Thermometer's Temperature, and calles the service I made in Step 5.

I wish I was able to trigger the subscriptions internally with a DataChange in the logged data, but in the end, even if this very janky and not at all scalable, it worked just fine for the few Thermometers I needed to simulate.