Marco Lurati

SNMP control via Home Assistant

- file://49OuIR5IhIhupSn3

Introduction

SNMP (Simple Network Management Protocol) is an internet protocol for collecting and organizing information about managed devices on an IP network and modifying that information to change device behavior.

The SNMP protocol uses a management information base (MIB) to describe the variables about the devices' status and configurations. Those variables can be remotely queried (read) and manipulated.

SNMP has three versions: v1 is the original one, and v2 and v3 are more recent versions that improve performance, flexibility, and security. Version v3 supports the encryption of data between the device and the manager.

SNMP messages are transported via UDP, and all the SNMP devices (agents) receive requests on UPD port 161. The manager receives notification on port 162 (Traps and InfromRequests).

SNMP MIB and OID

SNMP doesn't have a defined structure, because every device has different functionalities. Every device (agent) has its own Management Information Base (MIB) that contains the hierarchical structure of the object identifiers (OID). Each OID identifies a variable that can be read or set via SNMP.

The OID is identified hierarchically with indexes, something like ".1.3.6.1.4.1.13742.6.4.1.2.1.2.1.1".

SNMP protocol

The commands are the following:

SNMPv1, v2 and v3: GetRequest, SetRequest, GetNextRequest, Response, Trap

SNMP v2, v3: GetBulkRequest, InformRequest

SNMP v3: Report

Reading the MIB with iReasoning Browser

From the device (agent) it should be possible to download its own MIB file (or search for it online). The MIB file can be read by the free software iReasoning Browser to determine the object identifiers OID to be able to control and read the device.

In the iReasoning Browser click on File > Load MIB. Go through the structure to discover di different functionalities and the associated OID. You can "right-click > Table view" on the element to have a better overview.

By clicking on the elements of the table, you can see the OID of that object and you can also write it (if it is a writable object), by clicking on "SNMP SET" to see if the device responds correctly.

Controlling the SNMP device via Home Assistant

In Home Assistant is possible to read data and control the device by setting the SNMP instruction in the 'configuration.yaml' file. The setup is the following:

# Home Assistant configuration.yaml

switch:
  - platform: snmp
    name: PDU Outlet 01
    host: 192.168.10.21
    community: private
    baseoid: .1.3.6.1.4.1.13742.6.4.1.2.1.2.1.1
    payload_on: 1
    payload_off: 0
  - platform: snmp
    name: PDU Outlet 02
    host: 192.168.10.21
    community: private
    baseoid: .1.3.6.1.4.1.13742.6.4.1.2.1.2.1.2
    payload_on: 1
    payload_off: 0
  - platform: snmp
    name: PDU Outlet 03
    host: 192.168.10.21
    community: private
    baseoid: .1.3.6.1.4.1.13742.6.4.1.2.1.2.1.3
    payload_on: 1
    payload_off: 0

sensor: 
  - platform: snmp
    name: PDU Inlet Power
    host: 192.168.10.21
    community: public
    baseoid: .1.3.6.1.4.1.13742.6.5.2.3.1.4.1.1.5
    unit_of_measurement: W
    value_template: "{{(value | int)}}"
  - platform: snmp
    name: PDU Power 11 (NAS)
    host: 192.168.10.21
    community: public
    baseoid: .1.3.6.1.4.1.13742.6.5.4.3.1.4.1.11.5
    unit_of_measurement: W
    value_template: "{{(value | int)}}"