Using SNMP on variable (switching) oids

Hi,
I am currently using snmp to read some oids from a DD-wrt router for bandwidth monitoring and such.
All is well, but: the oids of the interfaces sometimes change after a reboot (probably something to do with the initialization times of the interfaces?) . The result is that my sensor reports nothing because of this.

baseoid: 1.3.6.1.2.1.31.1.1.1.6.8 on dd-wrt reports the total bandwidth used, but after a reboot it sometimes changes to 1.3.6.1.2.1.31.1.1.1.6.11 or 1.3.6.1.2.1.31.1.1.1.6.12 on the router.

I was thinking of creating sensors which read the oids where the interface names are contained:
iso.3.6.1.2.1.31.1.1.1.1.1 = STRING: “lo”
iso.3.6.1.2.1.31.1.1.1.1.8 = STRING: “br0”
and based on that use some kind of variable in the sensor which contains the last integer of the correct oid, in this case that would be “8”…

ie:

  • platform: snmp
    host: 192.168.0.253
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.{{some_variable}}

Any ideas on how i could go about this?

Tried this:

  • platform: snmp
    name: ddwrt br0 if 1
    host: 192.168.0.253
    baseoid: 1.3.6.1.2.1.31.1.1.1.1.8
    community: ‘public’
    version: ‘2c’
    scan_interval: 3600

  • platform: snmp
    name: ddwrt br0 if 2
    host: 192.168.0.253
    baseoid: 1.3.6.1.2.1.31.1.1.1.1.12
    community: ‘public’
    version: ‘2c’
    scan_interval: 3600

  • platform: template
    sensors:
    ddwrt_bridge0:
    value_template: >
    {% if is_state(‘sensor.ddwrt_br0_if_1’ , ‘br0’) %} 1.3.6.1.2.1.31.1.1.1.6.8
    {% elif is_state(‘sensor.ddwrt_br0_if_2’ , ‘br0’) %} 1.3.6.1.2.1.31.1.1.1.6.12
    {% endif %}

  • platform: snmp
    name: bridge0 rx test
    host: 192.168.0.253
    baseoid:
    value_template: “{{ states(‘sensor.ddwrt_bridge0’) }}”
    community: ‘public’
    version: ‘2c’
    scan_interval: 3600

Got an “Unknown” in template editor and below error in the log:

sensor.snmp:
- Invalid config for [sensor.snmp]: value should be a string for dictionary value @ data[‘baseoid’]. Got OrderedDict([(‘value_template’, “{{ states(‘sensor.ddwrt_bridge0’
) }}”)]). (See ?, line ?).

“Solved” it like this, i someone has an idea to make the template a bit smaller/more optimal…

Determine all interface names

  • platform: snmp
    name: ddwrt_ifname_1
    host: 192.168.0.253
    baseoid: 1.3.6.1.2.1.31.1.1.1.1.[1-12] (returns the name of the interface)
    community: ‘public’
    version: ‘2c’
    scan_interval: 3600

Read counters for all interfaces:

  • platform: snmp
    name: ddwrt_if_1_rx
    host: 192.168.0.253
    baseoid: 1.3.6.1.2.1.31.1.1.1.6.[1-12] (returns interface stats)
    community: ‘public’
    version: ‘2c’
    scan_interval: 3600

Read correct values for interfaces

  • platform: template
    sensors:
    ddwrt_eth0_rx:
    value_template: >
    {% if is_state(‘sensor.ddwrt_ifname_1’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_1_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_2’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_2_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_3’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_3_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_4’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_4_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_5’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_5_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_6’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_6_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_7’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_7_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_8’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_8_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_9’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_9_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_10’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_10_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_11’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_11_rx’) }}
    {% elif is_state(‘sensor.ddwrt_ifname_12’ , ‘eth1’) %}
    {{ states(‘sensor.ddwrt_if_12_rx’) }}
    {% endif %}

Please add proper formatting to things you mark as a solution. Your current solution will not work for anyone when they copy and paste because the formatting for the post is incorrect. Edit your post, add ``` before and after your code seconds to turn it into a codeblock. This is all described in the FAQ point 11.