REST sensor pv forecast postprocessing issues

Hi all,
I am reading via rest api the solar forecast for my pv (via Swagger UI). This is working fine and I get the following sensor:

  - scan_interval: 3610
    resource: https://api.forecast.solar/estimate/xx.xxx/xx.xxx/45/-100/5.5
    method: GET
    sensor:
      - name: "Solar Forecast East Watt"
        json_attributes_path: "$.result"
        json_attributes:
          - watts
        value_template: 'OK'
      - name: "Solar Forecast East Hours"
        json_attributes_path: "$.result"
        json_attributes:
          - watt_hours
        value_template: 'OK'
      - name: "Solar Forecast East Hours Day"
        json_attributes_path: "$.result"
        json_attributes:
          - watt_hours_day
        value_template: 'OK'

The same I have made for the west and south orientated pv modules.

Now I have two problems:

1.) At the moment the status of this sensore is always OK. How do I now get the value from the attribute list displayed as a state at the respective hour? (e.g. on the 20.02.2023 between 1000 and 1100 the status of the entity should be 2104.)
2.) I would like to add the sensor.solar_forecast_east_watt attribute ‘watts’ with the sensor.solar_forecast_west_watt attribute ‘watts’ to one sensor sensor.solar_forecast_east_west_watt attribute ‘watts’. How can I archieve this?

Thank you very much in advance!

Use the value_template rather than just setting it to “OK”. It also depends on how your data is presented, if it is a list this should work:

    sensor:
      - name: "Solar Forecast East Watt"
        json_attributes_path: "$.result"
        json_attributes:
          - watts
        value_template: "{{ value_json.result.watts[0]|slpit(': ')[1] }}"

Are the times for each set of attributes exactly the same?

Thank you for your fast answer!

Ad1: Sorry, I am not sure how it gets presented (does the screenshot in my last post maybe give us more infos about this?) How can I find this out? Nevertheless, I tried your code and when trying to restart HA I get the following error:

The system cannot restart because the configuration is not valid: Invalid config for [rest]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value @ data['rest'][1]['sensor'][0]['value_template']. Got "{{ value_json.result.watts[0]|slpit(': ')[1] }}". (See /config/configuration.yaml, line 39).

Ad2: Yes, the times are always the same between the different sensors.

Thank you!

Isn’t split spelled wrong? It is in the error.

you are right - but after changing the spelling error I recieve the same error again :frowning:

It looks to me that you data is not a single string, the key is the data and the value is the watts. So it should just be value_json.result.watts[0][1] (no split). Or maybe even value_json.watts[0][1] without seeing what is in the JSON response, I do not see where result is but it must be there if the top screen works.

In developer tools, you should enter:

{{state_attr('sensor.solar_forecast_east_watt','watts')[0][1]}}

and see what you get. Test various variations there and then you will see the proper way. I would think the above would return 1142 given the numbers above, the second entry is a 0 indexed set.

Thanks for you help. In the developer tool I get the following error:

Is there a way to look at the raw JSON response in HA? I believe it should look like this:
image

Use “| tojson” so:

{{ state_attr('sensor.solar_forecast_east_watt','watts') | tojson }}

I now found out that it is working if I put the date/time in the []

Now the question is how I can make it change each hour… So somehow I would have to read in the actual time and compare it with all entries of the json…

Personally I can tell you what I would do. I have many, many sensors that come from REST JSON data and I actually use command line sensor and not the REST sensor.

I do this because I can then run JQ with the raw JSON result and reorganize it how I want it. Like in your example, it would like be better if the result was structured without a “moving key” that is the date but instead

{'date': '2023-02-20 06:52:00', watt: 0}

You would need to learn JQ unless you already know it. An example I have for flood river levels is like this:

##
## water.weather.gov CSV
##
- platform: command_line
  scan_interval: 3600
  name: Russian River Flood Info
  command: "curl -s 'https://www.cnrfc.noaa.gov/graphicalRVF_csv.php?id=GUEC1' | jq --raw-input '{flooddata: [inputs | split(\",\") | {issued: .[0], valid: .[1] | strptime(\"%m/%d/%Y %I %p\") | strftime(\"%m/%d/%Y %I:%M %p\"), validts: .[1] | strptime(\"%m/%d/%Y %I %p\") | strftime(\"%Y-%d-%m %H:%M:%S\"), level: .[2] | tonumber, trend: .[3], status: .[4], mode: .[5] | gsub(\"[\\r]\"; \"\")  }]| .[2:999] }'"
  value_template: >
        {% set levels = namespace(lvl=[]) %}  
        {% for height in value_json.flooddata %}
          {% set levels.lvl = levels.lvl + [height.level] %}
        {% endfor %}
        {{levels.lvl | max }}
  json_attributes:
    - flooddata

That one actually converts CSV data to JSON.

Instead of enslaving yourself to someone’s bad design for JSON data, turn it into what you can use.

Google JQ PLayground and you can paste in the JSON and write your template to process it into something that works for you.

By the way, you could using this method structure all of this into one sensor with attributes for each and also do the math you wanted to sum up into another attribute of the single sensor. Unless you want automations to work on various states, I typically go that route. A REST sensor I have for Pokemon data is like this, with most all the information in attributes of one sensor.

- platform: rest
  name: Random Pokemon
  unique_id: sensor.random_pokemon
  scan_interval: 360000
  resource_template: >
      {% if states('input_select.select_pokemon_mode') == 'Increment' %}
        https://pokeapi.co/api/v2/pokemon/{{ state_attr('sensor.random_pokemon','id') + 1 }}
      {% elif states('input_select.select_pokemon_mode') == 'Decrement' %}
        https://pokeapi.co/api/v2/pokemon/{{ state_attr('sensor.random_pokemon','id') - 1 }}
      {% elif states('input_select.select_pokemon_mode') == 'Name' %}
        https://pokeapi.co/api/v2/pokemon/{{ states('input_select.pokedex') }}
      {% else %}
        https://pokeapi.co/api/v2/pokemon/{{ range(1, 1008) | random }}
      {% endif %}
  value_template: "{{ value_json.name }}"
  json_attributes:
    - abilities
    - base_experience
    - forms
    - game_indicies
    - height
    - held_items
    - id
    - is_default
    - location_area_encounters
    - moves
    - name
    - order
    - past_types
    - species
    - sprites
    - stats
    - types
    - weight

thank you for your nice inputs! The API also sends the data in a csv format:

e.g.:

curl -X 'GET' \
  'https://api.forecast.solar/estimate/47.00000/15.0000/45/-10/10?time=utc' \
  -H 'accept: text/csv' \
  -H 'X-Delimiter: |' \
  -H 'X-Separator: ;'

the output would look like:

watts;2023-02-21T06:50:00+01:00;0
watts;2023-02-21T07:00:00+01:00;1928
watts;2023-02-21T08:00:00+01:00;2686
watts;2023-02-21T09:00:00+01:00;5054
watts;2023-02-21T10:00:00+01:00;7466
watts;2023-02-21T11:00:00+01:00;7797
watts;2023-02-21T12:00:00+01:00;7520
watts;2023-02-21T13:00:00+01:00;6585
watts;2023-02-21T14:00:00+01:00;5084
watts;2023-02-21T15:00:00+01:00;3235
watts;2023-02-21T16:00:00+01:00;1345
watts;2023-02-21T17:00:00+01:00;294
watts;2023-02-21T17:31:00+01:00;0
watts;2023-02-22T06:49:00+01:00;0
watts;2023-02-22T07:00:00+01:00;2637
watts;2023-02-22T08:00:00+01:00;4629
watts;2023-02-22T09:00:00+01:00;6155
watts;2023-02-22T10:00:00+01:00;6639
watts;2023-02-22T11:00:00+01:00;6582
watts;2023-02-22T12:00:00+01:00;6124
watts;2023-02-22T13:00:00+01:00;5135
watts;2023-02-22T14:00:00+01:00;3412
watts;2023-02-22T15:00:00+01:00;1816
watts;2023-02-22T16:00:00+01:00;721
watts;2023-02-22T17:00:00+01:00;189
watts;2023-02-22T17:33:00+01:00;0
watt_hours_period;2023-02-21T06:50:00+01:00;0
watt_hours_period;2023-02-21T07:00:00+01:00;161
watt_hours_period;2023-02-21T08:00:00+01:00;2307
watt_hours_period;2023-02-21T09:00:00+01:00;3870
watt_hours_period;2023-02-21T10:00:00+01:00;6260
watt_hours_period;2023-02-21T11:00:00+01:00;7632
watt_hours_period;2023-02-21T12:00:00+01:00;7659
watt_hours_period;2023-02-21T13:00:00+01:00;7053
watt_hours_period;2023-02-21T14:00:00+01:00;5835
watt_hours_period;2023-02-21T15:00:00+01:00;4160
watt_hours_period;2023-02-21T16:00:00+01:00;2290
watt_hours_period;2023-02-21T17:00:00+01:00;820
watt_hours_period;2023-02-21T17:31:00+01:00;76
watt_hours_period;2023-02-22T06:49:00+01:00;0
watt_hours_period;2023-02-22T07:00:00+01:00;242
watt_hours_period;2023-02-22T08:00:00+01:00;3633
watt_hours_period;2023-02-22T09:00:00+01:00;5392
watt_hours_period;2023-02-22T10:00:00+01:00;6397
watt_hours_period;2023-02-22T11:00:00+01:00;6611
watt_hours_period;2023-02-22T12:00:00+01:00;6353
watt_hours_period;2023-02-22T13:00:00+01:00;5630
watt_hours_period;2023-02-22T14:00:00+01:00;4274
watt_hours_period;2023-02-22T15:00:00+01:00;2614
watt_hours_period;2023-02-22T16:00:00+01:00;1269
watt_hours_period;2023-02-22T17:00:00+01:00;455
watt_hours_period;2023-02-22T17:33:00+01:00;52
watt_hours;2023-02-21T06:50:00+01:00;0
watt_hours;2023-02-21T07:00:00+01:00;161
watt_hours;2023-02-21T08:00:00+01:00;2468
watt_hours;2023-02-21T09:00:00+01:00;6338
watt_hours;2023-02-21T10:00:00+01:00;12598
watt_hours;2023-02-21T11:00:00+01:00;20230
watt_hours;2023-02-21T12:00:00+01:00;27889
watt_hours;2023-02-21T13:00:00+01:00;34942
watt_hours;2023-02-21T14:00:00+01:00;40777
watt_hours;2023-02-21T15:00:00+01:00;44937
watt_hours;2023-02-21T16:00:00+01:00;47227
watt_hours;2023-02-21T17:00:00+01:00;48047
watt_hours;2023-02-21T17:31:00+01:00;48123
watt_hours;2023-02-22T06:49:00+01:00;0
watt_hours;2023-02-22T07:00:00+01:00;242
watt_hours;2023-02-22T08:00:00+01:00;3875
watt_hours;2023-02-22T09:00:00+01:00;9267
watt_hours;2023-02-22T10:00:00+01:00;15664
watt_hours;2023-02-22T11:00:00+01:00;22275
watt_hours;2023-02-22T12:00:00+01:00;28628
watt_hours;2023-02-22T13:00:00+01:00;34258
watt_hours;2023-02-22T14:00:00+01:00;38532
watt_hours;2023-02-22T15:00:00+01:00;41146
watt_hours;2023-02-22T16:00:00+01:00;42415
watt_hours;2023-02-22T17:00:00+01:00;42870
watt_hours;2023-02-22T17:33:00+01:00;42922
watt_hours_day;2023-02-21;48123
watt_hours_day;2023-02-22;42922

I will take a look into the JQ topic, but maybe you can help me with it :blush:

Always happy to help. You could almost use the sensor I posted above. Change the split to “;” from “,” and you don;t need to reformat the dates. As I do not have you data to test, this is just a guess:

Something like this?

Put that into the command line sensor and setting the attribute to “watts”. You will only need to be sure to escape quotes inside the JQ command because it is inside more quoted items.

Thank you, I just tried to do it, but I didn’T succeed. In the csv output I the as .[0] watts,watt_hours_period,watt_hours,watt_hours_day as .[1] the date/time and as .[2] the value.

How can I archive something like on the left side in the image?

At the moment I get something like on the right side. My jq code looks the following:
{ south: [inputs | split(";") | {types: .[0], date: .[1], values: .[2] } ] }

The complete .csv output can be found the post above: REST sensor pv forecast postprocessing issues - #11 by u20p17

Here ya go:

{ south: [(inputs | split(";")) | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber } ] | 
group_by(.label) | map({ key: (.[0].label), value: [.[] ] }) | from_entries
}

Yields:

{
  "south": {
    "watt_hours": [
      {
        "label": "watt_hours",
        "date": "2023-02-21T06:50:00+01:00",
        "values": 0
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T07:00:00+01:00",
        "values": 161
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2468
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T09:00:00+01:00",
        "values": 6338
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T10:00:00+01:00",
        "values": 12598
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T11:00:00+01:00",
        "values": 20230
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T12:00:00+01:00",
        "values": 27889
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T13:00:00+01:00",
        "values": 34942
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T14:00:00+01:00",
        "values": 40777
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T15:00:00+01:00",
        "values": 44937
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T16:00:00+01:00",
        "values": 47227
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T17:00:00+01:00",
        "values": 48047
      },
      {
        "label": "watt_hours",
        "date": "2023-02-21T17:31:00+01:00",
        "values": 48123
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T07:00:00+01:00",
        "values": 242
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T08:00:00+01:00",
        "values": 3875
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T09:00:00+01:00",
        "values": 9267
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T10:00:00+01:00",
        "values": 15664
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T11:00:00+01:00",
        "values": 22275
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T12:00:00+01:00",
        "values": 28628
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T13:00:00+01:00",
        "values": 34258
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T14:00:00+01:00",
        "values": 38532
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T15:00:00+01:00",
        "values": 41146
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T16:00:00+01:00",
        "values": 42415
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T17:00:00+01:00",
        "values": 42870
      },
      {
        "label": "watt_hours",
        "date": "2023-02-22T17:33:00+01:00",
        "values": 42922
      }
    ],
    "watt_hours_day": [
      {
        "label": "watt_hours_day",
        "date": "2023-02-21",
        "values": 48123
      },
      {
        "label": "watt_hours_day",
        "date": "2023-02-22",
        "values": 42922
      }
    ],
    "watt_hours_period": [
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T06:50:00+01:00",
        "values": 0
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T07:00:00+01:00",
        "values": 161
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2307
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T09:00:00+01:00",
        "values": 3870
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T10:00:00+01:00",
        "values": 6260
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T11:00:00+01:00",
        "values": 7632
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T12:00:00+01:00",
        "values": 7659
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T13:00:00+01:00",
        "values": 7053
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T14:00:00+01:00",
        "values": 5835
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T15:00:00+01:00",
        "values": 4160
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T16:00:00+01:00",
        "values": 2290
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T17:00:00+01:00",
        "values": 820
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-21T17:31:00+01:00",
        "values": 76
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T07:00:00+01:00",
        "values": 242
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T08:00:00+01:00",
        "values": 3633
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T09:00:00+01:00",
        "values": 5392
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T10:00:00+01:00",
        "values": 6397
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T11:00:00+01:00",
        "values": 6611
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T12:00:00+01:00",
        "values": 6353
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T13:00:00+01:00",
        "values": 5630
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T14:00:00+01:00",
        "values": 4274
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T15:00:00+01:00",
        "values": 2614
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T16:00:00+01:00",
        "values": 1269
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T17:00:00+01:00",
        "values": 455
      },
      {
        "label": "watt_hours_period",
        "date": "2023-02-22T17:33:00+01:00",
        "values": 52
      }
    ],
    "watts": [
      {
        "label": "watts",
        "date": "2023-02-21T07:00:00+01:00",
        "values": 1928
      },
      {
        "label": "watts",
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2686
      },
      {
        "label": "watts",
        "date": "2023-02-21T09:00:00+01:00",
        "values": 5054
      },
      {
        "label": "watts",
        "date": "2023-02-21T10:00:00+01:00",
        "values": 7466
      },
      {
        "label": "watts",
        "date": "2023-02-21T11:00:00+01:00",
        "values": 7797
      },
      {
        "label": "watts",
        "date": "2023-02-21T12:00:00+01:00",
        "values": 7520
      },
      {
        "label": "watts",
        "date": "2023-02-21T13:00:00+01:00",
        "values": 6585
      },
      {
        "label": "watts",
        "date": "2023-02-21T14:00:00+01:00",
        "values": 5084
      },
      {
        "label": "watts",
        "date": "2023-02-21T15:00:00+01:00",
        "values": 3235
      },
      {
        "label": "watts",
        "date": "2023-02-21T16:00:00+01:00",
        "values": 1345
      },
      {
        "label": "watts",
        "date": "2023-02-21T17:00:00+01:00",
        "values": 294
      },
      {
        "label": "watts",
        "date": "2023-02-21T17:31:00+01:00",
        "values": 0
      },
      {
        "label": "watts",
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "label": "watts",
        "date": "2023-02-22T07:00:00+01:00",
        "values": 2637
      },
      {
        "label": "watts",
        "date": "2023-02-22T08:00:00+01:00",
        "values": 4629
      },
      {
        "label": "watts",
        "date": "2023-02-22T09:00:00+01:00",
        "values": 6155
      },
      {
        "label": "watts",
        "date": "2023-02-22T10:00:00+01:00",
        "values": 6639
      },
      {
        "label": "watts",
        "date": "2023-02-22T11:00:00+01:00",
        "values": 6582
      },
      {
        "label": "watts",
        "date": "2023-02-22T12:00:00+01:00",
        "values": 6124
      },
      {
        "label": "watts",
        "date": "2023-02-22T13:00:00+01:00",
        "values": 5135
      },
      {
        "label": "watts",
        "date": "2023-02-22T14:00:00+01:00",
        "values": 3412
      },
      {
        "label": "watts",
        "date": "2023-02-22T15:00:00+01:00",
        "values": 1816
      },
      {
        "label": "watts",
        "date": "2023-02-22T16:00:00+01:00",
        "values": 721
      },
      {
        "label": "watts",
        "date": "2023-02-22T17:00:00+01:00",
        "values": 189
      },
      {
        "label": "watts",
        "date": "2023-02-22T17:33:00+01:00",
        "values": 0
      }
    ]
  }
}

I could not quite figure out striping the label from the object but just don;t use it, not much data there anyway. I did include stripping \r and converting the values to a number.

Update:

Just change like this:

group_by(.label) | map({ key: (.[0].label), value: [.[] | {date: .date, values: .values}] }) | from_entries

If you want to remove the label:

{
  "south": {
    "watt_hours": [
      {
        "date": "2023-02-21T06:50:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-21T07:00:00+01:00",
        "values": 161
      },
      {
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2468
      },
      {
        "date": "2023-02-21T09:00:00+01:00",
        "values": 6338
      },
      {
        "date": "2023-02-21T10:00:00+01:00",
        "values": 12598
      },
      {
        "date": "2023-02-21T11:00:00+01:00",
        "values": 20230
      },
      {
        "date": "2023-02-21T12:00:00+01:00",
        "values": 27889
      },
      {
        "date": "2023-02-21T13:00:00+01:00",
        "values": 34942
      },
      {
        "date": "2023-02-21T14:00:00+01:00",
        "values": 40777
      },
      {
        "date": "2023-02-21T15:00:00+01:00",
        "values": 44937
      },
      {
        "date": "2023-02-21T16:00:00+01:00",
        "values": 47227
      },
      {
        "date": "2023-02-21T17:00:00+01:00",
        "values": 48047
      },
      {
        "date": "2023-02-21T17:31:00+01:00",
        "values": 48123
      },
      {
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-22T07:00:00+01:00",
        "values": 242
      },
      {
        "date": "2023-02-22T08:00:00+01:00",
        "values": 3875
      },
      {
        "date": "2023-02-22T09:00:00+01:00",
        "values": 9267
      },
      {
        "date": "2023-02-22T10:00:00+01:00",
        "values": 15664
      },
      {
        "date": "2023-02-22T11:00:00+01:00",
        "values": 22275
      },
      {
        "date": "2023-02-22T12:00:00+01:00",
        "values": 28628
      },
      {
        "date": "2023-02-22T13:00:00+01:00",
        "values": 34258
      },
      {
        "date": "2023-02-22T14:00:00+01:00",
        "values": 38532
      },
      {
        "date": "2023-02-22T15:00:00+01:00",
        "values": 41146
      },
      {
        "date": "2023-02-22T16:00:00+01:00",
        "values": 42415
      },
      {
        "date": "2023-02-22T17:00:00+01:00",
        "values": 42870
      },
      {
        "date": "2023-02-22T17:33:00+01:00",
        "values": 42922
      }
    ],
    "watt_hours_day": [
      {
        "date": "2023-02-21",
        "values": 48123
      },
      {
        "date": "2023-02-22",
        "values": 42922
      }
    ],
    "watt_hours_period": [
      {
        "date": "2023-02-21T06:50:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-21T07:00:00+01:00",
        "values": 161
      },
      {
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2307
      },
      {
        "date": "2023-02-21T09:00:00+01:00",
        "values": 3870
      },
      {
        "date": "2023-02-21T10:00:00+01:00",
        "values": 6260
      },
      {
        "date": "2023-02-21T11:00:00+01:00",
        "values": 7632
      },
      {
        "date": "2023-02-21T12:00:00+01:00",
        "values": 7659
      },
      {
        "date": "2023-02-21T13:00:00+01:00",
        "values": 7053
      },
      {
        "date": "2023-02-21T14:00:00+01:00",
        "values": 5835
      },
      {
        "date": "2023-02-21T15:00:00+01:00",
        "values": 4160
      },
      {
        "date": "2023-02-21T16:00:00+01:00",
        "values": 2290
      },
      {
        "date": "2023-02-21T17:00:00+01:00",
        "values": 820
      },
      {
        "date": "2023-02-21T17:31:00+01:00",
        "values": 76
      },
      {
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-22T07:00:00+01:00",
        "values": 242
      },
      {
        "date": "2023-02-22T08:00:00+01:00",
        "values": 3633
      },
      {
        "date": "2023-02-22T09:00:00+01:00",
        "values": 5392
      },
      {
        "date": "2023-02-22T10:00:00+01:00",
        "values": 6397
      },
      {
        "date": "2023-02-22T11:00:00+01:00",
        "values": 6611
      },
      {
        "date": "2023-02-22T12:00:00+01:00",
        "values": 6353
      },
      {
        "date": "2023-02-22T13:00:00+01:00",
        "values": 5630
      },
      {
        "date": "2023-02-22T14:00:00+01:00",
        "values": 4274
      },
      {
        "date": "2023-02-22T15:00:00+01:00",
        "values": 2614
      },
      {
        "date": "2023-02-22T16:00:00+01:00",
        "values": 1269
      },
      {
        "date": "2023-02-22T17:00:00+01:00",
        "values": 455
      },
      {
        "date": "2023-02-22T17:33:00+01:00",
        "values": 52
      }
    ],
    "watts": [
      {
        "date": "2023-02-21T07:00:00+01:00",
        "values": 1928
      },
      {
        "date": "2023-02-21T08:00:00+01:00",
        "values": 2686
      },
      {
        "date": "2023-02-21T09:00:00+01:00",
        "values": 5054
      },
      {
        "date": "2023-02-21T10:00:00+01:00",
        "values": 7466
      },
      {
        "date": "2023-02-21T11:00:00+01:00",
        "values": 7797
      },
      {
        "date": "2023-02-21T12:00:00+01:00",
        "values": 7520
      },
      {
        "date": "2023-02-21T13:00:00+01:00",
        "values": 6585
      },
      {
        "date": "2023-02-21T14:00:00+01:00",
        "values": 5084
      },
      {
        "date": "2023-02-21T15:00:00+01:00",
        "values": 3235
      },
      {
        "date": "2023-02-21T16:00:00+01:00",
        "values": 1345
      },
      {
        "date": "2023-02-21T17:00:00+01:00",
        "values": 294
      },
      {
        "date": "2023-02-21T17:31:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-22T06:49:00+01:00",
        "values": 0
      },
      {
        "date": "2023-02-22T07:00:00+01:00",
        "values": 2637
      },
      {
        "date": "2023-02-22T08:00:00+01:00",
        "values": 4629
      },
      {
        "date": "2023-02-22T09:00:00+01:00",
        "values": 6155
      },
      {
        "date": "2023-02-22T10:00:00+01:00",
        "values": 6639
      },
      {
        "date": "2023-02-22T11:00:00+01:00",
        "values": 6582
      },
      {
        "date": "2023-02-22T12:00:00+01:00",
        "values": 6124
      },
      {
        "date": "2023-02-22T13:00:00+01:00",
        "values": 5135
      },
      {
        "date": "2023-02-22T14:00:00+01:00",
        "values": 3412
      },
      {
        "date": "2023-02-22T15:00:00+01:00",
        "values": 1816
      },
      {
        "date": "2023-02-22T16:00:00+01:00",
        "values": 721
      },
      {
        "date": "2023-02-22T17:00:00+01:00",
        "values": 189
      },
      {
        "date": "2023-02-22T17:33:00+01:00",
        "values": 0
      }
    ]
  }
}

thank you very much again!

I now tried to add the following sensor to the configuraion.yaml:

  - platform: command_line
    scan_interval: 3610
    name: Solar Forecast East
    command: "curl -X 'https://api.forecast.solar/estimate/47.00000/15.000000/45/-10/10?time=utc' -H 'accept: text/csv' -H 'X-Delimiter: |' -H 'X-Separator: ;' | jq --raw-input '{ south: [(inputs | split(";")) | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber } ] | group_by(.label) | map({ key: (.[0].label), value: [.[] ] }) | from_entries }' "
    value_template: "OK"
    json_attributes:
      - watts
      - watt_hours
      - watt_hours_period
      - watt_hours_day

Nevertheless, I get an bad indentation error in the “command:” line. When I remove the

{ south: [(inputs | split(";")) | {label: .[0], date: .[1], values: .[2] | gsub("[\r]"; "") | tonumber } ] | 
group_by(.label) | map({ key: (.[0].label), value: [.[] ] }) | from_entries
}

the error does disappear. So it must be somehow in the jq code, which is working in jqplay :frowning_face:

You need to escape the inner double quotes because the whole command is inside double quotes the ones inside the JQ template need to be escaped. You do this by adding a \ before every double quote.

    command: "curl -X 'https://api.forecast.solar/estimate/47.00000/15.000000/45/-10/10?time=utc' -H 'accept: text/csv' -H 'X-Delimiter: |' -H 'X-Separator: ;' | jq --raw-input '{ south: [(inputs | split(\";\")) | {label: .[0], date: .[1], values: .[2] | gsub(\"[\\r]\"; \"\") | tonumber } ] | group_by(.label) | map({ key: (.[0].label), value: [.[] ] }) | from_entries }' "

see the entries for the split and gsub parts are escaped. You can see that in the command_line sensor I posted above for the flooddata river level. You aslo need to escape the \ in the \r in gsub. Fixed that in the sample.

Just to be sure you see what I am saying, the gsub should look like this:

gsub(\"[\\r]\"; \"\") 

and the split like this:

split(\";\")

I would also note that your sample has a key overall of “south” you would need to set the json_attributes_path to “$.south” I believe if you really want the four things below that to be attributes. Or just set the attribute to “south” and then write your template to be below that.

And one last note, the name of your sensor is “Solar Forecast East” and the key is “south” … possibly you are just getting this rolling or maybe something needs correction.

You can’t use json_attributes_path with command_line. I also got errors using -X … here is what works for me:

 ##
 ## Test Solar Forecast
 ##
- platform: command_line
  scan_interval: 3610
  name: Solar Forecast South
  command: "curl -s 'https://api.forecast.solar/estimate/47.00000/15.000000/45/-10/10?time=utc' -H 'accept: text/csv' -H 'X-Delimiter: |' -H 'X-Separator: ;' | jq --raw-input '{ south: [(inputs | split(\";\")) | {label: .[0], date: .[1], values: .[2] | gsub(\"[\\r]\"; \"\") | tonumber } ] | group_by(.label) | map({ key: (.[0].label), value: [.[] ] }) | from_entries }' "
  value_template: "OK"
  json_attributes:
      - south

Returns this:

you are a hero - THANK you sooo much! :man_superhero:

one thing I recognized is that the very first line of the .csv does not get converted into the json format:

Do you also have a solution for this?

EDIT: Another different question: I now tried to build a template sensor:


template:
    - sensor:
          - name: "Forecast Watt East"
            unit_of_measurement: "W"
            device_class: power
            state_class: measurement
            unique_id: forecast_watt_east
            state: >
              {% set watts_east = state_attr('sensor.solar_forecast_east','east').watts %}
              {% set date = as_timestamp(utcnow().isoformat(),'%Y-%m-%d %H:%M:%S')| timestamp_custom('%Y-%m-%d') %}
              {% set actualtime = as_timestamp(utcnow().isoformat(),'%Y-%m-%d %H:%M:%S')| timestamp_custom('%H:%M') %}
              {% for i in range(0, watts_east | count, 1) %}
                {% if date == as_timestamp(watts_east[i].date,'%Y-%m-%d %H:%M:%S')| timestamp_custom('%Y-%m-%d') %}
                    {% if actualtime == as_timestamp(watts_east[i].date,'%Y-%m-%d %H:%M:%S')| timestamp_custom('%H:%M') %}
                        {{ watts_east[i].values}}
                    {% endif %}
                {% endif %}
              {% endfor %}

Every minute it compares the time to the time from the data and if it is the same it writes the value. If the time is not the same it does nothing. How can I manage that the value stays as sensor state between the time stamps of the data and while restarting?

Here’s a solution to that which doesn’t involve curl or jq:

  value_template: >
    {% set time_now = now().strftime('%Y-%m-%d %H:%M:%S') %}
    {% set tkey = value_json['result']['watts']
                  |sort|select('le', time_now)|list|last %}
    {{ value_json['result']['watts'][tkey] }}

Returns the watt value of the latest key that is earlier than the current time. I would paste a demonstration, but I’m not typing all that in and you only posted screenshots of the data.

thanks! this is working fine with the original json (Y)