Reverse, invert logic 0 to 1

Hi !
I have snmp sensor, which for some unknown reason shows an inverted state.

- platform: snmp
    name: "LC210 OUT 1"
    host: 192.168.1.7
    baseoid: 1.3.6.1.4.1.17095.3.2.0

The sensor returns a value of 0 or 1 ( on or off output relay )

How can I change the value of 1 to 0, and 0 to 1
Or better 1 = OFF, 0 = ON

Thankyou for help !

Template sensor

Can you help? I can’t find any similar example with a template sensor.

EDIT

Try below. You need to correct the entity I’d to match the snmp sensor but I think this is about correct. I changed the link to one that may be more relevant to what you need

binary_sensor:
  - platform: template
    sensors: 
      lc210_inverted:
        friendly_name: LC210 Inverted
        entity_id: sensor.lc210_out_1
        value_template: “{%if states.sensor.lc210_out_1.state == ‘1’ %}Off{% elif states.sensor.lc210_out_1.state == ‘0’ %}On{% endif %}”

The value template I use is different from example at link but this should get you close. I may have better example and will try to post it later

The following Template Binary Sensor converts the SNMP Sensor’s numeric value (0/1) to a boolean value (true/false) and then inverts it. Because it’s a binary sensor, the resulting inverted boolean value will be reported as on/off.

template:
  - binary_sensor: 
      - name: LC210 Inverted
        state: "{{ not(states('sensor.lc210_out_1') | bool) }}"
3 Likes

The SNMP integration provides a value_template option. If you wish, you can use it to invert the received value directly in the SNMP Sensor, without the need for using a Template Binary Sensor.

  - platform: snmp
    name: "LC210 OUT 1"
    host: 192.168.1.7
    baseoid: 1.3.6.1.4.1.17095.3.2.0
    value_template: "{{ iif(value | int(0) == 1, 'off', 'on') }}"
1 Like

It works, exactly what I wanted! Many thanks! Also, ON is better than 1 … :hugs: :hugs:

1 Like

Thankyou !

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.