I like the use of the snmpget
command, but it doesn’t seem to be installed on HASSIO, does anyone know a workaround?
Can someone help me do this with a Unifi USG?
I’d like to know if it’s possible to make this per client bandwidth?
What do I use to see capture the OIDs of my clients based on their MAC’s
Thanks
Hi, this is working really well for me, it allows me to see my see my bandwidth.
I can even log the Speed using the recorder component and make a nice graph of bandwidth usage.
However my SNMP updates every 10 seconds or so, so it seems to have made my database double in size!
Is there a way I can limit it so it only updates once per minute? Thanks
I think you can use the scan_interval
option to adjust the update frequency.
So scan_interval: 60
should be 1/min.
thanks, I didn’t see scan_interval
in the snmp docs:
turns out it works, thanks very much
I really appreciate this, but a few slight changes are necessary for the latest HASS code. Hope this can benefit the next person…
- input_slider renamed to input_number
- service select_value is now set_value
- input_number must be explicitly defined
NOTE: maximum has been defined assuming 32-bit counter
For example:
input_number:
internet_traffic_delta_in:
min: 0
max: 4294967295
step: 1
internet_traffic_delta_out:
min: 0
max: 4294967295
step: 1
Cheers!
How to run snmpget from docker ?
One trick to run anything in the docker host that I used, is a command line sensor that does ssh to the host itself and executes a command:
ssh ha@rpi /home/ha/.homeassistant/some_script.sh
this needs another trick since you need persistent storage for the container’s ssh to use always the same ssh key. I did that by creating ssh keys for the container to use, and then storing them in the host in a “container_persistent_storage” folder I created, mapping that to /root/.ssh in the container. In docker-compose, something like this works:
volumes: - /home/ha/.homeassistant:/config - /home/ha/.homeassistant/container_persistent_storage/ssh_config:/root/.ssh
I still use this method for some basic calls to host scripts, but I have moved most of the most frequent calls such as the snmp polls of my router to a web service. I wrote a simple web service in python that I run as a daemon in the host, using the werkzeug library I expose a bunch of KPIs that a dockerized homeassistant consumes via sensors using a REST sensor. This approach needs more work to setup but it is very light on resources.
Once I had the webservice running the polling, it was easier to do the throughput calculations directly in it and expose to HA the results via the REST API
After upgrading to 108 i’ve noticed an error in the log
2020-04-10 05:09:58 ERROR (MainThread) [homeassistant.components.automation] Monitor Traffic In: Error executing script. Invalid data for call_service at pos 1: Invalid value for input_number.internet_traffic_delta_in: -3072910986.1984906 (range 0.0 - 4294967295.0)
I previously had it set to
max: 1000000000000
and changed it according to the post above, but still the error
Adjust your min to -4294967295
I recently changed the MAC address of my WAN interface in pfsense, and after that I lost tracking of in/out data. I was using the BASEOIDs used in the original post here (1.3.6.1.2.1.2.2.1.10.2 and 1.3.6.1.2.1.2.2.1.16.2), which worked fine.
I’m guessing the change of NIC (and thus MACs address) caused the BASOID to change, but to what? Anyone got any an idea?
Use an SNMPwalker to find them.
Guys,
Have someone a good working configs with PFsense en SNMP integration?
I used SNMPwalker for windows to find the interfaces, and then I guessed based on their sent/received data which one was which.
Why guess when 1.3.6.1.2.1.2.2.1.2
tells you the description of the entries?
Hi to all!
I am in this condition:
Why?
This is my configuration.yaml
`sensor:
-
platform: systemmonitor
resources:- type: disk_use_percent
arg: /home - type: memory_use_percent
- type: load_1m
- type: load_5m
- type: load_15m
- type: processor_use
- type: disk_use_percent
-
platform: mitemp_bt
rounding: True
decimals: 2
period: 60
log_spikes: False
use_median: False
active_scan: False
batt_entities: False
sensor_names:
‘A4:C1:38:2E:FA:57’: ‘Ufficio’
‘A4:C1:38:FF:85:1F’: ‘Camera’
‘A4:C1:38:C6:F3:EF’: ‘Cucina’
report_unknown: False
whitelist: False -
platform: command_line
name: CPU Temperature
command: “cat /sys/class/thermal/thermal_zone0/temp”
unit_of_measurement: “°C”
value_template: ‘{{ value | multiply(0.001) | round(1) }}’ -
platform: snmp
name: snmp_wan_in
host: 192.168.10.201
baseoid: 1.3.6.1.2.1.2.2.1.10 -
platform: snmp
name: snmp_wan_out
host: 192.168.10.201
baseoid: 1.3.6.1.2.1.2.2.1.16.2 -
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
input_slider:
internet_traffic_delta_in:
name: “Traffic IN Delta”
initial: 0
min: 0
max: 1000000000000
internet_traffic_delta_out:
name: “Traffic OUT Delta”
initial: 0
min: 0
max: 1000000000000
`
and this is automation.yaml
-
alias: Monitor Traffic
trigger:
platform: state
entity_id: sensor.snmp_wan_inaction:
- 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) ) }}’
- service: input_slider.select_value
-
alias: Monitor Traffic
trigger:
platform: state
entity_id: sensor.snmp_wan_outaction:
- 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) ) }}’
- service: input_slider.select_value
Can anyone help me?
I think you missed the last digit for snmp_wan_in, also make sure that 2 is actually the proper interface for your WAN port. You can check what interfaces map to which numbers by doing snmpwalk to 1.3.6.1.2.1.2.2.1.2, though it changes occasionally in my experience, also on my old router it said something like vlan2 for the WAN interface so just be aware it might be something like that.
Other than that, some of the object names have changed since the original post. input_slider should be changed to input_number for example. I can’t remember off the top of my head if anything else needed to be changed, but your log should probably let you know if you put anything in there it can’t deal with.
Hi. Can you paste your full config of bandwidth monitor? I can’t get it working with current HA version.
Hey there,
Thanks to this thread, I have had a functional bandwidth monitor based on SNMP metrics for a while. This week, I wanted to add a utility meter-like sensor, so I could monitor my usage, and make sure I didn’t go over my monthly quota.
While reading around, I realised that HA now supports templated sensors (see Template - Home Assistant), which means the input_number
and automation helpers are no longer needed.
Here is my updated, simplified, config.
sensor:
# Based on https://community.home-assistant.io/t/snmp-bandwidth-monitor/7122; This hasn't changed
# Get raw SNMP readings
- platform: snmp
name: snmp_wan_in
host: 192.2.0.1
baseoid: 1.3.6.1.2.1.2.2.1.10.20 # the second last integer should match your WAN interface
- platform: snmp
name: snmp_wan_out
host: 192.2.0.1
baseoid: 1.3.6.1.2.1.2.2.1.16.20 # the second last integer should match your WAN interface
# Calculate the mean from templated speed sensors below
- platform: statistics
unique_id: internet_speed_down_mean
name: 'Internet Speed Down (mean)'
entity_id: sensor.internet_speed_down
state_characteristic: mean
sampling_size: 20
- platform: statistics
unique_id: internet_speed_up_mean
name: 'Internet Speed Up (mean)'
entity_id: sensor.internet_speed_up
state_characteristic: mean
sampling_size: 20
template:
# Templated internet_speed_down and internet_speed_up sensor, that update automatically when the snmp sensors do
- trigger:
- platform: state
entity_id: sensor.snmp_wan_in
sensor:
- unique_id: internet_speed_down
name: 'Internet Speed Down'
state: >
{{ (
( (trigger.to_state.state | int - trigger.from_state.state | int) * 8 / 1000000 )
/ ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated))
) | round(2)
}}
state_class: measurement
device_class: data_rate
unit_of_measurement: 'Mbit/s'
icon: mdi:download
- trigger:
- platform: state
entity_id: sensor.snmp_wan_out
sensor:
- unique_id: internet_speed_up
name: 'Internet Speed Up'
state: >
{{ (
( (trigger.to_state.state | int - trigger.from_state.state | int) * 8 / 1000000 )
/ ( as_timestamp(trigger.to_state.last_updated) - as_timestamp(trigger.from_state.last_updated))
) | round(2)
}}
state_class: measurement
device_class: data_rate
unit_of_measurement: 'Mbit/s'
icon: mdi:upload
Based on the SNMP sensor, I also created an total_increasing
counter of the sum of up and down traffic, as well as a monthly-resetting (on the 23rd) utility meter.
template:
[...]
- sensor:
- unique_id: internet_total_usage
name: 'Internet Total Usage'
state: >
{{ (
(states("sensor.snmp_wan_in")|int + states("sensor.snmp_wan_out")|int)
/ (1024*1024)
) | round(2)
}}
state_class: total_increasing
device_class: data_size
unit_of_measurement: 'MiB'
icon: mdi:chart-bell-curve-cumulative
utility_meter:
internet_usage:
unique_id: internet_usage
name: "Internet Usage"
source: sensor.internet_total_usage
cycle: monthly
offset:
days: 22 # offset from day 1
periodically_resetting: true
For ease of update of the monthly quota, I added a couple of input_number
to set the quota and the reset date (unfortunately, I haven’t found a way to reuse it in the utility_meter
)
input_number:
internet_quota:
name: "Internet Quota"
initial: 500000
min: 0
max: inf
icon: mdi:chart-multiline
mode: box
unit_of_measurement: MiB
internet_quota_rollover_day:
name: "Internet Quota Rollover Day"
initial: 23
min: 1
max: 31
icon: mdi:calendar-refresh
mode: box
This allowed me to calculate an ideal usage given the date, i.e., if my current usage is above the ideal usage, I’m at risk of exceeding the quota before the end of the month. A binary_sensor
just does that comparison; if it’s on
, I’m in trouble.
template:
[...]
- unique_id: internet_ideal_usage
name: 'Internet Ideal Usage'
state: >
{% set day = now().date().day %}
{% set month = now().month %}
{% set rollover = states("input_number.internet_quota_rollover_day")|int %}
{% if day >= rollover %}
{% set start = now().date().replace(day=rollover) %}
{% set next_month = start.replace(day=28) + timedelta(days=4) %}
{% set end = next_month.replace(day=rollover) %}
{% else %}
{% set end = now().date().replace(day=rollover) %}
{% set last_month = end.replace(day=1) - timedelta(days=1) %}
{% set start = last_month.replace(day=rollover) %}
{% endif %}
{% set full_period = (as_timestamp(end) - as_timestamp(start))|int %}
{% set current_period = (as_timestamp(now()) - as_timestamp(start))|int %}
{% set quota = states("input_number.internet_quota")|int %}
{% set ideal = ((quota*current_period)/full_period)|float %}
{{ ideal }}
state_class: total_increasing
device_class: data_size
unit_of_measurement: 'MiB'
icon: mdi:chart-bell-curve-cumulative
- binary_sensor:
- unique_id: internet_usage_warning
name: 'Internet Usage Warning'
state: '{{ (states("sensor.internet_usage")|int) > (states("sensor.internet_ideal_usage")|int) }}'
icon: >
{% if is_state('binary_sensor.internet_usage_warning', 'on') %}
mdi:alert
{% else %}
mdi:check-circle-outline
{% endif %}
And to tie it neatly, I added an automatic (daily) notification in case the warning is triggered.
automation:
- alias: "Notification: Internet usage"
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.internet_usage_warning
for:
hours: 1
from: "off"
to: "on"
condition:
- >-
{{ now() - (state_attr(this.entity_id, 'last_triggered') or
datetime.datetime(1, 1, 1, 0, 0) ) > timedelta(hours=12) }}
action:
- service: notify.persistent_notification
data:
title: Internet usage over quota
message: >
{{state_attr('sensor.internet_usage', 'friendly_name')}}
({{states('sensor.internet_usage')|round(2)}} MiB) is higher than ideal
({{states('sensor.internet_ideal_usage')|round(2)}} MiB)
Turn things off!
mode: single