If will fail because the for-loop will attempt to access a list item that doesn’t exist.
You can use the count
or length
filter to get the number of items in a list.
value_json.result.records | count
If will fail because the for-loop will attempt to access a list item that doesn’t exist.
You can use the count
or length
filter to get the number of items in a list.
value_json.result.records | count
Thanks again !
EDIT: updated
rest:
- authentication: basic
scan_interval: 3600
verify_ssl: false
headers:
Content-Type: application/json
User-Agent: Home Assistant
Accept-Encoding: identity
resource: "https://www.data.brisbane.qld.gov.au/data/api/3/action/datastore_search?resource_id=c6dbb0b3-1e00-4bb8-8776-aa1b8f1ecfaa"
sensor:
- name: GarbageThisWeek
value_template: >
{% set y = value_json.result.records | count %}
{% for x in range(0,y) %}
{% set days = (( as_timestamp(strptime(value_json["result"]["records"][x]["WEEK_STARTING"], '%d/%m/%Y')) - as_timestamp(now()) ) / 86400 ) | round() %}
{% if days >= 0 and days < 7 %}
{{ value_json["result"]["records"][x]["WEEK_STARTING"]}} - {{value_json["result"]["records"][x]["ZONE"]}}
{% endif %}
{% endfor %}
- name: GarbageNextWeek
value_template: >
{% set y = value_json.result.records | count %}
{% for x in range(0,y) %}
{% set days = (( as_timestamp(strptime(value_json["result"]["records"][x]["WEEK_STARTING"], '%d/%m/%Y')) - as_timestamp(now()) ) / 86400 ) | round() %}
{% if days >= 7 and days < 14 %}
{{ value_json["result"]["records"][x]["WEEK_STARTING"]}} - {{value_json["result"]["records"][x]["ZONE"]}}
{% endif %}
{% endfor %}
Thanks @vingerha and @123… busy at work this week, so will do some testing on the weekend. Appreciate all the effort and assistance!
If you’re interested, you can reduce the template to this:
rest:
- authentication: basic
scan_interval: 3600
verify_ssl: false
headers:
Content-Type: application/json
User-Agent: Home Assistant
Accept-Encoding: identity
resource: "https://www.data.brisbane.qld.gov.au/data/api/3/action/datastore_search?resource_id=c6dbb0b3-1e00-4bb8-8776-aa1b8f1ecfaa"
sensor:
- name: GarbageThisWeek
value_template: >
{% for x in value_json.result.records if 0 <= (strptime(x.WEEK_STARTING, '%d/%m/%Y')|as_local - now()).days < 7 %}
{{ x.WEEK_STARTING }} - {{x.ZONE}}
{% endfor %}
- name: GarbageNextWeek
value_template: >
{% for x in value_json.result.records if 7 <= (strptime(x.WEEK_STARTING, '%d/%m/%Y')|as_local - now()).days < 14 %}
{{ x.WEEK_STARTING }} - {{x.ZONE}}
{% endfor %}
Thanks… so this will not crash past the 53rd block? Then this is another way to run through all without needing to know the ‘size’, true?
The one thing seemingly missing is the > 7 and <14 etc. as the result can be negative with historical data in the file…but maybe I donot get the jinja well enough
It effectively does a “for each item in the list”, whatever the list’s length may be, so it can’t run past the end of the list.
If you mean the day range test for GarbageNextWeek, it’s part of the for-loop
’s if
statement.
The if
is used to reduce the number of items to be iterated by the for-loop
. For example, if I have a list of used cars and I only want to iterate through the ones that are between 2 and 5 years old inclusively:
{% for car in used_cars if 2 <= now().year - car.year <= 5 %}
{{ car.year }} {{ car.model }}
{% endfor %}
Thanks for everyone’s help on this… started getting to mostly what I wanted, but then found “Waste Collection Schedule” on HACS so have switched to that. Appreciate all the input tho… will likely com in handy in other stuff!