Getting data from value_json

Hi all

I hope someone can help.

I’ve created the following command line sensor:

# Trading 212 Pies
  - platform: command_line
    command: "curl -i -X GET https://live.trading212.com/api/v0/equity/pies -H 'Authorization: my_api_key'"
    name: trading_212_pies
    value_template: "{{ value_json[0].cash }}"
    scan_interval: 60

But I’m failing to get the value_template right.

I know that the cURL command works because if I run it in the HA terminal I get:

[{"id":1917009,"cash":n.nn,"dividendDetails":{"gained":0.00,"reinvested":0.00,"inCash":0.00},"result":{"investedValue":n.nn,"value":n.nn,"result":n.nn,"resultCoef":n.nn},"progress":null,"status":null},{"id":1927922,"cash":0.00,"dividendDetails":{"gained":0.00,"reinvested":0.00,"inCash":0.00},"result":{"investedValue":n.nn,"value":n.nn,"result":n.nn,"resultCoef":n.nnn},"progress":null,"status":null}]# 

What value_template should I use to get to the information returned?

Thanks in advance

That’s not valid json. So it’ll never work. You’ll have to regex the value out. For some reason, that json has n.nn all over the place which will break value_json.

Unless you’re changing those values… which cash was at 0.00 2 seconds ago and now is n.nn. Are you editing this post and adjusting the values?

Anyways if it’s truely not valid json,

{{ value | regex_findall('\"cash\":([0-9.n]+)' | list | first | float(0) }}
1 Like

What you have should work. What is happening, and what do you see if you use:

value_template: "{{ value[:250] }}"

That should display the first 250 characters to troubleshoot whether HA is getting the same response as curl from the CLI.

Why not use a Rest sensor instead?

1 Like

H @petro

Thanks for your reply.

Yes, I replaced the numbers with n.nn.

Does that make it possible to avoid the regex?

Yes, then do what @Troon is saying because that’s the next step into figuring out why your value_template isn’t working.

1 Like

Thanks @Troon ,

I’ve done as you suggested. I’m getting the following error in my log:

Logger: homeassistant.components.notify
Source: components/notify/legacy.py:94
Integration: Notifications (documentation, issues)
First occurred: 12:34:34 (1 occurrences)
Last logged: 12:34:34

Error setting up platform command_line
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/notify/legacy.py", line 94, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/command_line/notify.py", line 28, in get_service
    command: str = notify_config[CONF_COMMAND]
                   ~~~~~~~~~~~~~^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

How do I do this as a rest sensor? How do I get the Authorization and api key in?

Thanks

rest:
- scan_interval: 60
  resource: https://live.trading212.com/api/v0/equity/pies
  headers:
    Authorization: my_api_key
  sensor:
  - name: trading_212_pies
    value_template: "{{ value_json[0].cash }}"

Thanks @petro and @Troon

This worked:

# Trading 212
- resource: https://live.trading212.com/api/v0/equity/pies
  scan_interval: 3600
  headers:
    Authorization: my_api_key
  sensor:
  - name: trading_212_pies
    value_template: "{{ value[:250] }}"

Sadly I gor this:

{"code":"BusinessException","context":{"type":"TooManyRequests"},"errorMessage":"too many requests"}

Still, with your help I’m getting there - even if it is towards a dead end! I’ll have to see what the usage limits are.

Thanks again!

OK, so once your block has expired, your original value_template should give you the value you wanted.

Hey…do you have a solution for this? I would like to see my pies on HA dashboard.

I’m using this

# Trading 212
rest:
  - resource: https://live.trading212.com/api/v0/equity/account/cash
    scan_interval: 60
    headers:
      Authorization: YOUR API KEY
    sensor:
    - name: "TR^DING 212"
      unique_id: trading_212
      value_template: "{{ value_json.total }}"
      json_attributes:
        - free
        - total
        - ppl
        - result
        - invested
        - pieCash
        - blocked
      unit_of_measurement : GBP
      device_class: monetary
      state_class: total

@templeton_nash

this works for me also
Have you tried getting the Net Deposits somehow ?

thanks