Extracting nested json attributes

Can someone help me with a way to extract a json attribute from a rest sensor.
The rest sensor looks like this:

{
    "code": 0,
    "msg": "Success",
    "data": {
        "vip": [
            {
                "volt": "233.1",
                "current": "7.7",
                "power": -53
            }
        ],
        "pac": -53,
        "qac": 0,
        "fac": 50.0,
        "pf": 1.0,
        "status": -1,
        "etodayFrom": "13.8",
        "etodayTo": "0.0",
        "etotalFrom": "81.4",
        "etotalTo": "14.0",
        "limiterPowerArr": [
            -53,
            0
        ],
        "limiterTotalPower": -53
    },
    "success": true
}

And created like this:

- platform: rest
  name: sunsynk_api_grid
  resource: https://pv.inteless.com/api/v1/inverter/grid/
  value_template: '1'
  method: GET
  scan_interval: 300
  headers:
    Authorization: >
      Bearer {{ states("sensor.sunsynk_api_bearer_token") }}
  json_attributes:
    - "code"
    - "msg"
    - "data"

So inside json_attributes how can I add the etotalFrom value which is inside of the data object?
Not sure of the syntax or if it’s even allowed?
Also is there a proper way to not get that 255 character limit error without using the value_template: '1' hack?

If you would use a curl to get to the sourcedata, then you could use this, afterwards, the attribs will work as ‘normal’
So it would be somethings alike this in a command_line sensor

curl "https://pv.inteless.com/api/v1/inverter/grid/" | jq '.data.etotalFrom as $a | .msg as $b | .code as $c | { etotalFrom: $a, msg: $b, code: $c }'

Ideally I would not bring in all the json attributes but do something like this:

json_attributes:
    - data.etodayFrom
    - data.etodayTo
    - data.etotalFrom
    - data.etotalTo

As I only need those values but that doesn’t work.
Not sure how to use curl with the auth stuff.

Sadly does not work…other option is to read the file into the attributes as a whole but it all depends on what you want to do afterwards. curl also has options to go through authorization, you have to find out what/how applies to your conn

How do I send a Curl request with a bearer token authorization header? (reqbin.com)

Guess will have to create extra template sensors from the json attributes which is not great but possible.

Not really, if you use a command_line sensor then you could put the output of curl + jq in the attributes
example from one of mine

  - platform: command_line
    name: testevents
    scan_interval: 1500
    command: >
         echo "{\"events\":" $(
         curl 
         -s 
         'https://api.fingrid.fi/v1/variable/336/events/json?start_time={{ (now()-timedelta(hours=4)).strftime('%Y-%m-%dT%H:%M:%SZ') }}&end_time={{ (now()+timedelta(hours=12)).strftime('%Y-%m-%dT%H:%M:%SZ') }}'
         ) "}" 
    value_template: > 
        {{ value_json.events | length }}
    json_attributes:
        - events
1 Like

If you are doubting
I put your output in a file and then tsetup this

  - platform: command_line
    name: testjson3
    scan_interval: 30000
    command: > 
        curl "http://192.168.1.20:8124/local/test3.json" | jq '.data.etotalFrom as $a | .msg as $b | .code as $c | { etotalFrom: $a, msg: $b, code: $c }'
    value_template: > 
        {{ value_json.code }}            
    json_attributes:
        - etotalFrom
        - code
        - msg

EDIT: just noticed that my jq can be a lot shorter, I donot need to define the variables first

jq '{ etotalFrom: .data.etotalFrom, msg: .msg, code: .code }'
1 Like

Not getting anything back from curl looks like.
Pretty sure it looks correct but maybe not.

curl -X POST -H "Authorization: Bearer 123456789" "https://pv.inteless.com/api/v1/inverter/grid" | jq '{ etotalFrom: .data.etotalFrom, msg: .msg, code: .code }'

I cannot comment on the curl itself, before even adding this to a sensor you can just run this from the command line (wihout jq) untill you recieve the json back…e.g. with/out POST (as you used GET all the way above)

Yeah it’s not a post, only getting the token is the post, it is a get which does work :+1:

Good, I use jq since 2 months, took me w while to get into it but plenty of examples on the net.

Question, scan_interval doesn’t seem to be doing anything, I have it set to 300 which is 5 mins and it didn’t do anything, i.e. run the command every 300 seconds and get latest data

Yeah, forgot about that one… I too have that issue. After a lot of searching on how to overcome this I was left with a workaround…trigger it by an automation…not nice but only thing I know of today

Command line sensor not updating - Configuration - Home Assistant Community (home-assistant.io)

Yeah I found the update entity service in my searches as well but it does seem to update, it’s just hard to see because the state doesn’t change so the last update stays the same but the attributes do change
Which leads me to the next problem, I need to set up an alert if the state/attributes haven’t changed in x amount of time which I can’t do if the state is always static