SNMP Odd results

This was working fine until I updated to the latest HA. Had been holding off for a while due to incompatibilities with my code and deprecated functionality. I have some SNMP calls and it’s erroring out. So if I run an snmpget of the command I get:

iso.3.6.1.2.1.25.3.5.1.2.1 = Hex-STRING: 00

This is my sensor around this:

- platform: snmp
  name: Canon MF8580CDW Status
  host: <IP>
  baseoid: 1.3.6.1.2.1.25.3.5.1.2.1
  accept_errors: true
  scan_interval: 1000
  value_template: >-
    {% if value|int(base=16) is equalto 0 %}
      OK
    {% else %}
      Failed
    {% endif %}

Below is the error I’m getting in the logs now:

2024-11-10 16:38:40.377 ERROR (MainThread) [homeassistant.components.sensor] Error adding entity sensor.canon_mf8580cdw_status for domain sensor with platform snmp
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 599, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 918, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1367, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/snmp/sensor.py", line 203, in async_added_to_hass
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/snmp/sensor.py", line 214, in async_update
    value = self._value_template.async_render_with_possible_json_value(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 806, in async_render_with_possible_json_value
    render_result = _render_with_context(
                    ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2735, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2373, in forgiving_int_filter
    raise_no_default("int", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1911, in raise_no_default
    raise ValueError(
ValueError: Template error: int got invalid input '^@' when rendering template '{% if value|int(base=16) is equalto 0 %}
  OK
{% else %}
  Failed
{% endif %}' but no default was specified

So if I’m reading right it’s saying the resulting value it gets is ‘^@’ which isn’t what I get when querying it myself on the HA box.

Thoughts?

You did not query it at the same time, so your results are not the same.
The issue is that your conversion fails, because your input is not as you expect.
You expect to get a result like “70” and convert it to 112, but you get a “p” instead, because HA interpret the result different and “p” is the ACSII letter for hex 70/decimal 112.

I can’t say I know what’s wrong, but if you say this use to work, and after the upgrade you are now getting this error, then maybe a bug???

This reference says a ^@ can be interpreted as a null char whose hex value is 0. So it seems the code HA is using (either SNMP or templating code) is interpreting the returned value as a string that only consists of a null terminating character instead of a hex value.

Are you saying I didn’t query it at the same time so that’s why it’s different? That starting HA and then running snmpget a minute later could return different results for an idle printer status? Want to make sure I’m reading right. Also this did work as expected before the upgrade so not sure where the issue or change is coming from.

That was not really the important part.
The important part is that you do not get the input you expect, IE. a hexadecimal number.
You probably get a string with the ASCII characters or similar.