Value "0" if sensor unknown/unavailable

Hello!

I have problem (simple I think) which can’t handle.

Have PV installation connected to HA with REST

  - platform: rest
    resource: http://xxxxx
    name: PV
    json_attributes:
      - flg
[...]
      - vac
    value_template: "{{ value_json.flg}}"

and template:

  - platform: template
    sensors:
      PV_vac1:
        friendly_name: "PV ACF1-V"
        value_template: "{{ (state_attr('sensor.PV', 'vac')[1] | float(0) / 10)}}"
        unit_of_measurement: "V"

Works fine during day (returns proper value), but after sunset return unknown or unavailable.

My question is: how to change my config which returns me value when value exist or “0” if sensor is unknown/unavailable? Found few ideas, but didn’t fit to my config…

Thanx!

Need some more info. What does the attribute vac look like after sunset?

now (no sun):
sensor. pv_vac1: unavailable

My rest sensor is web page with data. When sunset, webpage is unavaiable and sensor data cant be read.

no, The rest sensor w/ the vac attribute, sensor.pv

ah ok,

{{ ((state_attr('sensor.PV', 'vac') or [0, 0])[1] / 10)}}

Hello! Thanks for answer!

Seems to be 50% working :slight_smile: Entity doesn’t return now “unknown” or “unavailable” status, which is fine! But didn’t return proper value - seems to be “0,0V” always…

make sure the entity_id is lowercase. It shouldn’t ever be 0 unless vac is emtpy. Can you verify that the attribute has values? I’m making guesses to how it should work based on your code. If your original code was wrong, then so will my code.

Well, my full config:

  - platform: rest
    resource: http://xxxxx
    scan_interval: "00:00:10"
    name: PV Moc
    device_class: energy
    state_class: measurement
    value_template: "{{ value_json.pac | float / 1000 | round(2) }}"
    unit_of_measurement: "kW"
  - platform: rest
    resource: resource: http://xxxxx
    scan_interval: "00:01:00"
    name: PV Suma
    device_class: energy
    state_class: total_increasing
    value_template: "{{ value_json.eto | float / 10}}"
    unit_of_measurement: "kWh"
  - platform: rest
    resource: resource: http://xxxxx
    scan_interval: "00:01:00"
    name: PV Dzis
    device_class: energy
    state_class: total_increasing
    value_template: "{{ value_json.etd | float / 10}}"
    unit_of_measurement: "kWh"
  - platform: rest
    resource: resource: http://xxxxx
    scan_interval: "00:01:00"
    name: PV Temp
    value_template: "{{ value_json.tmp | float / 10}}"
    unit_of_measurement: "C"
  - platform: rest
    resource: resource: http://xxxxx
    scan_interval: "00:00:10"
    name: PV
    json_attributes:
      - flg
      - tim
      - pac
      - etd
      - hto
      - eto
      - vac
      - tmp
      - iac
      - vpv
      - ipv
      - fac
      - err
    value_template: "{{ value_json.flg}}"

  - platform: template
    sensors:
      falownik_vac3:
        friendly_name: "PV ACF3-V"
        value_template: "{{ (state_attr('sensor.pv', 'vac')[0] | float(0) / 10)}}"
        unit_of_measurement: "V"
      falownik_vac1:
        friendly_name: "PV ACF1-V"
        value_template: "{{ ((state_attr('sensor.pv', 'vac') or [0, 0])[1] / 10)}}"
        unit_of_measurement: "V"
      falownik_vac2:
        friendly_name: "PV ACF2-V"
        value_template: "{{ ((state_attr('sensor.pv', 'vac') or [0, 0])[2] / 10)}}"
        unit_of_measurement: "V"
      falownik_iac3:
        friendly_name: "PV ACF3-A"
        value_template: "{{ (state_attr('sensor.pv', 'iac')[0] | float(0) / 10)}}"
        unit_of_measurement: "A"
      falownik_iac1:
        friendly_name: "PV ACF1-A"
        value_template: "{{ (state_attr('sensor.pv', 'iac')[1] | float(0) / 10)}}"
        unit_of_measurement: "A"
      falownik_iac2:
        friendly_name: "PV ACF2-A"
        value_template: "{{ (state_attr('sensor.pv', 'iac')[2] | float(0) / 10)}}"
        unit_of_measurement: "A"
      falownik_vpv1:
        friendly_name: "PV PV1-V"
        value_template: "{{ (state_attr('sensor.pv', 'vpv')[0] | float(0) / 10)}}"
        unit_of_measurement: "V"
      falownik_vpv2:
        friendly_name: "PV PV2-V"
        value_template: "{{ (state_attr('sensor.pv', 'vpv')[1] | float(0) / 10)}}"
        unit_of_measurement: "V"
      falownik_ipv1:
        friendly_name: "PV PV1-A"
        value_template: "{{ (state_attr('sensor.pv', 'ipv')[0] | float(0) / 100)}}"
        unit_of_measurement: "A"
      falownik_ipv2:
        friendly_name: "PV PV2-A"
        value_template: "{{ (state_attr('sensor.pv', 'ipv')[1] | float(0) / 100)}}"
        unit_of_measurement: "A"
      falownik_hto:
        friendly_name: "PV Czas Pracy"
        value_template: "{{ (state_attr('sensor.pv', 'hto') | float(0) / 10)}}"
        unit_of_measurement: "h"
      falownik_err:
        friendly_name: "PV Awaria"
        value_template: "{{ state_attr('sensor.pv', 'err') }}"
        unit_of_measurement: ""

I’ve been trying to edit “falownik_vac1” and “falownik_vac2” myself.

well vac2 will never work with the default the way it is. 1 should work fine

Still, that doesn’t help when I can’t see what data is in the attribute itself

Sth like that?

No, the source sensor that has the vac attribute.

OK, got it. Sorry for delay, I’ve been waiting for sun :slight_smile: Raw data from PV:

{"flg":1,"tim":"20230119112439","tmp":154,"fac":5000,"pac":114,"sac":114,"qac":0,"eto":39850,"etd":3,"hto":3350,"pf":32,"wan":0,"err":0,"vac":[2434,2439,2429],"iac":[4,4,4],"vpv":[4114,0],"ipv":[47,0],"str":[]}

and what does the vac attribute sesnor.pv show when the information is not populated? It seems as if the or in the template I provided is not working, which doesn’t make sense.

Resource of sensor is http page (http://IP/xxxxx), PV system returns this "
{“flg”:1,“tim”:“20230119112439”,“tmp”:154 […]" data. When PV is offline, its returns nothing (http timeout)

Downs sensor.pv go unavailable during that time?

yep…
Got error log:

Logger: homeassistant.components.rest.data
Source: components/rest/data.py:83
Integration: RESTful (documentation, issues)
First occurred: 11:58:53 (3812 occurrences)
Last logged: 20:45:45

    Error fetching data: http://xxxxx failed with Server disconnected without sending a response.
    Error fetching data: http://xxxxx failed with All connection attempts failed

Another question: why vac1 works fine and vac2 doesn’t? Syntax is the same…