Using "constant value" block in Streaming Analytics for mathematical operations

Product/components used and version/fix level:

cumulocity cloud, Streaming Analytics/Analytics Builder,

  • Frontend10.18.0.5
  • Backend10.18.0.5

Detailed explanation of the problem:

Hi,
this is my first posting, so I hope it’s in the correct place - sorry if not!
I’m trying to use Streaming Analytics using the Analytics Builder. I am using several instances to read out the same value from different devices. This works well. I then want to offset that value for each device individually, so I added another template parameter that I want to use as input for the constant value block. The idea is:
constant value → extract text and convert to float → round nearest → use as input for expression block.
However, if I do that, the expression calculation does not work. This usually happens if one of the inputs is of the wrong type, but I’ve done what I know to do to convert it to a float. Of course, the input in the template parameter is a number. It also does not work if I use a fixed value for the constant value block. I’m sure I’m using that block wrong, but how would one solve this issue?
Dennis

Error messages / full error message screenshot / log file:

No error messages - where do I see the logs?

Question related to a free trial, or to a production (customer) instance?

autosen.cloud, so production instance

Hello,
Please could you provide your expression used and each of the blocks in the model.
I’m not sure about this step ‘extract text and convert to float’, is it required? The template parameter can be set to the correct type directly.
The constant value can have the type appropriately set and then used directly in the expression block.
Best regards,
Jack

Thanks for the quick reply! The “constant value” block can only have a string or JSON value, but I need float (or at least integer).
Here’s the full code:

> {"id":"88456217","builderVersion":"10.18.0.4.20230828-1241-e470f1562","name":"Operating Hours (Variables)","description":"Reading out operating hours per device","builderModel":{"nodeDataArray":[{"blockType":"c8y_Output","location":"160 -456","id":"apama.analyticskit.blocks.cumulocity.CreateOperationStaticValue","type":"C8YDevice Block","parameters":[{"id":"deviceId","value":{"id":"MP_6byvp2wrl","type":"TemplateParameterReference"},"c8y_type":"c8y_deviceIdOrCurrentDevice"},{"id":"operationName","value":"c8y_Configuration"},{"id":"paramName","value":"config"},{"id":"paramValue","value":"{\"indx_read\":{\"542\":[{\"s\":0,\"l\":2}]}}"},{"id":"description","value":"Read Operating Hours","c8y_type":"pab_multiLine"}],"categoryId":"Output","key":-2},{"location":"-80 -336","id":"apama.analyticskit.blocks.core.Rounding","type":"Analytics Block","parameters":[{"id":"mode","value":"nearest"},{"id":"numDigits","value":0}],"categoryId":"Calculations","key":-6},{"blockType":"c8y_Output","location":"736 -168","id":"apama.analyticskit.blocks.cumulocity.CreateMeasurement","type":"C8YDevice Block","parameters":[{"id":"deviceId","value":{"id":"MP_6byvp2wrl","type":"TemplateParameterReference"},"c8y_type":"c8y_deviceIdOrCurrentDevice"},{"id":"fragment","value":"Operating_Hours"},{"id":"series","value":"OperatingHours"},{"id":"unit","value":"h"}],"categoryId":"Output","key":-7},{"location":"-296 -480","id":"apama.analyticskit.blocks.core.Pulse","type":"Analytics Block","parameters":[{"id":"mode","value":"on_every"}],"categoryId":"Flow Manipulation","key":-8},{"location":"-296 -336","id":"apama.analyticskit.blocks.core.ExtractProperty","type":"Analytics Block","parameters":[{"id":"propertyName","value":"c8y_Configuration.config"},{"id":"type","value":"float"},{"id":"clearOnMissing","value":false}],"categoryId":"Utilities","key":-9},{"blockType":"c8y_Input","location":"-552 -336","id":"apama.analyticskit.blocks.cumulocity.ManagedObjectInput","type":"C8YDevice Block","parameters":[{"id":"deviceId","value":{"id":"MP_6byvp2wrl","type":"TemplateParameterReference"},"c8y_type":"c8y_deviceOrGroupId"},{"id":"captureStartValue","value":false}],"categoryId":"Input","key":-11},{"location":"-552 -480","id":"apama.analyticskit.blocks.core.CronTimer","type":"Analytics Block","parameters":[{"id":"seconds","value":"0"},{"id":"minutes","value":"*"},{"id":"hours","value":"*"},{"id":"daysOfMonth","value":"*"},{"id":"months","value":"*"},{"id":"daysOfWeek","value":"*"}],"categoryId":"Utilities","key":-12},{"location":"-552 -72","id":"apama.analyticskit.blocks.core.CronTimer","type":"Analytics Block","parameters":[{"id":"seconds","value":"0"},{"id":"minutes","value":"0"},{"id":"hours","value":"2"},{"id":"daysOfMonth","value":"*"},{"id":"months","value":"*"},{"id":"daysOfWeek","value":"*"}],"categoryId":"Utilities","key":-13},{"location":"-304 -48","id":"apama.analyticskit.blocks.core.Counter","type":"Analytics Block","parameters":[],"categoryId":"Aggregates","key":-14},{"location":"-64 -72","id":"apama.analyticskit.blocks.core.Rounding","type":"Analytics Block","parameters":[{"id":"mode","value":"up"},{"id":"numDigits","value":0}],"categoryId":"Calculations","key":-15},{"blockType":"c8y_Output","location":"160 0","id":"apama.analyticskit.blocks.cumulocity.CreateMeasurement","type":"C8YDevice Block","parameters":[{"id":"deviceId","value":{"id":"MP_6byvp2wrl","type":"TemplateParameterReference"},"c8y_type":"c8y_deviceIdOrCurrentDevice"},{"id":"fragment","value":"Device Lifetime"},{"id":"series","value":"Days since first start"},{"id":"unit","value":"d"}],"categoryId":"Output","key":-16},{"location":"-296 -288","id":"apama.analyticsbuilder.blocks.ConstantValue","type":"Analytics Block","parameters":[{"id":"value","value":{"id":"MP_0yrk2ygpj","type":"TemplateParameterReference"},"c8y_type":"pab_multiLine"},{"id":"type","value":"string"}],"categoryId":"Utilities","key":-18},{"location":"-80 -288","id":"apama.analyticskit.blocks.core.ExtractProperty","type":"Analytics Block","parameters":[{"id":"propertyName","value":"text"},{"id":"type","value":"float"},{"id":"clearOnMissing","value":false}],"categoryId":"Utilities","key":-19},{"location":"136 -288","id":"apama.analyticskit.blocks.core.Rounding","type":"Analytics Block","parameters":[{"id":"mode","value":"nearest"},{"id":"numDigits","value":0}],"categoryId":"Calculations","key":-20},{"location":"408 -240","id":"apama.analyticskit.blocks.core.Expression","type":"Analytics Block","parameters":[{"id":"expression","value":"input1-input2"}],"categoryId":"Calculations","key":-17}],"linkDataArray":[{"inputId":"pulse","outputId":"createOperation","from":-8,"to":-2,"points":[-208,-480,-193,-480,-68,-480,-68,-480,57,-480,72,-480]},{"inputId":"value","outputId":"value","from":-11,"to":-9,"points":[-464,-336,-449,-336,-424,-336,-424,-336,-399,-336,-384,-336]},{"inputId":"extractedValue","outputId":"value","from":-9,"to":-6,"points":[-208,-336,-193,-336,-188,-336,-188,-336,-183,-336,-168,-336]},{"inputId":"tick","outputId":"value","from":-12,"to":-8,"points":[-464,-480,-449,-480,-424,-480,-424,-480,-399,-480,-384,-480]},{"inputId":"tick","outputId":"value","from":-13,"to":-14,"points":[-464,-72,-449,-72,-428,-72,-428,-72,-407,-72,-392,-72]},{"inputId":"count","outputId":"value","from":-14,"to":-15,"points":[-216,-72,-201,-72,-184,-72,-184,-72,-167,-72,-152,-72]},{"inputId":"roundedValue","outputId":"value","from":-15,"to":-16,"points":[24,-72,39,-72,48,-72,48,-72,57,-72,72,-72]},{"inputId":"output","outputId":"value","from":-18,"to":-19,"points":[-208,-288,-193,-288,-188,-288,-188,-288,-183,-288,-168,-288]},{"inputId":"extractedValue","outputId":"value","from":-19,"to":-20,"points":[8,-288,23,-288,28,-288,28,-288,33,-288,48,-288]},{"inputId":"roundedValue","outputId":"input1","from":-6,"to":-17,"points":[8,-336,23,-336,164,-336,164,-336,305,-336,320,-336]},{"inputId":"roundedValue","outputId":"input2","from":-20,"to":-17,"points":[224,-288,239,-288,272,-288,272,-288,305,-288,320,-288]},{"inputId":"value","outputId":"value","from":-17,"to":-7,"points":[496,-240,511,-240,572,-240,572,-240,633,-240,648,-240]}]},"c8y_analyticsModel":{},"type":"c8y_PublicAnalyticsBuilderModel","templateParameters":[{"id":"MP_6byvp2wrl","name":"In- and Output AI401","typeDisplayName":{"typeName":"c8y_deviceIdOrCurrentDeviceOrGroup","semanticType":"c8y_deviceIdOrCurrentDeviceOrGroup"},"description":"","type":"string","optional":false,"semanticType":"c8y_deviceIdOrCurrentDeviceOrGroup","defaultValue":"16437235"},{"id":"MP_0yrk2ygpj","name":"OperatingHoursZero","typeDisplayName":{"typeName":"string"},"description":"","type":"string","optional":false,"defaultValue":"0"}],"userInterfaceProperties":{"displayGrid":false},"tags":[]}

Apologies for the confusion caused. Watch this space for an easier solution in the future. (If possible upgrading to 2024 release would bring this enhancement)

For a quick solution.
Please unselect the type in the constant value block, it is an optional field, by default it will infer the type correctly as the intended float.

Reminder that expression block uses float as default for numeric calculations, use floor(input1) or ceil(input2) for rounding and doing integer calculations.

2024 documentation of the block explains the above and also the enhancement to set the type to float etc.
Analytics Builder block reference - Cumulocity IoT documentation

Hi Jack,

the type is mandatory, see attached screenshot. I cannot unselect, only switch between String and JSON.

Screenshot 2024-02-13 125545

Changing the Streaming Analytics version is currently not an option since it would affect far too many working instances.

I wonder; I cannot be the only one wanting to use a variable in calculations?

Thanks for the tips on the expression-block!

Oops, that was also a recent change I missed, my apologies again.

Please select JSON.

Still no need for extract property. Extract property is for retrieving from a dictionary, and the constant value will not set any properties to be extracted as it will just be simple JSON.

1 Like

That was a real simple fix. Thank you so much!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.