Skip to main content

I am trying to create a synthetic time series query that looks are two other time series and if those values meet a certain condition, is outputs a 1 or 0 as the synthetic time series but I keep getting a syntax error that I can’t solve. Below is a code sample.

 

temp_id_list = t3308928194658464]
flow_id_list = t1256034258207391, 3564715377369546]

for temperature in temp_id_list:
    temp_expression = f"TS{{id:{temperature}}} < 200"
    flow_expression = " + ".join( f"(TS{{id:{flowrate}}})" for flowrate in flow_id_list])

    final_expression = f"if({temp_expression} and {flow_expression} > 0, 1, 0)"

    cv_query = client.time_series.data.synthetic.query(expressions=final_expression, start="1d-ago", end="now")

Hi Talia!

Our current capabilities for synthetic time series do not support the use of if expressions. The supported functions include various mathematical operators, trigonometric functions, and specific functions like ln, pow, sqrt, exp, abs, and map. However, conditional statements like if are not listed among these supported functions. For more detailed information and to explore potential workarounds, you can refer to the Cognite synthetic time series documentation: Synthetic time series documentation.

In some cases the usage of functions like max and min can help you achieve the similar outcome as with an if expression, but we do understand that it’s not as flexible. If this is an important feature for you, I recommend making a feature request as a Product Idea here on the hub.


I’ve used some workarounds before, using max, min, round, but also hack with the on_error method.
For instance
on_error(sqrt(200-TS{id:...}) this will throw an error if TS>200, due to sqrt of negative number

* 0 + (value if expression is true), value if no error

(value if expression is false)) value if error

You can also use ln, which fails on <= 0, not just <0

The syntax is not very nice, but it should get the work done

If the input is string time series, it is actually easier, using the map() function


Reply