Carlo was posting in the Discord chat about wanting to make a sensor for Home Assistant that could use data from a service to determine if today was a day that a flag should be shown. He had found an online RESTful service that would spit back structured data in a JSON format and wasn’t sure how to proceed.

The service had a URL that would be ever-fresh because it could use “current_year” as a parameter http://www.webcal.fi/cal.php?id=335&format=json&start_year=current_year&end_year=current_year&tz=America%2FNew_York

Then it was time to parse the returned JSON.

sensor:
  - platform: rest
    resource: http://www.webcal.fi/cal.php?id=335&format=json&start_year=current_year&end_year=current_year&tz=America%2FNew_York
    name: Flag  Day
    value_template: >-
      {% set is_flag_day = False %}
      {%- for day_val in value_json -%}
        {% set now_string = now().strftime('%Y-%m-%d') %}
        {%- if day_val.date == now_string and day_val.flag_day == 1-%}
          {% set is_flag_day = True %}
        {%- endif -%}
      {% endfor %}
      {{is_flag_day}}

The JSON returned is an array so we tell our jinja template to loop through each day and compare a formatted string of today’s date against what’s returned from the service. If it matches and the JSON object also says it’s a flag day we return true, otherwise false.

Using my demonstration code he was able to remove a relatively large chunk of  code that had magic numbers in it and replace it with something that is way easier to follow.