Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Hello everyone,
After a recent presales experience, I wanted to make my knowledge available to you. The core of this article is to demonstrate how you can format a Flux request in Thingworx and post it to InfluxDB, with the aim of reporting the need for performance in calculations to InfluxDB. The following context is renewable energy.
There is no detail about Kepware and how to connect to InfluxDB. As a prerequisite, you may like to read this article: Using Influx to store Value Stream properties from... - PTC Community
Introduction
The following InfluxDB usage has been developped for an electricity energy provider
We deal with Wind farm, Solar farm and Battery Storage.
Technical Context
Kepware is used as a source of data. A simulation for Wind assets based on excel file is configured, delivering sata in realtime.
SQL Database also gather the same data than the simulation in Kepware. It is used to load historical data into InfluxDB, addressing cases of temporary data loss. Once back online, SQL help to records the lost data in InfluxDB and computes the KPIs.
InfluxDB is used to store data overtime as well as calculated KPIs.
Invoicing third party system is simulated to get electricity price according time of the day.
Orchestration between TWX and InfluxDB
Thingworx v9.4.4
InfluxDB Cloud v2
Note: Free InfluxDB version is slower in write and read, and only 30 days data retention max.
Thingworx model and services
Thingworx context
Due to the fact relevant numeric properties are logged overtime, new KPIs are calculated based on the logged data. In the following example, each Wind asset triggered each minute a calculation to get the monetary gain based on current power produced and current electricity price.
The request is formated in Thingworx, pushed and executed in InfluxDB. Thus, Thingworx server memory is not used for this calculation.
Services breakdown
CalculateMonetaryKPIs
FormatFlux_CalculateMonetaryKPI
PostTextToInflux
Highlights - CalculateMonetaryKPIs
Find in attachments the full script in "CalculateMonetaryKPIs script.docx".
Url, token, organization and bucket are configured in the Persitence Provider used by the ValueStream. We dynamically get it from the ValueStream attached to this thing.
From here, we can reuse it to set the inputs of two other services using “MyConfig”.
Highlights - FormatFlux_CalculateMonetaryKPI
Find in attachments the full script in "FormatFlux_CalculateMonetaryKPI script.docx".
The major part of this script is a text, in Flux synthax, where we inject dynamic values. The service get the last values of ElectricityPrice, Power and Capacity to calculate ImmediateMonetaryGain, PotentialMaxMonetaryGain and PotentialMonetaryLoss.
Flux logic might not be easy for beginners, so let's break down the intermediate variables created on the fly in the Flux request. Let’s take the example of the existing data in the bucket (with only two minutes of values):
_time | _measurement | _field | _value |
2024-07-03T14:00:00Z | WindAsset1 | ElectricityPrice | 0.12 |
2024-07-03T14:00:00Z | WindAsset1 | Power | 100 |
2024-07-03T14:00:00Z | WindAsset1 | Capacity | 150 |
2024-07-03T15:00:00Z | WindAsset1 | ElectricityPrice | 0.15 |
2024-07-03T15:00:00Z | WindAsset1 | Power | 120 |
2024-07-03T15:00:00Z | WindAsset1 | Capacity | 160 |
The request articulates with the following steps:
_time | ElectricityPrice |
2024-07-03T15:00:00Z | 0,15 |
_time | Power |
2024-07-03T15:00:00Z | 120 |
_time | Capacity |
2024-07-03T15:00:00Z | 160 |
_time | ElectricityPrice | Power | Capacity |
2024-07-03T14:00:00Z | 0,15 | 120 | 160 |
_time | _measurement | _field | _value |
2024-07-03T15:00:00Z | WindAsset1 | ImmediateMonetaryGain | 18 |
Highlights - PostTextToInflux
Find in attachments the full script in "PostTextToInflux script.docx".
Pretty straightforward script, the idea is to have a generic script to post a request. The header is quite original with the vnd.flux content type
Url needs to be formatted according InfluxDB API
Right after, I asked to convert it to a Thingworx script format:
In this last picture, the script won’t work. The fluxQuery is not well formatted for TWX. Please, refer to the provided script "FormatFlux_CalculateMonetaryKPI script.docx" to see how to format the Flux query and insert variables inside.
Despite mistakes, ChatGPT still mainly provides relevant code structure for beginners in Flux and is an undeniable boost for writing code.