Json sensor data unknown, replace with value

Hi,

I have a RESTful sensor configured, reading data from a Fronius solar inverter. The sensor is configured thus:

  - platform: rest
    resource: http://192.168.0.115/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CommonInverterData
    name: "Current Power"
    value_template: '{{ value_json.Body.Data.PAC.Value }}'
    unit_of_measurement: 'W'
    force_update: true

This works fine when the panels are generating power, but the system stops presenting the data once the sun goes away.

Is there a way, using templating, that I can use the value of the field when it is present, and return zero for the sensor value when the object returns unknown?

Any assistance will be much appreciated.

Thanks

Without knowing exactly what the json looks like normally it can be hard to say for sure but try something like this:

{% if value_json.Body.Data.PAC.Value is defined %}
  {{ value_json.Body.Data.PAC.Value }}
{% else %}
  0
{% endif %}

Ah - I wasn’t aware that you could use templated values as results of an “if” clause - none of the examples show that, only literals…

Thanks - will try it.

Edit: Actually there are heaps of examples if you look in the right place - thanks again.

So - checking “Value” returns an error, you need to check the level up in the tree exists - this would probably have been obvious if I supplied examples of the Json, when the panels are off the object PAC is not supplied, not just the value. The final template looks like:

   - platform: rest
     resource: http://192.168.0.115/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=CommonInverterData
     name: "Current Power"
     value_template: >
         {% if value_json.Body.Data.PAC is defined %}
             {{ value_json.Body.Data.PAC.Value }}
         {% else %}
             0
         {% endif %}
     unit_of_measurement: 'W'
     force_update: true
2 Likes