rest:
resource: https://api.ipma.pt/open-data/observation/meteorology/stations/observations.json
scan_interval: 3600
sensor:
- name: "IPMA precipitacao acumulada"
json_attributes_path: $..1210604 #I used "https://jsonpath.com/" to extract the "location" I want. There are always 24 entries
value_template: "{{ state_attr('sensor.ipma_precAcumulada', 'value_json[2].precAcumulada') }}"
json_attributes:
- "precAcumulada"
- name: "IPMA humidade acumulada"
json_attributes_path: $..1210604
value_template: "{{ state_attr('sensor.ipma_humidade', 'humidade') }}" #for test, tryed other field but here show the first element of 24
json_attributes:
- "humidade"
For now I’m just trying to extract an element but what I really want is to extract the 24 values from this “precCumulative” field and calculate their average.
In the tests I did in the developers tools, it works without problems but in the state of the entity it is unknown.
The issue is that your json path is not unique as this station occurs (as you state) 24 times
IPMA have chosen a different setup then I have seen before, instead of grouping FIRST by station and then by hour they did this vv, which makes it quite a challenge as I assume you want to have both the hour as well as the value returned. Can I ask what the goal is?
Clear but in this case there is no relation between the time of day and the value.
Meaning one has to hardcode (?) the sensor per time of day
Assuming (?) this is a rolling forecast, I (but that is me) would like to have all of them as attributes in one sensor for this particular station. Possibly this is more a cli thing with jq but this is far from my experience
No problem. To do what you need, you might want to try modifying my code to give a sensor state that is a list of values effectively refactoring the original JSON into a more helpful structure, then using another sensor to read that into attributes. I’m not sure it’s feasible to do all of that in one step.
If you do that, watch out for the 256-character limit on sensor state.
Off topic question… how would you go about to extract all forecasts for a specific station in this case? Not asking for a solution but what tech/method would this require?
Personally, I’d use AppDaemon, because I already have it set up. It basically allows you to use Python scripting of arbitrary complexity to create whatever sensor structure you want.
Thanks @vingerha
What I want is for an automation to run at a certain time of the day (eg 11pm) that consults this API and tells me the averages, for the last 24 hours, of the “precAcumulada” field for a given station, in this case 1210604. I don’t need dates because the API records the weather values, in this case the accumulated precipitation, every hour.
Hello @Troon
Strange but for me it’s not working. The value_template is “old school” but that’s the logic. Something is not going well because the state is always the one in the last “else”.
You have deviated the structure from what Troon suggested…the value_template should be in the rest sensor(s), check the doc below on the structure to follow
Yes, you’re right!
It makes all the difference and it works. I will adapt the remaining cases to this way.
I was using these examples (RESTful Sensor - Home Assistant) but at the beginning of that page there is a tip essential for anyone wanting to create multiple sensors “Tip: If you want to create multiple sensors using the same endpoint, use the RESTful configuration instructions.”