JSONPATH path not found issue

Hello, I’ve something kinda weird with jsonpath transform in a rule

i’ve a sonoff mqtt item SONOFF_LAMPE_RGB_1_State

Depending on what was its last action, it could be

Case 1

SONOFF_LAMPE_RGB_1_State received update State= {“Time”:“2018-08-23T07:14:10”,“Uptime”:“0T11:24:29”,“Vcc”:3.385,“POWER”:“OFF”,“Dimmer”:100,“Color”:“FF00000000”,“HSBColor”:“0,100,100”,“Channel”:[100,0,0,0,0],“CT”:499,“Scheme”:0,“Fade”:“ON”,“Speed”:1,“LedTable”:“ON”,“Wifi”:
{“AP”:1,“SSId”:“NotAGoodWifi”,“RSSI”:100,“APMac”:“D8:32:14:D8:A5:11”}}

Or
Case 2

SONOFF_LAMPE_RGB_1_State received update State= {“POWER”:“OFF”}

I’ve got error when doing transform(“JSONPATH”, “$.CT”, json) on case 2
that’s ok, could be better if avoidable
But mainly I was expecting this to receive NULL or an empty string, but that’s not the case.

How to detect that the path “CT” is not found by the “JSONPATH” transform ?

Thank you
Aymeric

You may need to get down the Javascript transform route

to be more precise

if json equal {“POWER”:“OFF”} then transform(“JSONPATH”, “$.CT”, json) returns -> {“POWER”:“OFF”}
Isn’t it odd ?

Perhaps I can do something with regular expression like



json=item.state.tostring
if (regex 'CT' found in json) then 
     newCT= transform(“JSONPATH”, “$.CT”, json)
   ...
else 
   do_nothing_to_CT

If json.contains("CT") 

arfff “knowledge is power” …:hugs:

You could easily switch to a Javascript (JS) transformation. I eliminated the JSONPATH transformation everywhere exactly because of this.

(function(x){
 
    var result = "none";
 
    var json = JSON.parse(x);  
    try
    {
        result = json.CT;
    }
    catch(e)
    {
        return null;
    }

    return result;
    
})(input)