BQLQuery - Niagara N4

How can I connect the result of a BQL query to a numeric point? Any help?

So you will need to put this into a program object and when you do a count, you have to use |cell:0,0 at the end of your query.

To elaborate, you need to do the following in a program object:

Use the BQL query with the cell extension to directly fetch the count, then fetch the result.

However, the result is going to be in a BFormat.

Then you have to parse that result into double and you should create a double method to parse into a string and create a numberFormatException.

This would be an example of taking the number of devices in fault

public void onExecute() throws Exception
{
    // Use the BQL query with the cell extension to directly fetch the count
    BOrd ord = BOrd.make("station:|slot:/|bql:select count(status) from bacnet:BacnetDevice where status.fault = 'true'|cell:0,0");
    
    // Fetch the result
    Object result = ord.get(Sys.getStation());
    
    // Use BFormat to format the result
    String formattedResult = BFormat.format("%.%", result);
    
    // Parse the formatted result
    double faultCount = parseToDouble(formattedResult, "Fault count");
    
    // Use the parsed result
    setDevicesFault(faultCount);
}

/**
 * Helper method to parse a string to a double with error handling.
 * @param value The string to parse.
 * @param label The context of the value for error messages.
 * @return The parsed double value.
 * @throws Exception If the string cannot be parsed.
 */
private double parseToDouble(String value, String label) throws Exception {
    try {
        return Double.parseDouble(value);
    } catch (NumberFormatException e) {
        throw new Exception("Failed to parse " + label + " as a number: " + value, e);
    }
}
2 Likes

Charles How would you do this if it was for a Niagara Network?

Replace bacnet:BacnetDevice with niagaraDriver:NiagaraStation. If you’re looking for edges are would be niagaraDriver:NiagaraEdgeLiteStation

3 Likes