API, command_line and REST sensor errors, solar panel optimisers

Hello, I’m trying to get home assistant sensors working from an API for solar panel optimisers. I’m running on a Raspberry Pi 4B and have tried using a bash script and a command line sensor (as in REST sensor and comma delimited data - #7 by mehstg), but I get an error where the sensor won’t show up in developer tools > states:

2024-02-23 14:53:03.938 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up command_line platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 344, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 66, in async_setup_platform
    name: str = sensor_config[CONF_NAME]
                ~~~~~~~~~~~~~^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

which is apparently a Python error and from what I can gather, the syntax has now changed since that post?

The bash script, which works perfectly when executed manually in terminal:

#!/bin/bash

curl --location --request GET "https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=<redacted>&start=$(date +"%Y-%m-%dT00:00:01")&end=$(date +"%Y-%m-%dT%H:%M:%S")&level=min&header=key" --header "Authorization: Bearer <REDACTED>" --silent | tail -1 | cut -d',' -f2-

That curl command returns a list of my 19 panel wattages, for example “8,4,8,4,8,4,8,8,7,7,9,9,9,9,7,7,9,9,9”. If there isn’t sufficient sunlight, some or all may return as blank (for example “8,4,8,4,8,7,7,7,7,9,9”) and I don’t know if that’s hurting my ability to get the sensors working?

My configuration.yaml:

# Pull in granular panel wattage from Tigo API
sensor:
  - platform: command_line
    command: "bash /homeassistant/tigo/powerPerPanelMin.sh"
    name: "Solar Panels"
    scan_interval: 120

I also tried instead of referring to the bash script, actually putting the curl command in the command line sensor, but apparently there’s an issue with how the date format is formatted and this is affecting the parsing of the command. I tried messing with this, but it prevents the Tigo API from returning correct data:.

sensor:
  - platform: command_line
    name: "Solar Panels"
    command: >-
      bash -c "curl --location --request GET "https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=<redacted>&start=$(date +"%Y-%m-%dT00:00:01")&end=$(date +"%Y-%m-%dT%H:%M:%S")&level=min&header=key" --header "Authorization: Bearer <redacted>" --silent | tail -1 | cut -d',' -f2-"
    scan_interval: 120

So I tried to setup a REST sensor, but the API is returning so much data that it exceeds the maximum length for a state, but I’m not knowledgable enough to fix it. I tried to add a template to just get the last minute of data, but again failed.

sensor:
  - platform: rest
    name: Solar Panels
    resource_template: >
      https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=<redacted>&start={{ now().strftime('%Y-%m-%dT00:00:01') }}&end={{ now().strftime('%Y-%m-%dT%H:%M:%S') }}&level=min&header=key
    method: GET
    headers:
      Authorization: "Bearer <redacted>"
    value_template: "{{ value.split(',')[1:] | join(',') }}"
    scan_interval: 60

I’ve tried using chatGPT over the last couple of days, but I don’t think it’s helping a lot, haha.
Apologies for the long post, but I feel like I’m really close to getting it sorted.

Thanks very much for anyones help,
Matthew.

You have quoting issues.

Try

bash -c 'curl --location --request GET "https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=<redacted>&start=$(date +''%Y-%m-%dT00:00:01'')&end=$(date +''%Y-%m-%dT%H:%M:%S'')&level=min&header=key" --header "Authorization: Bearer <redacted>" --silent | tail -1 | cut -d'','' -f2-'

Thanks for your help. I went backwards and forwards with ChatGPT on this so the clarification is useful. The error was in the layout of the command line sensor, which changed last year as far as I know:

# Pull in granular panel wattage from Tigo API
command_line:
 - sensor:
    command: "bash /config/tigo/powerPerPanelMin.sh"
    name: "Solar Panels"
    scan_interval: 60

The sensor works now. Am unsure as to whether it’ll still work when some of the values become blank, but we’ll see.

Thanks again

Can anyone help with an issue with the output of the script for current please? As the script returns 19 comma separated values (for example: 2.7058823529412,2.5,2.6764705882353,2.5,2.7058823529412,2.5454545454545,2.6666666666667,2.6969696969697,2.6363636363636,2.6666666666667,3,3,2.9705882352941,2.9705882352941,2.9090909090909,2.8484848484848,3.0294117647059,3.0294117647059,3.0294117647059), home assistant is sometimes giving an error while creating the sensor, as the state length sometimes exceeds 255 characters. I’ve been trying to figure out how to trim the script output to say 5 decimal places, but I can’t get it working.

The unmodified script:

curl --location --request GET "https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=redacted&start=$(date +"%Y-%m-%dT00:00:01")&end=$(date +"%Y-%m-%dT%H:%M:%S")&level=min&param=Iin&header=key" --header "Authorization: Bearer redacted" --silent | tail -1 | cut -d',' -f2-

I tried adding

 | awk 'BEGIN {OFS = FS} {for(i=1;i<=NF;i++) {$i = sprintf("%.2f", $i)} print $0}'

to the end, but it’s just returning a single number, instead of all 19 values.

Thanks very much

1 Like

I seem to have stumbled on the solution for anyone else with the issue:

#!/bin/bash

curl --location --request GET "https://api2.tigoenergy.com/api/v3/data/aggregate?system_id=redacted&start=$(date +"%Y-%m-%dT00:00:01")&end=$(date +"%Y-%m-%dT%H:%M:%S")&level=min&param=Iin&header=key" \
--header "Authorization: Bearer redacted" \
--silent | tail -1 | \
cut -d',' -f2- | \
awk -v OFS=',' '{for(i=1;i<=NF;i++) $i=sprintf("%.5f", $i)} 1' FS=','

gets me 5 decimal places.

Hi all,
i’m very interested about this topic.
I have TIGO system with premium account.
Sameone could help me to create the script?
many thanks

@matknowles I’ve followed your setup and I’m getting the following error. Any ideas?

Logger: homeassistant.components.command_line.utils
Source: components/command_line/utils.py:62
integration: Command Line (documentation, issues)
First occurred: 23 July 2024 at 23:42:48 (89 occurrences)
Last logged: 06:08:48

Timeout for command: bash /config/custom_components/tigo_api/scripts/tigoRSSI.sh
Timeout for command: bash /config/custom_components/tigo_api/scripts/tigoPin.sh
Timeout for command: bash /config/custom_components/tigo_api/scripts/tigoVin.sh
Timeout for command: bash /config/custom_components/tigo_api/scripts/tigoIin.sh