Search code examples
grafanawonderware

How to implement this logic in grafana dashboard?


I pulled 3 time series signals A, B, and C from a SQL server data source. I want to implement the following logic:

if A > 0: 
    if B > C:
        Status = 1
    else:
        Status = 0
else Status = 0

What is the best way to implement this logic in Grafana? I tried Transform in Grafana and it is possible there to subtract B and C there but I need to implement the comparison. Thank you for your help.

Edit: The query is a bit complex because A, B, and C are queried from the Wonderware historian table. That's why I want to query each tag separately in Grafana and realize the logic there.

SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(mi,-5,GetDate())
SET @EndDate = GetDate()
SET NOCOUNT OFF

SELECT  TagName, Value
FROM (
    SELECT History.TagName, DateTime, Value, vValue, StartDateTime
    FROM History
    WHERE History.TagName IN ('A')
    AND wwRetrievalMode = 'Cyclic'
    AND wwCycleCount = 100
    AND wwVersion = 'Latest'
    AND DateTime >= @StartDate
    AND DateTime <= @EndDate) temp 
WHERE temp.StartDateTime >= @StartDate

Solution

  • So I know nothing of grafana but I took your description and query to come up with this which runs on my historian. Mod to fit your needs I guess?

    SET NOCOUNT ON
    
    DECLARE @StartDate DateTime
    DECLARE @EndDate DateTime
    SET @StartDate = DateAdd(mi,-5,GetDate())
    SET @EndDate = GetDate()
    
    SET NOCOUNT OFF
    
    SELECT  QueryDate,
            'AValue' = [A],
            'BValue' = [B],
            'CValue' = [C],
            'Status' = IIF( [A] > 0 AND [B] > [C], 1, 0)
    FROM (
        SELECT 'QueryDate' = [DateTime],
                Tagname,
                Value
        FROM (
            SELECT [DateTime],Tagname,Value
            FROM History
            WHERE History.TagName IN ('A','B','C')
            AND wwRetrievalMode = 'Cyclic'
            AND wwCycleCount = 100
            AND wwVersion = 'Latest'
            AND DateTime >= @StartDate
            AND DateTime <= @EndDate
        ) HistoricalData
    ) As DataValues
    PIVOT (
        MAX(Value) FOR Tagname IN ([A],[B],[C])
    ) As PivotData