Open Meteo data from json array

Hi,

I think i’ve looked through so many examples to figure out how to get Open Meteo’s soil moisture data to display on Plotly Graph but just can’t seem to figure out how to input the array of data for each measurement.

I’ve managed to get the current weather working, but the forecast or history seems to be difficult to work out being in an array.

Open Meteo API call for data used

{
  "latitude": -33.75,
  "longitude": 151.125,
  "generationtime_ms": 0.0450611114501953,
  "utc_offset_seconds": 0,
  "timezone": "GMT",
  "timezone_abbreviation": "GMT",
  "elevation": 51,
  "hourly_units": {
    "time": "iso8601",
    "temperature_2m": "°C",
    "precipitation": "mm",
    "soil_moisture_0_to_1cm": "m³/m³"
  },
  "hourly": {
    "time": [
      "2024-06-22T00:00",
      "2024-06-22T01:00",
#..... Multiple lines of time stamp removed .....
      "2024-06-25T22:00",
      "2024-06-25T23:00"
    ],
    "temperature_2m": [11, 13.4, 14, 14.2, 14.2, 13.9, 13.6, 12.9, 12.2, 11.7, 11.4, 11.1, 11, 10.9, 10.7, 10.7, 10.6, 10.5, 10.1, 10, 9.8, 9.7, 9.8, 10.5, 11.8, 13.3, 14.7, 15.2, 15.5, 15.3, 14.7, 13.3, 12.1, 11.5, 11, 10.6, 10.2, 9.6, 8.8, 8.4, 7, 7, 6.8, 6.7, 6.7, 6.7, 6.9, 8.2, 10.2, 12.5, 14.3, 15.6, 16.1, 16, 15.4, 13.9, 12.4, 11.5, 10.5, 9.4, 8.6, 8, 7.6, 7.4, 7.6, 7.6, 7.8, 7.6, 7.3, 7.3, 7.7, 8.9, 12.1, 15, 17, 17.8, 17.6, 17.3, 16.5, 14.8, 13.4, 13.3, 13.2, 13.1, 12.7, 12.2, 11.5, 10.8, 10.1, 9.7, 9.3, 9, 8.8, 8.6, 9, 11.7],
    "precipitation": [0, 0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0.3, 0.2, 0, 0, 0, 0, 0, 0, 0.2, 0.3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    "soil_moisture_0_to_1cm": [0.264, 0.26, 0.256, 0.254, 0.252, 0.251, 0.253, 0.252, 0.252, 0.253, 0.255, 0.264, 0.269, 0.279, 0.282, 0.276, 0.272, 0.27, 0.269, 0.268, 0.267, 0.275, 0.283, 0.278, 0.276, 0.275, 0.27, 0.266, 0.262, 0.26, 0.259, 0.259, 0.261, 0.262, 0.262, 0.263, 0.263, 0.263, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.264, 0.262, 0.26, 0.256, 0.253, 0.25, 0.249, 0.249, 0.251, 0.253, 0.254, 0.255, 0.256, 0.256, 0.257, 0.257, 0.258, 0.258, 0.259, 0.259, 0.259, 0.259, 0.259, 0.259, 0.259, 0.257, 0.254, 0.249, 0.244, 0.24, 0.237, 0.237, 0.239, 0.241, 0.243, 0.244, 0.245, 0.246, 0.246, 0.247, 0.248, 0.249, 0.25, 0.25, 0.251, 0.251, 0.252, 0.252, 0.251]
  }
}

My configuration.yaml which shows a value of 0 on each item at least on the graph.

rest:
    scan_interval: 60
    resource: "https://api.open-meteo.com/v1/forecast?latitude=-33.8678&longitude=151.2073&hourly=temperature_2m,precipitation,soil_moisture_0_to_1cm&past_days=1&forecast_days=3"
    sensor:
        - name: omhtime
          value_template: "{{ value_json.hourly.time[] }}"
        - name: omhtemp
          value_template: "{{ value_json.hourly.temperature_2m[] }}"
        - name: omhprecipitation
          value_template: "{{ value_json.hourly.precipitation[] }}"
        - name: omhsoilmoisture01
          value_template: "{{ value_json.hourly.soil_moisture_0_to_1cm[] }}"

I’ve seen some examples of looping through data in array, but can’t quite seem to get any of them to work.

I’m hoping to use this data to get a general idea of soil moisture readings over time and precipitation to turn on sprinklers accordingly. Saves me getting my own weather sensors at home for the time being. Open Meteo have a good range of measurements that i’d like to be able to display / use that aren’t in the custom card.

Thanks in advance.

A graph will not help you automate things but if you want a graph then you could achieve this by getting the hourly dict as an attribute and use Apexcharts.
If you want to automate things, then how do you want to trigger?

EDIT; here is an example of how it could work with Apex using multiple attributes

RomRider/apexcharts-card: :chart_with_upwards_trend: A Lovelace card to display advanced graphs and charts based on ApexChartsJS for Home Assistant (github.com)

It’s not so much the graph I’m concerned about, it’s getting the data from json array. I’ll check this out though. Thank you.

With jinja/template you can iterate over the arrays but then ‘what’ do you want to get out. e.g. you can get the peek precipitation moment, the total precipitation over a period, the min soil_moisture etc.etc.

I understand why you want to use the hourly forecast. Because the soil moisture is not available in the current weather data. For automation, you probably only need a sensor for the following hour, which is updated every hour:

rest:
  - resource_template: https://api.open-meteo.com/v1/forecast?latitude={{ state_attr("zone.home","latitude") }}&longitude={{ state_attr("zone.home","longitude") }}&elevation=94&timezone=auto&past_hours=0&forecast_hours=1&hourly=temperature_2m,relative_humidity_2m,dew_point_2m,surface_pressure,cloud_cover,wind_speed_10m,precipitation,soil_moisture_0_to_1cm,soil_moisture_1_to_3cm,soil_moisture_3_to_9cm,soil_moisture_9_to_27cm,soil_moisture_27_to_81cm
    scan_interval: 3600
    sensor:
      - name: openmeteo_hourly_forecast_soil_moisture
        unique_id: sensor.openmeteo_hourly_forecast_soil_moisture
        value_template: "{{ value_json.hourly.soil_moisture_1_to_3cm[0] }}"
        force_update: true

The right template:

{{ states("sensor.openmeteo_hourly_forecast_soil_moisture") | float }}

Then you create a sensor with which you can create your automation:

template:
  - sensor:
    - name: openmeteo_soil_moisture
      unique_id: sensor.openmeteo_soil_moisture
      unit_of_measurement: 'm³/m³'
      state: '{{ states("sensor.openmeteo_hourly_forecast_soil_moisture") | float }}'