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.
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.
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:
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 }}
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!!
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 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.
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
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.