[SOLVED] - [Q] GET request to create HA sensor from value, I want to only show relevant 'field' out of a block of response

Hey All,
I am wondering how to get some values from an API using GET(I assume thats what I need)
I can’t say I have any real experience in this so forgive me but I have been trying to work it out.

I am trying to get my current portfolio value that is on Ghostfolio (and maybe other items if i can get that working) listed as a sensor within a local home assistant instance.

I am self hosting my instance so not too concerned about hitting it every hour or whatever. (I have anonymized the below URL/data values so they are not leaking my info)

It appears they have a restful sensor that can be created as per here;

When poking around in the network dev mode in the browser I believe i am trying to access;
https://ghostfol.io/api/v2/portfolio/performance?range=max

With some mucking about I have managed to run a curl get request in terminal such as this;
curl -X GET https://ghostfol.io/api/v2/portfolio/performance?range=max -H “Authorization: Bearer some-big-auth-token”

and I end up with a huge output of data.

It appears to be everyday… however i can see in there that there is this output.
“performance”:{“currentValue”:$$$$$,“currentGrossPerformance”:$$$$$$,“currentGrossPerformancePercent”:$$$$$$$,“currentNetPerformance”:$$$$$$,“currentNetPerformancePercent”:$$$$$$$,“totalInvestment”:$$$$$$}}
It would seem “currentValue” is what I am after but have no idea how to parse that in order to get the sensor to output just that value…

Thanks for any help.
I did try and have a look through their docs and the home assistant docs and a bunch of googling but as I have no real idea other than trial and error so far I am at a bit of a loss.

See maybe: Command line Sensor - Home Assistant

As for parsing JSON, there’s a utility called jq that can help, I think it’s part of home assistant os.

So probably, your command would be a tiny pipeline of (curl ..... | sed ... | jq) || echo "some alternative failure value"

2 Likes

Yep - shouldn’t need the sed even, if the Content-type is application/json. curl ... | jq -r .currentValue should do it.

If the JSON response is more complex than that, and it’s hard to find which element to output, try piping into gron - you can use the value paths as value selectors for jq.

1 Like

Thanks @risk and @xzpfzxds

I had also asked on the ghostfolio github and also got some help there.

What turned out to work is this;

Within my sensors.yaml file

#GhostFolio Performance Sensor
  - platform: command_line  
    command: 'curl -X GET https://ghostfolio.com/api/v2/portfolio/performance?range=max -H "Authorization: Bearer some-big-auth-token"'
    name: Ghostfolio
    json_attributes:
      - performance
    value_template: 'OK'

within my templates.yaml

#Ghostfolio Current Value
  -  sensor:
        name: Ghostfolio Current Value
        unit_of_measurement: "EUR"
        state: >
            {{ states.sensor.ghostfolio.attributes.performance.currentValue }}

They did state that I would need an automation to update the current value command line sensor and therefore the template sensor I actually want.

4 Likes