[SOLVED] Parsing a json value from an existing entity in a template sensor

Any chance this could make it to become an officially supported component? This is an outstanding job!

This is the output I get:

 siteCurrentPowerFlow: {
  "updateRefreshRate": 3,
  "unit": "kW",
  "connections": [
    {
      "from": "GRID",
      "to": "Load"
    }
  ],
  "GRID": {
    "status": "Active",
    "currentPower": 3.68
  },
  "LOAD": {
    "status": "Active",
    "currentPower": 3.68
  },
  "PV": {
    "status": "Idle",
    "currentPower": 0
  }
}
friendly_name: SolarEdge jsonrest

any hints how to “extract” che currentPower values (GRID, LOAD and PV) from it?

This contribution is great, but I believe this should be transformed into a general solution, similar to the “template sensor”

The behaviour should be the same for REST, MQTT or Serial (just naming few I have a current use case)

1 Like

try:

sensor:
 - platform: jsonrest
   resource: http://your.url/json
   name: solaredge_jsonrest
  - platform: template
    sensors:
      grid_status:
        value_template: '{{ states.sensor.solaredge_jsonrest.siteCurrentPowerFlow["GRID"].status }}'
1 Like

Guys, I’m working on merging the jsonrest with the rest sensor and push it upstream. But first I want to do some tests and make sure I’m not breaking anything. I’ll let you know when I make the Push Request. Hopefully it will be done in a few days/weeks :slight_smile:

2 Likes

Thanks @sti0, but I get a

ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template grid_status: UndefinedError: 'homeassistant.core.State object' has no attribute 'siteCurrentPowerFlow'

EDIT: here’s working, really thanks for the hint!! (you just missed attributes)

- platform: jsonrest
  resource: [redacted]
  method: GET
  name: SolarEdge jsonrest
  scan_interval: 400

- platform: template
  sensors:
    grid_consumo:
      value_template: '{{ states.sensor.solaredge_jsonrest.attributes.siteCurrentPowerFlow["LOAD"].currentPower}}'
    grid_produzione:
      value_template: '{{ states.sensor.solaredge_jsonrest.attributes.siteCurrentPowerFlow["PV"].currentPower}}'

For anybody interested in SolarEdge, I’ll write a little tutorial and will try to keep it up to date with the mainstream edition of this great piece of code. You can find relevant post here

Thanks so much guys, you rock!

1 Like

Ok, I’ve made the time to send a PR for this: https://github.com/home-assistant/home-assistant/pull/10483
Follow progress there and let’s hope it gets merged in the next release.

3 Likes

Ok. Did it out of my head. Missed the attributes part. Sorry for that, but finding your own solution is even better :smiley:

@sti0 I am using this component (jsonrest.py), but unfortunately it doesn’t seem to parse the values that are in an array?
I would like to read the vallues of a device (Youless LS120) that spits out JSON with values that it reads from my smartmeter:

[{"tm":1513196051,"net":-332.767,"pwr": 407,"ts0":1512921868,"cs0": 0.000,"ps0": 0,"p1": 616.196,"p2": 454.733,"n1": 430.932,"n2": 972.764,"gas": 350.759,"gts":1712132100}]

Or in a prettified-view:

[  
   {  
      "tm":1513196051,
      "net":-332.767,
      "pwr":407,
      "ts0":1512921868,
      "cs0":0.000,
      "ps0":0,
      "p1":616.196,
      "p2":454.733,
      "n1":430.932,
      "n2":972.764,
      "gas":350.759,
      "gts":1712132100
   }
]

As you can see the values are in a array.

I added the sensor like this:

- platform: jsonrest
  resource: http://192.168.6.28/e
  name: 'ls120_json'
  scan_interval: 10

Unfortunately, the sensor isn’t added. When i use a different URL (which spits out JSON without array-brackets) it works like i would expect. However, that alternative page doesn’t contain all the values i need :frowning:

Normally i would use something like value_template: '{{ value_json.[0].gas }}' but i have no idea how to do it with this custom component.

Can you please enlighten me how to get these JSON values into HASS?

You should create a template sensor to read out the values of the jsonrest sensor.

Something like this:

sensor:
    - platform: template
        sensors:
          mygas:
            value_template: '{{ states.sensor.ls120_json.attributes.gas }}'

Please check also if the attributes of your sensor displayed correctly at the dev-state view.

I know! But the sensor isn’t even created in Home Assistant after a restart, when using that URL.
When using a different URL (/a) it is, but that one doesn’t contain all the values i need.

The output of that page looks like this:

{"cnt":"-332,318","pwr":287,"lvl":0,"dev":"","det":"","con":"","sts":"","ps0":0,"raw":0}

Or in a prettified-view:

{  
   "cnt":"-332,318",
   "pwr":287,
   "lvl":0,
   "dev":"",
   "det":"",
   "con":"",
   "sts":"",
   "ps0":0,
   "raw":0
}

As you can see, the only differences between those outputs (except from the fields) is that the one i want to use, has square brackets around the JSON data. What do i need to alter in the jsonrest.py so that it can handle the brackets?

Is the output of your first post everything you got?

Yes, that is the output (the version with the square bracket) is the one i would like to use inside Home Assistant.
I need the fields p1, p2, n1 etc.

Ok. As you said the parsing with square brackets maybe not working correctly. I’m in a hurry and can’t help you at this time. Maybe you can ask the author @mad_ady of this component.

I’ve looked into it and reproduced the problem on my end. The cause is the fact that the JSON reply gets parsed as a list instead of a dictionary. I’ve made some changes to the custom component, and now it seems to work fine. Get the latest copy from here: https://github.com/mad-ady/home-assistant-customizations/blob/master/custom_components/sensor/jsonrest.py

Here is a sample config:

  - platform: jsonrest
    name: 'array'
    scan_interval: '00:01'
    resource: http://127.0.0.1/cgi-bin/array.sh

Here’s how you can parse the result (the array gets associated to the name ‘list’ and ‘list[0]’ is the first dictionary in the array):

{{ states.sensor.array.attributes.list[0].pwr }}
4 Likes

Totally awesome! Works exactly like you mentioned!
Thanks for the very fast fix!

The state attributes for the sensor are shown like this:

{
  "list": [
    {
      "pwr": 372,
      "p1": 617.783,
      "n1": 430.932,
      "n2": 977.498,
      "gas": 354.626,
      "ps0": 0,
      "cs0": 0,
      "p2": 458.643,
      "net": -332.004,
      "gts": 1712142000,
      "tm": 1513279322,
      "ts0": 1512921868
    }
  ],
  "friendly_name": "ls120_json"
}

Iihi @mad_ady thank you for your great work! I set up the custom componet but currently geht the following Info in the attributes colom:

senrows: [object Object],[object Object]
friendly_name: egardia_json

the Json I am trying to recieve is as follows:

{  
   "senrows":[  
      {  
         "area":1,
         "zone":1,
         "type":4,
         "type_f":"Door Contact",
         "name":"Door1",
         "cond":"",
         "cond_ok":"1",
         "battery":"",
         "battery_ok":"1",
         "tamper":"",
         "tamper_ok":"1",
         "bypass":"No",
         "rssi":"Strong, 9",
         "status":"Door Close",
         "id":"RF:007fc310",
         "su":1
      },
      {  
         "area":1,
         "zone":2,
         "type":15,
         "type_f":"Door Contact",
         "name":"Door2",
         "cond":"",
         "cond_ok":"1",
         "battery":"",
         "battery_ok":"1",
         "tamper":"",
         "tamper_ok":"1",
         "bypass":"No",
         "rssi":"N/A",
         "status":"",
         "id":"RF:024bf670",
         "su":0
      }
   ]
}

do you have a clue of what I might be doing wrong?

Thanks in advance!

EDIT:

Never mind, i found the issue. The json was displayed there and I managed to retrieve the attributes with the following template:

{{states.sensor.egardia_json.attributes.senrows[0]["status"].title()}}

3 Likes

I think that the newest (0.61) version of HA no longer displays nested attributes next to the object in the explorer interface. You now have to click the entity and it will load the attributes at the top of the page.

1 Like

I’ve used this custom sensor to grab some data that is >255 and it works great. I can populate my template sensors at startup with the then current values. However, the template sensors never get updated even when the values in the son change. Would the fact that it is in a list have anything to do with it?

I don’t know. You need to check that the entity has attributes which are parsed.

How do I get a sub attribute value out of a sensor. Example in the attached photo of polling my unifi gateway, I would like to get the values from gw_system-stats and then cpu and mem. I have used the following config to get similiar data from an attribute but not sure how to get it from a sub attribute. Any help would be greatly appreciated.

  • platform: template
    sensors:
    unifi_wlan_users:
    value_template: “{{ state_attr(‘sensor.unifi_gateway_wan.attributes.gw_system-stats’, ‘mem’) }}”