Hi I am pulling my hair out on how I might create a template that pulls the “tempHigh” and “tempLow” values created from a json nested array that comes from the weather underground API. The entity that is created from the weatherunground integration gives me all the information i need with the values listed as attributes in a sensor called “sensor.pws_history”. The list of attributes contain values for the past 7 days including the current day.
What I am trying to pull out with a template is the ability to look for the current date within the array and then read the high and low temp value from the correct nested value in the array. I do not know how to do this.
Here is what the json file of mine looks like: (I have identified below with arrows the 2 values i wish to create their own sensors for using a template. They will always be the latest set of tempHigh and tempLow values as that will always be the most up to date set of values ie current day)
{
"summaries":[
{
"stationID":"IJIMBO62",
"tz":"Australia/Brisbane",
"obsTimeUtc":"2021-11-20T13:59:54Z",
"obsTimeLocal":"2021-11-20 23:59:54",
"epoch":1637416794,
"lat":-27.869,
"lon":153.03,
"solarRadiationHigh":975.1,
"uvHigh":9.0,
"winddirAvg":281,
"humidityHigh":78.0,
"humidityLow":30.0,
"humidityAvg":61.0,
"qcStatus":-1,
"metric":{
"tempHigh":34.3,
"tempLow":22.1,
"tempAvg":26.7,
"windspeedHigh":9.0,
"windspeedLow":0.0,
"windspeedAvg":1.8,
"windgustHigh":13.0,
"windgustLow":0.0,
"windgustAvg":2.7,
"dewptHigh":19.8,
"dewptLow":14.0,
"dewptAvg":18.0,
"windchillHigh":34.3,
"windchillLow":22.1,
"windchillAvg":26.7,
"heatindexHigh":35.6,
"heatindexLow":22.4,
"heatindexAvg":27.2,
"pressureMax":1013.82,
"pressureMin":1009.82,
"pressureTrend":0.05,
"precipRate":0.00,
"precipTotal":0.00
}
},
{
"stationID":"IJIMBO62",
"tz":"Australia/Brisbane",
"obsTimeUtc":"2021-11-21T13:59:48Z",
"obsTimeLocal":"2021-11-21 23:59:48",
"epoch":1637503188,
"lat":-27.869,
"lon":153.03,
"solarRadiationHigh":1387.7,
"uvHigh":13.0,
"winddirAvg":138,
"humidityHigh":87.0,
"humidityLow":52.0,
"humidityAvg":74.0,
"qcStatus":1,
"metric":{
"tempHigh":27.3,
"tempLow":21.0,
"tempAvg":23.3,
"windspeedHigh":8.7,
"windspeedLow":0.0,
"windspeedAvg":0.8,
"windgustHigh":9.3,
"windgustLow":0.0,
"windgustAvg":1.2,
"dewptHigh":20.5,
"dewptLow":15.5,
"dewptAvg":18.3,
"windchillHigh":27.3,
"windchillLow":21.0,
"windchillAvg":23.3,
"heatindexHigh":28.1,
"heatindexLow":21.0,
"heatindexAvg":23.7,
"pressureMax":1015.51,
"pressureMin":1010.33,
"pressureTrend":0.04,
"precipRate":3.00,
"precipTotal":0.51
}
},
{
"stationID":"IJIMBO62",
"tz":"Australia/Brisbane",
"obsTimeUtc":"2021-11-22T13:59:31Z",
"obsTimeLocal":"2021-11-22 23:59:31",
"epoch":1637589571,
"lat":-27.869,
"lon":153.03,
"solarRadiationHigh":708.8,
"uvHigh":6.0,
"winddirAvg":163,
"humidityHigh":98.0,
"humidityLow":68.0,
"humidityAvg":87.6,
"qcStatus":1,
"metric":{
"tempHigh":25.5,
"tempLow":19.6,
"tempAvg":21.4,
"windspeedHigh":9.7,
"windspeedLow":0.0,
"windspeedAvg":0.5,
"windgustHigh":13.0,
"windgustLow":0.0,
"windgustAvg":0.9,
"dewptHigh":21.6,
"dewptLow":17.7,
"dewptAvg":19.2,
"windchillHigh":25.5,
"windchillLow":19.6,
"windchillAvg":21.4,
"heatindexHigh":26.2,
"heatindexLow":19.6,
"heatindexAvg":21.7,
"pressureMax":1018.12,
"pressureMin":1012.84,
"pressureTrend":0.14,
"precipRate":7.80,
"precipTotal":10.39
}
},
{
"stationID":"IJIMBO62",
"tz":"Australia/Brisbane",
"obsTimeUtc":"2021-11-23T13:59:46Z",
"obsTimeLocal":"2021-11-23 23:59:46",
"epoch":1637675986,
"lat":-27.869,
"lon":153.03,
"solarRadiationHigh":1427.7,
"uvHigh":13.0,
"winddirAvg":112,
"humidityHigh":99.0,
"humidityLow":53.0,
"humidityAvg":83.5,
"qcStatus":0,
"metric":{
"tempHigh":28.4,
"tempLow":17.7,
"tempAvg":22.4,
"windspeedHigh":12.2,
"windspeedLow":0.0,
"windspeedAvg":1.0,
"windgustHigh":18.3,
"windgustLow":0.0,
"windgustAvg":1.6,
"dewptHigh":22.9,
"dewptLow":17.1,
"dewptAvg":19.3,
"windchillHigh":28.4,
"windchillLow":17.7,
"windchillAvg":22.4,
"heatindexHigh":30.8,
"heatindexLow":17.7,
"heatindexAvg":22.9,
"pressureMax":1019.54,
"pressureMin":1015.34,
"pressureTrend":0.02,
"precipRate":19.81,
"precipTotal":16.79
}
},
{
"stationID":"IJIMBO62",
"tz":"Australia/Brisbane",
"obsTimeUtc":"2021-11-23T22:09:52Z",
"obsTimeLocal":"2021-11-24 08:09:52",
"epoch":1637705392,
"lat":-27.869,
"lon":153.03,
"solarRadiationHigh":129.7,
"uvHigh":1.0,
"winddirAvg":173,
"humidityHigh":99.0,
"humidityLow":97.0,
"humidityAvg":98.9,
"qcStatus":1,
"metric":{
"tempHigh":21.4, <--
"tempLow":19.6, <--
"tempAvg":20.1,
"windspeedHigh":3.5,
"windspeedLow":0.0,
"windspeedAvg":0.1,
"windgustHigh":3.5,
"windgustLow":0.0,
"windgustAvg":0.2,
"dewptHigh":21.2,
"dewptLow":19.4,
"dewptAvg":19.9,
"windchillHigh":21.4,
"windchillLow":19.6,
"windchillAvg":20.1,
"heatindexHigh":22.2,
"heatindexLow":19.6,
"heatindexAvg":20.2,
"pressureMax":1018.83,
"pressureMin":1016.73,
"pressureTrend":0.06,
"precipRate":43.21,
"precipTotal":30.99
}
}
]
}
I have created the following template which whilst does pull the values it hardcodes the nested postion of the values I am looking for. As new days of data roll through the position of the current day of data changes. As this is a new integration I can see that once i have 7 days of data then perhaps i can just set the nested positon to 6 which should then always give me the current day of data as the API is a rolling 7 day list of data.
However I would love to know for now and perhaps future setups how can I dynamically look for the current day within the array using the “obsTimeLocal” and then pull the matching tempHigh and tempLow values that are within the same nested series of values that exisit for the current date.
Here is what i have so far as my template:
- platform: template
sensors:
pws_temp_high:
value_template: '{{ states.sensor.pws_history.attributes.summaries[4].metric.tempHigh }}'
friendly_name: Temp High
unit_of_measurement: '°C'
pws_temp_low:
value_template: '{{ states.sensor.pws_history.attributes.summaries[4].metric.tempLow }}'
friendly_name: Temp Low
unit_of_measurement: '°C'
Any suggestions?