home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.71k stars 30.83k forks source link

Error while setting up snmp platform for sensor #93493

Closed mhzawadi closed 1 year ago

mhzawadi commented 1 year ago

The problem

I have an SNMP temperature sensory that has been working fine, but now doesnt

What version of Home Assistant Core has the issue?

core-2023.5.3

What was the last working version of Home Assistant Core?

core-2022 - maybe

What type of installation are you running?

Home Assistant OS

Integration causing the issue

snmp

Link to integration documentation on our website

https://www.home-assistant.io/integrations/snmp/

Diagnostics information

This device cannot be added from the UI

Example YAML snippet

# Example configuration.yaml entry
- platform: snmp
  host: 192.168.?.?
  baseoid: 1.3.6.1.4.1.21796.3.3.3.1.5.1
  community: ro-things
  name: "snmp telemetry01 Temp"
  unit_of_measurement: "°C"
  device_class: temperature
  state_class: measurement

Anything in the logs that might be useful for us?

Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:583
Integration: Sensor (documentation, issues)
First occurred: 21:02:02 (2 occurrences)
Last logged: 21:02:02

Error adding entities for domain sensor with platform snmp
Error while setting up snmp platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 581, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: '23.7 C'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 455, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 731, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 844, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/helpers/template_entity.py", line 411, in async_added_to_hass
    self.hass.bus.async_listen_once(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1150, in async_listen_once
    HassJob(_onetime_listener, f"onetime listen {event_type} {listener}"),
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 675, in __repr__
    return super().__repr__()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 972, in __repr__
    return f"<entity {self.entity_id}={self._stringify_state(self.available)}>"
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 591, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 583, in state
    raise ValueError(
ValueError: Sensor sensor.snmp_telemetry01_temp has device class temperature, state class measurement unit °C and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: 23.7 C (<class 'str'>)

Additional information

the endpoint is a Poseidon model 3268 monitored by nagios just fine, screenshot from nagios

image

home-assistant[bot] commented 1 year ago

snmp documentation snmp source

epenet commented 1 year ago

The problem is that the data contains already a unit 23.7 C, so the value cannot be converted to a number. I don't know much about the snmp integration, but the trailing C should be stripped by the integration.

epenet commented 1 year ago

Maybe you should use value_template to strip it: https://www.home-assistant.io/integrations/snmp#value_template

# Example configuration.yaml entry
sensor:
  - platform: snmp
    name: "Printer uptime"
    host: 192.168.2.21
    baseoid: 1.3.6.1.2.1.1.3.0
    accept_errors: true
    unit_of_measurement: "minutes"
    value_template: "{{((value | int) / 6000) | int}}"
mhzawadi commented 1 year ago

thanks for the prompt, config drafted and now need to be home to restart HA

mhzawadi commented 1 year ago

config now fixed, thanks for the help

# Poseidon model 3268
- platform: snmp
  host: 192.168.49.159
  baseoid: 1.3.6.1.4.1.21796.3.3.3.1.5.1
  community: horwood
  name: "snmp telemetry01 Temp"
  unit_of_measurement: "°C"
  device_class: temperature
  state_class: measurement
  value_template: "{{ value|replace(' C', '') | float }}"