Skip to main content
Answer

Hosted Extractors - maximum transformation code length (2000)

  • November 27, 2023
  • 3 replies
  • 72 views

Forum|alt.badge.img

Hi,

From my MQTT topic I receive sensor data as a timestamp and 25 values, where each value should gets transformed as a datapoint for its own timeseries. However, there is a limit on 2000 char for the transformation code AND a constraint that does not allow you to set up duplicate streams on the same topic: meaning you can only do one transformation per topic, but this function is limited to 2000 chars. 

I run into a problem now where, for this sample data:

“{"timestamp":1701093489556,"values":[-0.035999998450279236,-0.10400000214576721,-0.09099999815225601,-0.08299999684095383,-0.09399999678134918,-0.10999999940395355,-0.10700000077486038,-0.10499999672174454,-0.11299999803304672,-0.12099999934434891,-0.13899999856948853,-0.1340000033378601,-0.14399999380111694,-0.1340000033378601,-0.15000000596046448,-0.15000000596046448,-0.17000000178813934,-0.16699999570846558,-0.15399999916553497,-0.16500000655651093,-0.15000000596046448,-0.10199999809265137,-0.11100000143051147,-0.1469999998807907,-0.12600000202655792]}”

I use this template, but cannot include the all 25 values as this is just over 4000 char. Is there a way to get around this? e.g using a loop function to generate the template? 

“[From 0…
{
  "externalId": concat("mysystem.ExtEnvironment.mysensor.input9.", context.topic),
  "timestamp": input.timestamp,
  "value": input.values[8],
  "type": "datapoint"
},
{
  "externalId": concat("mysystem.ExtEnvironment.mysensor.input10.", context.topic),
  "timestamp": input.timestamp,
  "value": input.values[9],
  "type": "datapoint"
},
{
  "externalId": concat("mysystem.ExtEnvironment.mysensor,input11.", context.topic),
  "timestamp": input.timestamp,
  "value": input.values[10],
  "type": "datapoint"
},
{
  "externalId": concat("mysystem.ExtEnvironment.mysensor.input12.", context.topic),
  "timestamp": input.timestamp,
  "value": input.values[11],
  "type": "datapoint"
},
{
  "externalId": concat("mysystem.ExtEnvironment.mysensor..input13.", context.topic),
  "timestamp": input.timestamp,
  "value": input.values[12],
  "type": "datapoint"
}
...up to 25
]”

Best answer by Nicolai

Hey, I couldn’t find a way to generate the index-list (perhaps someone else here knows how to do so), but if the value-list is always static then perhaps this will work?

 

zip(
input.values,
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
(value, index) => {
"externalId": concat("mysystem.ExtEnvironment.mysensor.input",index,".",context.topic),
"value":value,
"timestamp": input.timestamp,
"type": "datapoint"
}
)

 

3 replies

  • Seasoned
  • Answer
  • November 27, 2023

Hey, I couldn’t find a way to generate the index-list (perhaps someone else here knows how to do so), but if the value-list is always static then perhaps this will work?

 

zip(
input.values,
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24],
(value, index) => {
"externalId": concat("mysystem.ExtEnvironment.mysensor.input",index,".",context.topic),
"value":value,
"timestamp": input.timestamp,
"type": "datapoint"
}
)

 


  • Practitioner
  • November 27, 2023

The zip approach will work fine, but we can add a function to get the indices in an array, as you here have a clear use case of such an approach, perhaps `input.enumerate((value, index) => ...)`?


  • Practitioner
  • December 5, 2023

Hi, it should now be possible to let the lambda given to “map” take a second argument, which will be the index, so you should be able to do

["a", "b", "c"].map((it, idx) => concat(it, idx))

produces

["a0", "b1", "c2"]

Hopefully that solves the problem in a more elegant way!