SNMP returns multiple states in one sensor

I’m trying to get a fleet of Xerox printers at my work in Home Assistant so we can better monitor them. I found the OID and I’m able to retrieve the “hrPrinterDetectedErrorState”. The issue I’m having is how the data comes into the sensor.

" ```
"This object represents any error conditions detected
by the printer. The error conditions are encoded as
bits in an octet string, with the following
definitions:

        Condition        Bit #

        lowPaper              0

        noPaper              1
        lowToner              2
        noToner              3
        doorOpen              4
        jammed                5
        offline              6
        serviceRequested      7
        inputTrayMissing      8
        outputTrayMissing    9
        markerSupplyMissing  10
        outputNearFull      11
        outputFull          12
        inputTrayEmpty      13
        overduePreventMaint  14"

I’ve been beating my head against the wall all day trying to figure this out, because when SNMP pulls the data into home assistant, it gives the data as a HEX value. Soo, something like Low Paper would be 10000000, but it comes into Home Assistant as \x80. If a printer has multiple issues it may list out \x80\x20, which would be Low Paper and Low Toner. I need to be able to take the values it is pulling in, and split them up, I don’t need the \x part, but the 2 numbers are crucial to determine exactly what is wrong with a machine. In my configuration.yml file each printer has an entry like this

  • platform: snmp
    name: Language Hallway Printer Error
    host: 10.xx.xx.xx
    community: public
    baseoid: .1.3.6.1.2.1.25.3.5.1.2.1
    accept_errors: true

One printer may return \x00, another may return \x01\x04. If I could store the values like “00”, “01”, “04”, I could then use the value_template to go along with a human error code. But I’m struggling to figure out how to do that. When I put SNMP in debug mode I can see some printers send really weird info like !\x00 … Not sure what the exclamation point is for, but… Any help is appreciated

I haven’t figured out how HA deals with an SNMP octet-string, maybe it treats it as a character string (of hex digits).

I’m not sure myself how I would make use of this for human consumption but the following may be of some use with a value_template or create a new template sensor to break this up into its constituent bits (this is something I tried in the DevTools->Template):

{% set statehexstring = '\0x80\0x20' %}
{{ statehexstring | replace("\0x", "") }}  => this displays 8020

{% set hexstring =  statehexstring | replace("\0x", "")  %}
{{ hexstring | int(base=16) | bitwise_and(1) }} => displays results as 0
{{ hexstring | int(base=16) | bitwise_and(32) }}  => displays results as 32
{{ hexstring | int(base=16) | bitwise_and(32768) }} => displays result a 32768

There are probably better solutions, but this is just what I came up with playing around.