How to check if json file really loaded from server

Hi there,

I just started with HA and i’m stuck with this.

I am loading a json file for my inverters from the server with:

  - platform: rest
    name: solarpanels
    resource: 'http://10.10.1.4/api/v1/production/inverters'
    username: 'installer'
    password: 'xxxxxxxxx'
    authentication: 'digest'

When i go to the website i get my json list like:

[
  {
    "serialNumber": "122023018045",
    "lastReportDate": 1606231538,
    "devType": 1,
    "lastReportWatts": 3,
    "maxReportWatts": 220
  },
  {
    "serialNumber": "122023018083",
    "lastReportDate": 1606232449,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 222
  }
]

I’m trying to make a sensor template as followed.

  - platform: template
    sensors:
      serialnumber:
        value_template: "{{ state_attr(value_json['0'].serialNumber, [serialnumber]) }}"

Within the template editor this gives me the serialnumber of the first inverter, when i put this in the configuration file it gives me nothing…

After trying for day’s with this template i start to wonder if i get a json from the webserver through HA, is there a way to check if it’s really working?
Secondly how could i loop through all those inverters???
\

Regards Fred

This line doesn’t look quite right.

state_attr requires an entity_id string and an attribute string as arguments.

The first argument looks right, but the 2nd one doesn’t.

It’s either you want the attribute ‘serialnumber’, or ‘[serialnumber]’. Either way, you’ll need quotes around the 2nd parameter. Also, not sure what’s up with the quotes around the [‘0’]. Maybe it’s ok, but unless the entity is named “0”, just use a number for the index.

value_template: "{{ state_attr(value_json[0].serialNumber, 'serialnumber') }}"

Hi Jim,
Thx for the fast reply.
I checked your , but it still gives me the same unavailable error so i really think i did something wrong with getting the file from the server. But have looked around and as far as i can find this should be the way to call the website for the json file.

To be sure i checked both your and mine value template line in the editor and both gave me the serialnumber i want but nothing in the sensor:-(

I f i only knew how to be sure it pulls down the json file i could continue with templating…

TIA Fred

Is this your whole config for the template sensor?

There is nothing telling it to get json from anywhere. Perhaps you meant to integrate your rest sensor ‘solarpanels’ into this template sensor?

 - platform: template
    sensors:
      serialnumber:
        value_template: "{{ state_attr('sensor.solarpanels', 'serialnumber') }}"

The value_json is only a property of the original sensor that grabbed it. Outside of that sensor, you can’t access the value_json.

Further, you’ll have to update your rest sensor to tell it how to parse that json.

You have 2 options here. 1) have the restful sensor just a dumb sensor that grabs ALL of the json and stores it in attributes for other template sensors to parse.

  1. Create a rest sensor for each entry in the json.

I think this will work with #1. I’m not 100% sure how to do number 2.


If you want multiple rest sensors, it would look like this:

  - platform: rest
    name: solarpanel_1
    resource: 'http://10.10.1.4/api/v1/production/inverters'
    username: 'installer'
    password: 'xxxxxxxxx'
    authentication: 'digest'
    value_template: "{{ value_json[0].lastReportWatts }}"
    # Just store all values as an attribute. Sure, lastReportWatts will be duplicated, but oh well.
    json_attributes_path: "$.[0]"
    json_attributes:
      - serialNumber
      - lastReportDate
      - devType
      - maxReportWatts

  # This one will get the 2nd in the json
  - platform: rest
    name: solarpanel_2
    resource: 'http://10.10.1.4/api/v1/production/inverters'
    username: 'installer'
    password: 'xxxxxxxxx'
    authentication: 'digest'
    value_template: "{{ value_json[1].lastReportWatts }}"
    # Just store all values as an attribute. Sure, lastReportWatts will be duplicated, but oh well.
    json_attributes_path: "$.[1]"
    json_attributes:
      - serialNumber
      - lastReportDate
      - devType
      - maxReportWatts
1 Like

Thx, i got a bit occupied with the wife tonight, but sure would look into it tomorrow, you helped me a lot so far thx.

Will let you know in a day if i got it to work…

regards Fred