On/off rest sensor does not work as binary sensor

I’ve searched the documentation but can see any good reason why this sensor works fine as an ordinary sensor, but throws an error when configured as a binary sensor. The state is only off/on so should work as binary sensor. Only thing I can think off is whether the xml → json conversion does not happen for binary sensor?

- platform: rest
  resource: https://www.eastsussex.gov.uk/media/18627/termdates-2021-2023.xml
  name: is_school_day
  value_template: >
    {% set today = now().replace(tzinfo=None) %}
    {% set ns = namespace(isIn=(now().weekday()>4)) %}
    {% for schoolYear in value_json.TermDates.SchoolYear -%}
      {% for holiday in schoolYear.Holiday -%}
      {% set startDate = strptime(holiday['@startDate'], '%Y-%m-%d') %}
      {% set endDate = strptime(holiday['@endDate'], '%Y-%m-%d') %}
      {% set ns.isIn = ns.isIn or (today >= startDate and today <= endDate) %}
    {%- endfor %}  
    {%- endfor %}
    {% if ns.isIn %}
    off
    {% else %}
    on
    {% endif %}
  scan_interval: 3600

When configured as binary sensor I get exception:

Template variable error: 'value_json' is undefined when rendering

Indeed. Please open an issue, and I’ll do PR to fix this inconsistency if someone else doesn’t pick it up before me.

Done - RESTful binary sensor does not covert xml to json · Issue #59833 · home-assistant/core (github.com)

Thanks for the fast response.

1 Like