JSON with null value

i have setup a rest sensor for my Solar panels , refer below … when the panels are producing no power ( ie at night) , the rest call produces a null but i can not get the template to determine if the value is null , it always tries to evaluate the value and returns a 2 !!!

i’m after the P_PV value, works when daylight !!

JSON
{
“Body” : {
“Data” : {
“Inverters” : {
“1” : {
“DT” : 76,
“E_Day” : 31861,
“E_Total” : 8564700,
“E_Year” : 7369462.5,
“P” : 0
}
},
“Site” : {
“E_Day” : 31861,
“E_Total” : 8564700,
“E_Year” : 7369462.5,
“Meter_Location” : “grid”,
“Mode” : “meter”,
“P_Akku” : null,
“P_Grid” : 489.16998906619847,
“P_Load” : -489.16998906619847,
“P_PV” : null,
“rel_Autonomy” : 0,
“rel_SelfConsumption” : null
},
“Version” : “10”
}
},
“Head” : {
“RequestArguments” : {},
“Status” : {
“Code” : 0,
“Reason” : “”,
“UserMessage” : “”
},
“Timestamp” : “2018-10-07T23:19:09+08:00”

Sensor definition
platform: rest
resource: http://10.0.0.200/solar_api/v1/GetPowerFlowRealtimeData.fcgi
name: “PV”
value_template: >-
{% if value_json.Body.Data.Site.P_PV != null %} {{value_json.Body.Data.Site.P_PV | multiply(0.001) | float(2) | round (2) }}
{% else %} 0
{% endif %}
unit_of_measurement: kWh

any ideas ?

thanks LEEB

You’ve specified a default value of 2 in the float filter, which means if it’s input value is not a valid number it should output the value 2. I think what you want instead is:

value_template: >
  {{ value_json.Body.Data.Site.P_PV|float(0)|multiply(0.001)|round(2) }}

If value_json.Body.Data.Site.P_PV is not a valid number (e.g., if it’s null), then float(0) will output zero.

thanks that work :slight_smile:

the terminology (float) is a bit misleading !

Glad it’s working, but I guess I’m not sure what is misleading about it. “float” is a type of number, and the “float()” filter converts its input (e.g., an “int” or a “string”) to type “float”. See here and here in the Jinja documentation.