Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNMP sensor and OPAQUE data type #112392

Closed
Al-Dek opened this issue Mar 5, 2024 · 5 comments · Fixed by #113624
Closed

SNMP sensor and OPAQUE data type #112392

Al-Dek opened this issue Mar 5, 2024 · 5 comments · Fixed by #113624

Comments

@Al-Dek
Copy link

Al-Dek commented Mar 5, 2024

The problem

SNMP sensor not processing OPAQUE values.
SNMP sensors based on OID with "opaque" data type does not converted into float.

What version of Home Assistant Core has the issue?

Core 2024.2.5

What was the last working version of Home Assistant Core?

Core 2024.2.5

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

SNMP sensor not processing OPAQUE values.
I have the same problem as was described in Issue 2767 and apparently was solved 7 years ago.
But nowadays I have the same problem - all values for SNMP sensors based on OID with "opaque" data type does not converted into float.
All other ( 20+) snmp sensors are working excellent.

I'm trying to get snmp data from NAS Synology DS with connected APC UPS.

Snmpwalk:
OID Object Type Value
1.3.6.1.4.1.6574.4.4.1.4.0 upsInputVoltageNominal OPAQUE Ÿx�Cf

Sensor configuration

  • platform: snmp
    name: "upsInputVoltageNominal"
    host: 192.168.2.6
    baseoid: 1.3.6.1.4.1.6574.4.4.1.4.0
    version: 2c
    community: "xxxxxxx"
    accept_errors: true
    unique_id: ups_input_voltage_nominal
    ===================================

Result value ='0x9f780443660000'
image

If other parameters for sensor will be defined such as device_class, state_class and unit_of_measurement we get a sensor creation error.

  • platform: snmp
    name: "upsInputVoltageNominal"
    host: 192.168.2.6
    baseoid: 1.3.6.1.4.1.6574.4.4.1.4.0
    version: 2c
    community: "xxxxxxx"
    accept_errors: true
    unique_id: ups_input_voltage_nominal
    device_class: voltage
    state_class: measurement
    unit_of_measurement: V

image

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Error for sensor with device_class, UoM, state_class

ERROR:
Logger: homeassistant
Source: components/sensor/__init__.py:671
First occurred: 17:00:44 (1 occurrences)
Last logged: 17:00:44

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 664, in state
    numerical_value = int(value)
                      ^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '0x9f780443660000'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 667, in state
    numerical_value = float(value)
                      ^^^^^^^^^^^^
ValueError: could not convert string to float: '0x9f780443660000'
The above exception was the direct cause of the following exception:
raceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 533, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 782, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1324, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/snmp/sensor.py", line 206, in async_added_to_hass
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/snmp/sensor.py", line 222, in async_update
    self._process_manual_data(raw_value)
  File "/usr/src/homeassistant/homeassistant/helpers/trigger_template_entity.py", line 238, in _process_manual_data
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 989, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1110, in _async_write_ha_state
    state, attr, capabilities, shadowed_attr = self.__async_calculate_state()
                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1047, in __async_calculate_state
    state = self._stringify_state(available)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 995, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 671, in state
    raise ValueError(
ValueError: Sensor sensor.upsinputvoltagenominal has device class 'voltage', state class 'measurement' unit 'V' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '0x9f780443660000' (<class 'str'>)
================================================

Additional information

No response

@home-assistant
Copy link

home-assistant bot commented Mar 5, 2024

@lextm
Copy link
Contributor

lextm commented Mar 16, 2024

The proposed changes from initial discussion you linked to weren't made into the HA code base. That's why you hit the same.

It's the same issue and my team is investigating to see how to fix it.

@lextm
Copy link
Contributor

lextm commented Mar 16, 2024

The work done by @ChristianKuehnel in #11239 remains helpful. We have cherry picked them and tested out in https://github.com/lextm/core/tree/better-sensor with updated test cases.

We will do more testing before sending a pull request.

@Al-Dek
Copy link
Author

Al-Dek commented Mar 16, 2024

@lextm Thank you for the information on this matter.
Currently for OPAQUE I just use snmp sensor with the "trick" :
#------------ DSHV UPS Input voltage --------------------

  • platform: snmp
    name: "dshv_ups_inp_voltage"
    host: 192.168.2.6
    baseoid: 1.3.6.1.4.1.6574.4.4.1.1.0
    version: 2c
    community: "xxxxx"
    accept_errors: true
    scan_interval: 60
    unique_id: dshv_ups_inp_voltage
    device_class: voltage
    unit_of_measurement: V
    value_template: '{{unpack(pack(value[8:16] | int(base=16), "<I"), "f") | float(0) | round(0)}}'
    =================================

@lextm
Copy link
Contributor

lextm commented Mar 16, 2024

Sure. That seems to be a good workaround.

The linked PR has been accepted, so you won't need this workaround once the next HA release is out.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants