Remaping states of SNMP sensors

Dear HA Community,

I’m facing some configuration problems with remapping sensor values.

I have a network switch that I would like to monitor in HA over SNMP.

I have the following sensor configured (one for every port, but let’s keep it simple):

- platform: snmp
  name: 'SNMP XS1920-12 Port 3'
  host: 192.168.1.254
  baseoid: 1.3.6.1.2.1.2.2.1.8.3
  community: mycommunity
  version: '2c'
  scan_interval: 60
  accept_errors: true

That correctly gives me numeric sensor values for the ports.

There are multiple possible values: 1 for port state up, 2 for port state down, 3 for port testing, etc.

I would like to remap the values to strings of what the state actually is.

Therefore following some other threads on this forum, I tried adding:

     value_template: >-
           {% set mapper =  {
               '1' : 'Up',
               '2' : 'Down',
               '3' : 'Testing',
               '4' : 'Unk',
               '5' : 'Dormant',
               '6' : 'notPresent',
               '7' : 'lowerLayerDown' } %}
           {% set state = states.sensor.snmp_xs1920_12_port_3.state %}
           {{ mapper[state] if state in mapper else 'Weird State' }}

and also tried:

     value_template: >-
         {% set mapper = ['Weird State', 'Up', 'Down', 'Testing', 'Unk', 'Dormant', 'notPresent', 'lowerLayerDown'] %}
         {{ mapper[states.sensor.snmp_xs1920_12_port_3.state | int] }}

In both cases I’m getting “Weird State” regardless of the actual port state, as if the value is never matched.

2

Is there something I’m missing?

The value from the device is in fact an integer:

# snmpwalk -v2c -c mycommunity 192.168.1.254 1.3.6.1.2.1.2.2.1.8.3
iso.3.6.1.2.1.2.2.1.8.3 = INTEGER: 1

Thank you very much in advance for any help.
Cheers!

This is not the recommended way to get an entity’s state in a template. You should use states('sensor.snmp_xs1920_12_port_3') instead.
However, that isn’t the cause of your problem.
Are you creating a new template sensor? If so, please check in developer tools what the state of your SNMP sensor is.
Open your Home Assistant instance and show your state developer tools.
If you are using the value_template of your SNMP sensor, you’ll need to use the value variable instead of fetching the state from the state machine:

     value_template: >-
           {% set mapper =  {
               '1' : 'Up',
               '2' : 'Down',
               '3' : 'Testing',
               '4' : 'Unk',
               '5' : 'Dormant',
               '6' : 'notPresent',
               '7' : 'lowerLayerDown' } %}
           {{ mapper[value] if value in mapper else 'Weird State' }}

You can also simplify the last part of the template:

           {{ mapper.get(value, 'Weird State') }}

I’m not sure if value will be a string, you might need to remove the single quotes around numbers in mapper if value is an integer.

1 Like

Thank you! That was extremely informative and helpful. Indeed your solution does work.

I was just setting up a sensor, trying to figure it out. Now that I have it working, I can try preparing a sensor template.

Btw., Quotes for the numbers were actually needed.

I greatly appreciate your help! Thank you!

Cheers!