Curl API service call extract

I use shell_command and created a service calling API using curl command, that service runs every minute and gives me the following output:
stdout: >-
{“pvSystemId”:“b16d8d79-566d-449d-b809-a5ee0164faf9”,“status”:{“isOnline”:true,“battMode”:1.0},“data”:{“logDateTime”:“2024-07-03T12:34:10Z”,“channels”:[{“channelName”:“PowerFeedIn”,“channelType”:“Power”,“unit”:“W”,“value”:-1455.02},{“channelName”:“PowerLoad”,“channelType”:“Power”,“unit”:“W”,“value”:-81.38},{“channelName”:“PowerBattCharge”,“channelType”:“Power”,“unit”:“W”,“value”:-2521.10},{“channelName”:“PowerPV”,“channelType”:“Power”,“unit”:“W”,“value”:4057.5},{“channelName”:“PowerOutput”,“channelType”:“Power”,“unit”:“W”,“value”:null},{“channelName”:“BattSOC”,“channelType”:“Percentage”,“unit”:“%”,“value”:99.0},{“channelName”:“RateSelfConsumption”,“channelType”:“Percentage”,“unit”:“%”,“value”:5.30},{“channelName”:“RateSelfSufficiency”,“channelType”:“Percentage”,“unit”:“%”,“value”:100.0},{“channelName”:“PowerEVCTotal”,“channelType”:“Power”,“unit”:“W”,“value”:null}]}}
stderr: “% Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0\r100 878 100 878 0 0 2083 0 --:–:-- --:–:-- --:–:-- 2080”
returncode: 0

This is fine so far, now I want to use the values from that output i.e:
{“channelName”:“PowerPV”,“channelType”:“Power”,“unit”:“W”,“value”:4057.5}, to create an entity. Is this somehow possible to do? This is from my PV system and I want to use the values in lovelace dashboard to get them updated once a minute.
I know there is a custom integration available for this, but unfortunatley this is not working properly for me, so I try to run the API calls in services and that works fine.

What do you do with the output of that shell command?

This would probably be better done as a Restful integration and sensors (might need to add authentication as required):

rest:
  - resource: [URL]
    scan_interval: 60
    sensor:
      - name: PowerPV
        value_template: "{{ (value_json['data']['channels']|selectattr('channelName','eq','PowerPV')|first)['value'] }}"
        unit_of_measurement: W
        device_class: power
      - name: BattSOC
        value_template: "{{ (value_json['data']['channels']|selectattr('channelName','eq','BattSOC')|first)['value'] }}"
        unit_of_measurement: '%'
        device_class: battery

and similarly for the others. After a restart, that will give you sensor.powerpv and sensor.battsoc with the appropriate data in for adding to a dashboard card.

To help you work out the templates needed, paste this in Developer Tools / Template and have a play around:

{% set value_json = {"pvSystemId":"b16d8d79-566d-449d-b809-a5ee0164faf9","status":{"isOnline":true,"battMode":1.0},"data":{"logDateTime":"2024-07-03T12:34:10Z","channels":[{"channelName":"PowerFeedIn","channelType":"Power","unit":"W","value":-1455.02},{"channelName":"PowerLoad","channelType":"Power","unit":"W","value":-81.38},{"channelName":"PowerBattCharge","channelType":"Power","unit":"W","value":-2521.10},{"channelName":"PowerPV","channelType":"Power","unit":"W","value":4057.5},{"channelName":"PowerOutput","channelType":"Power","unit":"W","value":null},{"channelName":"BattSOC","channelType":"Percentage","unit":"%","value":99.0},{"channelName":"RateSelfConsumption","channelType":"Percentage","unit":"%","value":5.30},{"channelName":"RateSelfSufficiency","channelType":"Percentage","unit":"%","value":100.0},{"channelName":"PowerEVCTotal","channelType":"Power","unit":"W","value":null}]}} %}
{{ (value_json['data']['channels']|selectattr('channelName','eq','PowerPV')|first)['value'] }}

In future, please post code like your curl response formatted correctly using the </> button or surrounding with backticks:

```
CODE GOES HERE
```

…otherwise the forum software tries to format it and messes up indentation, quotes and other structures. I had to search and replace what you posted above to fix the quotes.

Thanks for your answer, this looks good, just have a question about the URL: I need to add the API key as well in the URL, the request looks like this:

curl -X GET "https://api.solarweb.com/swqapi/pvsystems/b16d8d79-566d-449d-b809-a5ee0164faf9/flowdata" -H "accept: application/json" -H "AccessKeyId: xxxxxxxxxxxxxxx" -H "AccessKeyValue: yyyyyyyyyyyyy"

Blockquote

Is this something I can use as resource URL?

Should be easy enough:

rest:
  - resource: https://api.solarweb.com/swqapi/pvsystems/b16d8d79-566d-449d-b809-a5ee0164faf9/flowdata
    scan_interval: 60
    headers:
      AccessKeyId: xxxx
      AccessKeyValue: yyyy
      Accept: application/json
    sensor:
[etc]

Hey Troon, this looks good, I think I will be able to get this implemented, I just need to change it a bit since I am using -platform and as I am quite new on HA I need to figure out how to change this so it will work in my config.
Thanks a lot!

The advantage of the RESTFul integration (under rest:) is that you can create multiple sensors from a single API request.

Just add everything above to the end of configuration.yaml, assuming you don’t already have a rest: heading.

yes that is the thing I need to figure out, as I already have a rest: heading in my config:

Blockquote #Boiler State
sensor:

  • platform: rest
    resource: http://192.168.1.241/digital_out.json
    name: EMPS output 1
    value_template: “{{value_json[‘pin 1’][‘set’] }}”

  • platform: template
    sensors:
    boiler_heater_status:
    value_template: >-
    {% if ( states.sensor.emps_output_1.state != ‘True’) %}
    off
    {% else %}
    on
    {% endif %}

That’s not a rest: heading, it’s a sensor: heading. I’m talking about the most left-aligned level of your YAML.

If you do already have one of those, put my config under it but excluding the duplicate rest:.

Please see my first reply to you about formatting code.

ok great, this works, thank you so much for your time helping with this, when I add the BattSOC then I get an error in the screenshot attache here something about mapping. The PowerPV works fine already,

I think the “%” character is not allowed in the unit_of_measurement

That may well be it. Put the % in quotes and see if that works. Corrected in my earlier post too.

it works perfeclty fine in quotes, thank you so much for your support, this is a much better solution than the Fronius integration!

1 Like