homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

I am getting the below exception on all similar sensors when first starting up Home Assistant:

homeassistant.exceptions.TemplateError: TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
2021 - 02 - 07 21:19:00 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'') while processing template 'Template("{{ (state_attr('sensor.tautulli','lan_bandwidth') / 1000) | round(2) }}")' for attribute '_state' in entity 'sensor.tautulli_lan_bandwidth'
2021 - 02 - 07 21:19:00 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ (state_attr('sensor.tautulli','total_bandwidth') / 1000) | round(2) }}")

Code:

tautulli_lan_bandwidth:
  friendly_name: 'LAN bandwidth'
  value_template: "{{ (state_attr('sensor.tautulli','lan_bandwidth') / 1000) | round(2) }}"
  unit_of_measurement: "Mbps"

Below, is my best attempt to avoid the exception. However, it doesn’t avoid the error.

      tautulli_lan_bandwidth:
        friendly_name: 'LAN bandwidth'
        {% if (lan_bandwidth != None ) %}
            value_template: "{{ (state_attr('sensor.tautulli','lan_bandwidth') / 1000) | round(2) }}"
        {% endif %}
        availability_template: >-
                {{ states("sensor.tautulli") not in ["unknown", "unavailable"] }}
        unit_of_measurement: "Mbps"

Assuming your sensor is good some time after the restart, your attribute lan_bandwidth seems to be a number and normally would not require the | float or | int filter to convert it from a string to a number, but on start up it is likely to be none, which can not be divided by 1000. Using the | float or | int filters will convert strings to 0 and stop the error occurring.

tautulli_lan_bandwidth:
  friendly_name: 'LAN bandwidth'
  value_template: "{{ (state_attr('sensor.tautulli','lan_bandwidth') | float / 1000) | round(2) }}"
  unit_of_measurement: "Mbps"

However this may put zeros in your data every restart so a better option might be to add an availability template instead:

tautulli_lan_bandwidth:
  friendly_name: 'LAN bandwidth'
  value_template: "{{ (state_attr('sensor.tautulli','lan_bandwidth') / 1000) | round(2) }}"
  unit_of_measurement: "Mbps"
  availability_template: "{{ state_attr('sensor.tautulli','lan_bandwidth') is number }}"

or (you nearly had it but missed the ‘none’ case):

        availability_template: >-
                {{ states("sensor.tautulli") not in ["unknown", "unavailable", "none"] }}

EDIT: go with the second option. There seems to be something wrong with the is number test:

EDIT2:

Actually it’s the template editor that has the issue. is number works when the attribute is actually a number:

So you can use that test if you find it easier/cleaner.

I’ve opened an issue regarding the template editor.

2 Likes

Thank you. I want to be sure I understood correctly. My best option currently is to use the below code?

tautulli_lan_bandwidth:
  friendly_name: 'LAN bandwidth'
  value_template: "{{ (state_attr('sensor.tautulli','lan_bandwidth') / 1000) | round(2) }}"
  unit_of_measurement: "Mbps"
  availability_template: >-
    {{ states("sensor.tautulli") not in ["unknown", "unavailable", "none"] }}

You can use either solution.

1 Like