APC UPS, how to monitor a 64 bit flag OID with a custom sensor

Hi All, I’m interested in creating a custom sensor for my APC UPS, more specific:

- the snmp OID is  1.3.6.1.4.1.318.1.1.1.11.1.1.0 and the value is represented as follows: "0001010000000000001000000000000000000000000000000000000000000000"

in a nutshell, each bit represents an event as follows:
Flag 1: Abnormal Condition Present
Flag 2: On Battery
Flag 3: Low Battery
Flag 4: On Line
Flag 5: Replace Battery
… and so on

I am interested in creating a sensor as this one:

  - platform: snmp
    name: ups_smart_status
    host: SNMP_IP
    baseoid: 1.3.6.1.4.1.318.1.1.1.4.1.1.0
    accept_errors: true
    value_template: >-
      {%if value == '1' %}
        Unknown
      {% elif value == '2' %}
        On Line
      {% elif value == '3' %}
        On Battery
      {% elif value == '4' %}
        On Smart Boost
      {% elif value == '5' %}
        Timed Sleeping
      {% elif value == '6' %}
        Software Bypass
      {% elif value == '7' %}
        Off
      {% elif value == '8' %}
        Rebooting
      {% elif value == '9' %}
        Switched Bypass
      {% elif value == '10' %}
        Hardware Failure Bypass
      {% elif value == '11' %}
        Sleeping Until Power Returns
      {% elif value == '12' %}
        On Smart Trim
      {% endif %}

the only problem being the “value”, witch in my case is not an integer number.

Any ideeas that could point me in the right direction ?

Many thanks, Gigel

You need multiple binary sensors, one for each flag, as multiple flags can be set at once. Make your main sensor the binary response in string form from the UPS (as above but without the value_template lines), then create template binary sensor helpers with state templates like this:

{{ states('sensor.ups_smart_status')[0] == '1' }}
{{ states('sensor.ups_smart_status')[1] == '1' }}

and so on. Note that [0] is the left-most character in the string, the most-significant bit of the value.

You can choose appropriate device classes too, to get icons and state descriptions. Here, I’ve chosen problem for the first flag:

1 Like

thank you for your quick reply and sorry for the code misshap (this is my first post).

Ok, so I have a question about the position of the flag. Did I understand this corect: the [0] in the statement represents the position (of the 64 bits) ?

thanks again !

1 Like

Yes. [0] is the left-most bit (Flag 1); [63] would be the right-most bit (Flag 64).

1 Like

Sweet ! I don’t have access yet to the UPS, but I can’t wait to try this in a couple of days. Thanks again for helping a noob ! :))

1 Like