REST sensor templates causing 'unknown' state: NaN values

For some reason lately, when Home Assistant first starts up, I get the below error messages in my log file 4 consecutive times ). This causes the value for the respective templates to show as NaN MB/s instead of a value such as 11.03 MB/s.

The below errors in my log happens 4 times in a row when HA first starts up:

2020-09-27 01:00:13 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup
    result_info = async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 792, in async_track_template_result
    tracker.async_setup()
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup
    self._info[template] = template.async_render_to_info(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int

The 4 templates that seem to be causing this issue:

  - platform: template
    sensors:
      horizon_c_drive_read_bytes:
        value_template: '{{ ((state_attr("sensor.diskio_0", "read_bytes") / state_attr("sensor.diskio_0", "time_since_update") | int) / 1000000) | round (2) }}'
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_c_drive_write_bytes:
        value_template: '{{ ((state_attr("sensor.diskio_0", "write_bytes") / state_attr("sensor.diskio_0", "time_since_update") | int) / 1000000) | round (2) }}'
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_e_drive_read_bytes:
        value_template: '{{ ((state_attr("sensor.diskio_1", "read_bytes") / state_attr("sensor.diskio_1", "time_since_update") | int) / 1000000) | round (2) }}'
        unit_of_measurement: 'MB/s'
  - platform: template
    sensors:
      horizon_e_drive_write_bytes:
        value_template: '{{ ((state_attr("sensor.diskio_1", "write_bytes") / state_attr("sensor.diskio_1", "time_since_update") | int) / 1000000) | round (2) }}'
        unit_of_measurement: 'MB/s'

REST sensors that the above templates depend on:

  - platform: rest
    name: diskio_0
    resource: http://192.168.1.2:61208/api/3/diskio
    value_template: '{{ ( value_json[0] ) }}'
    json_attributes:
      - time_since_update
      - disk_name
      - read_count
      - write_count
      - read_bytes
      - write_bytes
    verify_ssl: false
    timeout: 20
    scan_interval: 8
  - platform: rest
    name: diskio_1
    resource: http://192.168.1.2:61208/api/3/diskio
    value_template: '{{ ( value_json[1] ) }}'
    json_attributes:
      - time_since_update
      - disk_name
      - read_count
      - write_count
      - read_bytes
      - write_bytes
    verify_ssl: false
    timeout: 20
    scan_interval: 8

**Oddly, if I use the same exact 4 templates in Home Assistant’s template editor, the template editor displays the correct values perfectly for each entity. For example, the below template will return correctly values:

{{ ((state_attr("sensor.diskio_0", "write_bytes") / state_attr("sensor.diskio_0", "time_since_update") | int) / 1000000) | round (2) }}

will produce:
0.11

However, when the same template is used in my configuration, it will display a NaN value. The state will have a state of, “unknown”.

Hopefully, someone here has an explanation for this. I just puzzled why this started happening recently if I haven’t changed anything in my configuration. I did update Home Assistant a couple of times though. I’m using Home Assistant version 0.115.3

Thank you in advance for your help!

Feels like it cannot access the rest server at startup. I take it that you eventually get a reading?

Nick, thanks for replying. If the REST sensor fails to reach the server on startup, Home Assistant produces those weird template errors in my log.

The problem is that once Home Assistant produces those errors, it will never try again to reach the server. I have to restart Home Assistant for it to try again.

This started happening a month or two ago. I haven’t changed anything on my setup for a while now. I’ve only updated Home Assistant and respective integrations.

I also created an issue on Gitgub… since this seems to be a bug with Home Assistant, not my templates or REST server.