Rest Sensor Help - json_attributes_path

I need some help with “json_attributes_path:” and “json_attributes:”.

I have a Rest sensor where I want all of the wind_speed_of_gust values from this API link as attributes. There are around 57 of the wind_speed_of_gust in the API.
https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=58.97023884691835&lon=5.723643489610628

This json_path only lists up all of the wind_speed_of_gust values, but what should I type in the “json_attributes:” field when there are no names before the values in the output?
$…timeseries…instant.details.wind_speed_of_gust

This json_path lists up everything below “details” including “wind_speed_of_gust” but as far as I understand I cant type wind_speed_of_gust[0], [1], [2], etc. Only “wind_speed_of_gust” one time, but I need all of them.
$…timeseries…instant.details

I even tried this json_path but the sensor attribute was waaay too big because it basically includes the whole API content, so HA was not happy.

json_attributes_path: "$.properties"
json_attributes:
  - timeseries

The best thing for me would be to get all the dates AND the wind_speed_of_gust values listed up.

This is my yaml code which outputs only the first wind_speed_of_gust:

  - scan_interval: 600
    resource: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=58.97023884691835&lon=5.723643489610628
    sensor:
      - name: Vindkast
        value_template: "{{ value_json.properties.timeseries[0].time }}"
        json_attributes_path: "$..timeseries..instant.details"
        json_attributes:
          - wind_speed_of_gust

I am using https://jsonpath.com/ and https://jsonviewer.stack.hu/ to test the paths.

As that value is further in the tree, you will not be able to do so this way, it would need a path to 0, 1, 2, etc. so one sensor for each.

What is the target use for this data? e.g. if you want a apexcharts graph then you could load all properies.timeseries and iterate through that in the card.
If you really only want these values then one option is to add a jq to filter them out or via command line tools.

When I tried to make a sensor with properies.timeseries as attribute, I got an error in log saying that the sensor was too big, but yes. A apexcharts graph would be perfect.

I guess this would be way to advanced for me.

Why? You’d be surprised what you can learn by needing to do something.

Back to the original question: I can’t find a simple solution. If you can cope with the first 10 values (the most you can fit in the 255-character state size limit), you could create two sensors with states for the times:

value_template: "{{ (value_json['properties']['timeseries']|map(attribute='time')|list)[:10] }}"

and the gusts:

value_template: "{{ (value_json['properties']['timeseries']|map(attribute='data.instant.details.wind_speed_of_gust')|list)[:10] }}"

I came up with a nice script to build a dictionary of time:gust items which works in the Template Editor, but can’t immediately think how you’d use it in a rest sensor. This is where the json_attributes_template that only exists in the MQTT sensor (despite several attempts to expand) would come in handy — perhaps you could find a way (probably outside of HA) to publish the JSON response to an MQTT topic and pick it up from there?

{% set value_json = PASTE_IN_HUGE_JSON_HERE_FOR_TEMPLATE_EDITOR %}
{% set tl = value_json['properties']['timeseries']|map(attribute='time')|list %}
{% set gl = value_json['properties']['timeseries']|map(attribute='data.instant.details')|list %}
{% set ns = namespace(gusts={}) %}
{% for i in range(tl|length) %}
{% if 'wind_speed_of_gust' in gl[i].keys() %}
{% set ns.gusts = dict(ns.gusts, **{tl[i]: gl[i]['wind_speed_of_gust']}) %}
{% endif %}
{% endfor %}
{{ ns.gusts }}
1 Like

jq I will skip then, this is what you could do for apex

  - platform: rest
    name: testing_fc
    scan_interval: 36000
    resource: https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=58.97023884691835&lon=5.723643489610628
    value_template: 'OK'
    json_attributes_path: $.properties
    json_attributes:      
      - timeseries

and then apex (you need to apply your own tuning)

type: custom:apexcharts-card
header:
  title: TEST
  show: true
  floating: false
  show_states: true
  colorize_states: true
span:
  start: hour
series:
  - entity: sensor.testing_fc
    name: Test wind speed
    type: area
    stroke_width: 2
    data_generator: |
      return entity.attributes.timeseries.map((entry) => {
            return [new Date(entry.time), entry.data.instant.details.wind_speed_of_gust];
          });

1 Like

I think this is perfect!
I realized that the date is not that important because there are 1 hour between every date value, so I actually only need the first date to know when the first wind speed of gust is at (Should be 2 hours before current hour, but nice to confirm this with a sensor).
And because of the 255-character state size limit would be a problem for the dates only, I just increase the [:10] on the wind sensor so I have more than enough values for my use. Thanks!!

This was exactly what I wanted actually and it worked great, but I would not need it because of your previous solution. Very interesting tho, so thanks!!

1 Like

Like I said in the original post, this solution made an error in my log saying the attributes was waay too big.

This is very nice tho! I would assume that this is possible with the sensor state @Troon made above? I have never used the apexcharts before, but looks like there are alot of possible ways to use it. Thanks!!

I just ran this on my HA dev instance, I admit to not have looked at the logs :slight_smile: and indeed it states something but I do see quite a few of them loaded in the sensor though.
Then there is the option to cut it down a bit, using jq…or ignore the error.

EDIT: it loaded data up to June 4th

Similarly, yes

Was on the road, did a quick jq…this works too

  - platform: command_line
    name: testing_fc2
    scan_interval: 30000
    command: >
         curl "https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=58.97023884691835&lon=5.723643489610628" | jq '{series: [.properties.timeseries[] | { time: .time, gust: .data.instant.details.wind_speed_of_gust }]}'
    value_template: > 
        {{ value_json.events | length }}
    json_attributes:
      - series

apex

type: custom:apexcharts-card
header:
  title: TEST
  show: true
  floating: false
  show_states: true
  colorize_states: true
span:
  start: hour
series:
  - entity: sensor.testing_fc2
    name: Test wind speed
    type: area
    stroke_width: 2
    data_generator: |
      return entity.attributes.series.map((entry) => {
            return [new Date(entry.time), entry.gust];
          });

This is even better! Almost perfect!, but is it possible to stop the list of attributes when the gust no longer has value? Half of them are like this:

- time: '2023-05-28T06:00:00Z'
  gust: null
- time: '2023-05-28T12:00:00Z'
  gust: null
- time: '2023-05-28T18:00:00Z'
  gust: null

and what should this state? “0”?

    value_template: >
        {{ value_json.events | length }}

Change the jq a bit to…

jq '{series: [.properties.timeseries[] | select(.data.instant.details.wind_speed_of_gust!=null) | { time: .time, gust: .data.instant.details.wind_speed_of_gust }]}

Side note, I am not very advanced in any of this, I mean what @troon wrote on the namespace…I would never get to this myself (only via search/copy for others)
jq is something I ‘learned’ a few months ago when I (similarly) had to cut down on a json before reading into HA. What I love about jq vs. a lot of other CLI tools is that it is quite simple and you can easily format the output to json which is then again nice for HA to load…but it has its own learning curve, true.
Not saying that you have to learn this, I only have 2 good use case, all other with jinja and quite a few with the help of @troon :slight_smile:

1 Like

Typo… just hange events to series and it will give you a count of the entries,

1 Like

Just perfect! Apex chart was nice aswell, only had to put in a 2h offset between date and gust. Thanks!

I thought it was more advanced than this tbh, so I will definitely learn more about jq in my next projects.

Thank you both for very nice help! :slight_smile:

1 Like

And JQ playground on the web is invaluable for creating templates. I have some super complex ones that completely restructure and group JSON into something more manageable.