I’m using the REST integration to pull in some weather data from the MyRadar API. So far I’m able to populate several sensors with point data.
The snag that I’m hitting is getting an array (hourly or minutely) forecast data into a single sensor. It is possible to capture hourly.data[].time and hourly.data[].temperature arrays into a single sensor directly in the rest call?
Edit: I’m trying to pull in these arrays as sensor attributes
I don’t think I understand how exactly this sensor’s state would look like when accumulating lots of forecast data and how you would then display this data, but the major hurdle is the limit of 255 characters for a sensor state. If you think you can squeeze the data into this character limit then you could iterate through the array in the value_template.
Another option is to create multiple sensors - each one for a specific future point in time (assuming that the API returns suitable data for this approach).
Sorry, I should have been more clear. I’m looking to pull the time and temperature array as sensor attributes where the 255 limit doesn’t apply. I’m trying to avoid creating 42 individual sensors.
Something along the lines of:
rest:
sensor:
- name: "myradar_forcast"
value_template: "OK"
json_attributes_path: "$.hourly.data[]"
json_attributes:
- time
- temperature
Can I do something more complex in the value template to iterate through the json? (This is probably gibberish but I know about as much python as I do jinja.)
sensor:
- name: "myradar_forecast"
value_template: >-
{% for index in $.hourly.data[] %}
{{ states.sensor.myradar_forecast.attributes['time'][index] = $.hourly.data[index].time }}
{{ states.sensor.myradar_forecast.attributes['temperature'][index] = $.hourly.data[index].temperature }}
{% endfor %}
I thought you can set variables in jinja? I’m under the impression arbitrary python is possible between the {% %}
After a bit more reading I’ve gotten to this:
value_template: >-
{% for datapoint in $.hourly.data %}
{% set states.sensor.myradar_forecast.attributes['time'][loop.index0] = value_json['hourly']['data'][loop.index0]['time'] %}
{% set states.sensor.myradar_forecast.attributes['temperature'][loop.index0] = value_json['hourly']['data'][loop.index0]['temperature'] %}
{% endfor %}
But I’m getting invalid template (TemplateSyntaxError: unexpected char '$' at 20). So I’m not even making it inside of the loop. I’m not sure what’s wrong with that for statement though.
Inside the value_template you can access data using the value_json, i.e. value_json.hourly.data should work. Now sure if the two lines inside the loop work this way though.