Monitoring Your Network Devices Using SNMP in Home Assistant

by Pete
Published: Updated: 9 minutes read

In the digital age, network performance is paramount. Discover how you can supercharge your home network monitoring by seamlessly integrating SNMP (Simple Network Management Protocol) with Home Assistant. This article guides you through the process of harnessing SNMP’s power to fetch crucial network statistics from your devices, providing you with real-time insights to ensure smooth operations.

What is SNMP?

In the realm of network monitoring, SNMP emerges as a fundamental protocol that plays a pivotal role in the efficient management of network devices. As networks grow in complexity, understanding why SNMP matters becomes essential for optimizing performance and maintaining seamless operations.

SNMP, or Simple Network Management Protocol, offers a standardized approach to gathering and organizing information from a diverse range of network devices. From routers and switches to servers and printers, SNMP acts as a universal communicator that extracts essential data from these devices.

Configuring Home Assistant to poll a host

To monitor an SNMP-enabled device in Home Assistant, you need to configure SNMP settings in the configuration.yaml file. Follow these steps to set up SNMP monitoring:

1
Access Configuration.yaml

Open your preferred text editor to access the configuration.yaml file of your Home Assistant setup.

2
Add SNMP Configuration

Under the snmp key, add the configuration for the SNMP device you want to monitor. Here’s an example configuration:

## SNMP 
  - platform: snmp
    name: "internet-inbound_counter"
    host: 192.168.1.1
    baseoid: .1.3.6.1.2.1.2.2.1.10.3
    accept_errors: true
    community: your_community_string
    version: 2c
    unit_of_measurement: "bps"
    scan_interval: 15
    
  - platform: snmp
    name: "internet-outbound_counter"
    host: 192.168.1.1
    baseoid: .1.3.6.1.2.1.2.2.1.16.3
    accept_errors: true
    community: your_community_string
    version: 2c
    unit_of_measurement: "bps"
    scan_interval: 15

In this configuration, the SNMP platform is used to create two SNMP sensors:

  1. internet-inbound_counter: This sensor monitors inbound network traffic. It fetches data using SNMP OID .1.3.6.1.2.1.2.2.1.10.3, which represents the inbound network traffic counter. The sensor will update its data every 15 seconds and measure the traffic in bits per second (bps).
  2. internet-outbound_counter: This sensor monitors outbound network traffic. It uses SNMP OID .1.3.6.1.2.1.2.2.1.16.3, representing the outbound network traffic counter. Like the previous sensor, it updates every 15 seconds and measures traffic in bps.

3
Save and Restart

  1. Save the changes to the configuration.yaml file.
  2. Restart Home Assistant to apply the new SNMP configuration.

Using an Automation to convert values

In my example, the default SNMP lookup will return bps (or bits per second). What happens if we want Megabits per second? We can use an automation to help us convert the values returned from the sensos from bps to Mbps.

The provided YAML code represents an automation in Home Assistant that calculates and updates network bandwidth metrics using the SNMP sensor data you’ve configured. Here’s a breakdown of the automation:

alias: Internet Bandwidth
description: ""
trigger:
  - platform: time_pattern
    seconds: /15
condition: []
action:
  - parallel:
      - service: input_number.set_value
        entity_id: input_number.inbound_memory
        data_template:
          value: >-
            {{ states('sensor.internet-inbound_counter') | float | round(2) }}
      - service: input_number.set_value
        entity_id: input_number.outbound_memory
        data_template:
          value: >-
            {{ states('sensor.internet-outbound_counter') | float | round(2) }}
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
      - service: input_number.set_value
        entity_id: input_number.inbound_bandwidth
        data_template:
          value: >-
            {{
            (((
            (states('sensor.internet-inbound_counter') | float | round(2))
            - (states('input_number.inbound_memory') | float | round(2))) * 8 * 1000)
            / (15*1000000000)) | float | round(2) }}
      - service: input_number.set_value
        entity_id: input_number.outbound_bandwidth
        data_template:
          value: >-
            {{
            (((
            (states('sensor.internet-outbound_counter') | float | round(2))
            - (states('input_number.outbound_memory') | float | round(2))) * 8 * 1000)
            / (15*1000000000)) | float | round(2) }}
mode: single

This automation runs every 15 seconds (as specified in the trigger) and performs the following actions:

  1. Retrieves the current inbound and outbound SNMP counter values and stores them in input number entities (inbound_memory and outbound_memory).
  2. Delays for 1 second to allow SNMP counters to update.
  3. Calculates the inbound bandwidth based on the difference between current and previous inbound counter values, considering the time interval, and stores it in the inbound_bandwidth input number entity.
  4. Calculates the outbound bandwidth in a similar manner and stores it in the outbound_bandwidth input number entity.

The need for Input Text Fields

In the Home Assistant automation, input text fields are used to store and display the network statistics. These fields are versatile as they can hold both numerical and textual data. They are utilized to store the current values of inbound and outbound memory, as well as inbound and outbound bandwidth.

  1. input_number.inbound_memory: input text field is pivotal here. It captures real-time values of the inbound memory counter, which monitors incoming network traffic. It’s exclusively for numerical values, rounding them to two decimal places for precision.
  2. input_number.outbound_memory: Similar to the inbound memory, this input text field stores the current value of the outbound memory counter, representing outgoing network traffic. It is updated dynamically and reflects the changes in network activity.
  3. input_number.inbound_bandwidth: The input text field inbound_bandwidth is designed to hold the calculated inbound bandwidth value. It takes into account the difference between the current inbound counter value and the stored inbound memory value. The formula (current - memory) * 8 * 1000 / (15 * 1000000000) calculates the bandwidth in megabits per second (Mbps) and updates this field accordingly.
  4. input_number.outbound_bandwidth: Similarly, the outbound_bandwidth input text field captures the calculated outbound bandwidth value.

These input text fields act as repositories for network statistics, allowing the automation to update and display the data accurately, providing insights into network usage over time.

Lovelace Entry

To display the collected network statistics in a visually appealing manner, you can create a Lovelace dashboard in Home Assistant. Utilizing Lovelace cards, you can present the monitored network metrics such as inbound and outbound bandwidth, memory values, and more in an easily understandable format. This allows you to have a clear overview of your network’s performance at a glance, enhancing your monitoring and control capabilities.

Example using some Mushroom card elements:

type: vertical-stack
cards:
  - type: custom:mushroom-title-card
    title: Internet
    subtitle: Current Utilisation (15s Refresh)
  - type: markdown
    content: 'xxxxxxxx xxxxxx Login: https://192.168.1.1'
  - square: false
    type: grid
    cards:
      - type: gauge
        entity: input_number.inbound_bandwidth
        unit: Mbps
        name: Inbound
        max: 1000
        needle: true
        severity:
          green: 0
          yellow: 550
          red: 800
      - type: gauge
        entity: input_number.outbound_bandwidth
        name: Outbound
        unit: Mbps
        severity:
          green: 0
          yellow: 28
          red: 42
        needle: true
        max: 50
    columns: 2
  - graph: line
    type: sensor
    entity: input_number.inbound_bandwidth
    detail: 2
    unit: Mbps
    hours_to_show: 48
    name: Inbound Usage History
  - graph: line
    type: sensor
    entity: input_number.outbound_bandwidth
    detail: 2
    name: Outbound Usage History
    hours_to_show: 48
    unit: Mbps
bandwidth_screenshot.png

Conclusion

Empower your network management with the integration of SNMP and Home Assistant. You can enhance your network’s performance and gain valuable insights into its operation—welcome to the world of intelligent network monitoring with Home Assistant and SNMP.