If you want to monitor current bandwidth on a SNMP accessible network device, such as routers and wifi access points. Here is a tutorial on how to configure it in Home Assistant. Hopefully someone else will find it useful.
SNMP Sensor
Grabs data via SNMP from my router addressing the WAN port IN/OUT traffic via the OID:
- platform: snmp
name: snmp_wan_in
host: 10.0.1.1
baseoid: 1.3.6.1.2.1.2.2.1.10.2
- platform: snmp
name: snmp_wan_out
host: 10.0.1.1
baseoid: 1.3.6.1.2.1.2.2.1.16.2
Automations
Where the magic happens. Here’s a breakdown:
- Grab trigger.to_state and trigger.from_state values.
- Subtract those two values to get the delta.
- Multiply that delta by 8 to get the total number of bits transmitted between the time interval.
- Grab the time interval delta using the last_updated time from the to_state and from_state entities.
- We now have our bits per second.
- alias: Monitor Traffic
trigger:
platform: state
entity_id: sensor.snmp_wan_in
action:
- service: input_slider.select_value
data_template:
entity_id: input_slider.internet_traffic_delta_in
value: '{{ ((trigger.to_state.state | int - trigger.from_state.state | int) * 8 ) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) ) }}'
- alias: Monitor Traffic
trigger:
platform: state
entity_id: sensor.snmp_wan_out
action:
- service: input_slider.select_value
data_template:
entity_id: input_slider.internet_traffic_delta_out
value: '{{ ((trigger.to_state.state | int - trigger.from_state.state | int) * 8 ) / ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated) ) }}'
Formatting
Next we want to make it look good in the Home Assistant UI.
The code here was used to take the data in the input slider, and first create a sensor that shows us Mbps by coverting bps to Mbps. Then, I choose to setup a statistics sensor for the IN/OUT.
- platform: template
sensors:
internet_speed_in:
friendly_name: 'Internet Speed'
value_template: '{{ ((states.input_slider.internet_traffic_delta_in.state | float ) / 1000000 ) | round(2) }}'
unit_of_measurement: 'Mbps'
- platform: template
sensors:
internet_speed_out:
friendly_name: 'Internet Speed'
value_template: '{{ ((states.input_slider.internet_traffic_delta_out.state | float ) / 1000000 ) | round(2) }}'
unit_of_measurement: 'Mbps'
- platform: statistics
name: 'WAN Traffic IN'
entity_id: sensor.internet_speed_in
- platform: statistics
name: 'WAN Traffic OUT'
entity_id: sensor.internet_speed_out
Make it look good.
Just a few customizations:
sensor.download_speed_mean:
friendly_name: 'Speedtest Download Average'
icon: mdi:speedometer
sensor.upload_speed_mean:
friendly_name: 'Spedtest Upload Average'
icon: mdi:speedometer
sensor.ping_time_mean:
friendly_name: 'Speedtest Ping Average'
icon: mdi:clock-fast
sensor.wan_traffic_in_mean:
friendly_name: 'WAN Download Average'
icon: mdi:download
sensor.wan_traffic_out_mean:
friendly_name: 'WAN Upload Average'
icon: mdi:upload