How to set a value to a SNMP-enabled switch using input_number?

Hello, I’m trying to set a value (integer) to a SNMP-enabled switch.
The value is defined using input_number. My example code is:

input_number:
  brightness:
    name: Brightness
    initial: 0
    min: 0
    max: 1023
    step: 1

switch:
  - platform: snmp
    name: "Set Brightness"
    command_payload_on: "{{ states('input_number.brightness') | int }}"
    host: 192.168.1.100
    community: private
    baseoid: 1.3.6.1.4.1.42505.8.2.4.1.3.0

The result is receiving: AttributeError: ‘str’ object has no attribute ‘getTagSet’. Here is the last log message:

File "/usr/local/lib/python3.7/site-packages/pysnmp/proto/api/v1.py", line 42, in setOIDVal
varBind.setComponentByPosition(1).getComponentByPosition(1).setComponentByType(val.getTagSet(), val, verifyConstraints=False, matchTags=False, matchConstraints=False, innerFlag=True)

Am I wrong about the value of command_payload_on ?

Your syntax is right.

There looks to have been a bug sending integers as payloads. Not sure what version you’re running.

I’m not 100%, but you could try removing the “| int” conversion at the end of the command_payload_on. Not sure it will make much of a difference if pysnmp is converting it to an integer anyway…but worth a shot.

It looks like this was merged into 0.101.2, so not 100% sure what the issue is…unless you’re still running 0.101.0 :stuck_out_tongue:

1 Like

Thank you for the prompt reply.
Removing the “| int” conversion reproduces the same error.
Using a constant (for example command_payload_on: “123”) works well.

My version is 0.104.3:
sys
Maybe some other conversion is needed for this case ?

Oh, I see.

The “command_payload_on” can’t be a template. It is passed in on init…not evaluated at execution time.

You’ll have to modify the integration if you want to use the snmp switch class.

Thank you for this detailed clarification.
I’m still looking for a way to send a run-time evaluated value using SNMP.

All switches I’ve seen all take a fixed on/off command. Not a template. But, since you are setting brightness…could you use a template light?

To call snmp from the command line, maybe define a shell command (assuming you can run the same command in cmd line.

You could leave your snmp_switch. Just define the command_payload_off (hard coded) and use these as the turn_on and turn_off in the template light. Then use the shell command to set brightness.

Just a thought.

Thank you for the suggestion to use shell_command.
Finally, I managed to set the SNMP value using input_number and automation (the code below).

shell_command:
  set_brightness: /usr/bin/snmpset -v 1 -c private 192.168.1.100 1.3.6.1.4.1.42505.8.2.4.1.3.0 i {{ brightness }}

input_number:
  brightness:
    name: Brightness
    initial: 0
    min: 0
    max: 1023
    step: 1
    
automation:
  - alias: "Set Brightness"
    hide_entity: true
    trigger:
      platform: state
      entity_id: input_number.brightness
    action:
      - service: shell_command.set_brightness
        data_template:
          brightness: "{{ states('input_number.brightness') | int }}"

Of course, the final implementation needs a sensor using snmpget to refresh the input_number value.

Best wishes!

1 Like