Expression vs. difference block


we would like to simply substract input1 - input2 but the challenge faced with having an ‘expression block’ of (input1 - input2) or even with ‘difference block’ is that the calculation happens when new measurements of all inputs is received. While we need to calculate the difference between the latest measurements of both. For example: input1 has changed 10 times during the day while input2 has changed 1 time only during the day-> we need to calculate (measurement#10 of input1 (latest one)) - ( (measurement#1 of input2 (latest one)).

is there a way to do it using analytics builder? we tried latsh before the expression block so that we provide latshed value only during a specific time which is when we will trigger this model based on a chron timer at the end of day

update: testing now the following idea: doing the calculation during a specific time (using gate enabled by a crontimer) + using managed objects for their asynchronous nature in order to store synchronous measurements and to be able to insert them in the main model later on when this model’s main gate is opened by the chrontimer

Note, I haven’t tried this, but I think it will work, and may need to be applied to both inputs.

The Combiner block gives a new output if only one value changes. So we can connect an input there.

Connect the other input to a Constant block trigger via a Pulse block, and have the Constant block output a value you’d never get (may negative), and connect this to another input on the Combiner block.

Whilst the value remains unchanged, it will count as a new input to the Combiner (this is explicit with the Latest Input mode). Thus using the Maximum or Minimum you should then get a new output of the same value as the original unchanged input. The difference block should treat this as a new input, even though the value is unchanged, and give a new output.

I;ll raise an enhancement ticket about this use case, as it seems reasonable to want to calculate the difference when only one input has changed.

update: re-testing with managed object as input to expression (by moving the measurement to a managed object in another analytics model) this time without a gate worked!!
so in summary: if we have an expression of input1 - input2:

  1. with measurement inputs (synchronous) we need new inputs for both input1 and input2 each time
  2. after transfering the measurement inputs to manage objects in model#1 and having in model#2 the managed object as input, we tested:
    test1: input1 and input2 changing (ex: input 1 = 5 and input 2 = 3) - expression result: 2
    test2: input1 changed only from 5 to 4 while input2 MO didnt have any change - Expression result = 1
    test3: input2 changed only from 3 to 2 while input1 MO didnt have any change - Expression result = 2

conclusion: if expression is input1 - input2 and we change the Managed object value of only one inputs, the new input1 will be used with the old input2 and that is what we need

I dont know if any of the tests was missleading but is it the right conclusion?

we should face a problem in expression if its inputs are 2 measurements and 1 input changed 3 times while the 2nd input changed only 1 time? we will not be able to measure the difference of the 3rd measurement value of input1 vs. the 1st measurement value of input2?? new inputs are always required for both since measurements are synchronous? and it is solved with managed objects since managed objects are asynchronous?

or even with measurements the expected value for expression is to compare the latest version of the inputs? and for some reason our test missleaded us?

newest update: I tested again the same scenario using measurements as inputs to expression (input1 - input2) and expression worked correctly:

Test1: pushing input1 and input2 → expected result soon
Test2: changing the value of input1-> new input1 value was executed in expression vs. old/existing input2 value and new output seen correctly
Test2: changing the value of input2-> new input2 value was executed in expression vs. old/existing input1 value and new output seen correctly

so expression provides a new output if only one input changes and it doesn’t need to receive a new value for all inputs! Apprarently the 1st test was missleading and it was an expression problem, probably it was a display problem where new output measurements were not seen on info gauge

I’m pleased to the Expression block works as required.

As a general note, the Measurement → ManagedObject solution you tried first is adding a lot of traffic across the Apama Cumulocity Transport API. Whilst it might work correctly, you would really need to be very aware and careful of your Measuremeent event rate before using this in production, to make sure you didn’t create a performance issue.

I’ve had a chance to look at the Difference block in more detail, and it does calculate a new output if only one input changes. However, it does require a value on both inputs to calculate a difference. From the original post’s description, I’m wondering whether th ‘static’ input value is being held on the input throughout the period in question, or whether it is only on the input when it is received, and the dropped. If, as I suspect, this is what is happening, then the Difference block no longer has the value available later to vcalculate its output. The way to solve this is to put a Latch block on the input so that desired value is held on the Difference block’s input for the duration.