Working SNMP example with OID string transformation

Hi, I am posting a custom SNMP integration example here so that others might find it. I was trying to write an SNMP integration with OID string transformation and had to cobble together a configuration I found from several different forum posts (all were very helpful, thanks).

My requirement is pretty simple: I want to monitor the status of an ancient Datum Tymserve 2100 that I have running as an NTP server. There are three LEDs on the front panel of the device that correspond to three SNMP OIDs that need to be polled. I also want to transform the string returned by the device for each OID into custom text, with a custom icon depending on the value of the OID string.

There may be a better way to do this with platform templates, but this example works for me and I actually like having the original sensor (sensor.datum_tymserve_2100_foo) and transformed sensor (sensor.template_datum_tymserve_2100_foo) available.

My configuration.yaml:

sensor:
  - platform: snmp
    name: "Datum Tymserve 2100 tymingStatus"
    # DESCRIPTION "This variable reflects the current status information on the 2100 Time and Frequency Processor."
    # Tracking or Flywheeling
    host: 192.168.0.19
    # version: 1
    community: public
    baseoid: 1.3.6.1.4.1.601.1.1.2.1.2.2.0
    accept_errors: true
    scan_interval: 15
  - platform: snmp
    name: "Datum Tymserve 2100 tymingMode"
    # DESCRIPTION "Operating mode of the timing engine."
    # timecode (1)
    # freerun (2)
    # internal (3)
    # gps (7)
    host: 192.168.0.19
    # version: 1
    community: public
    baseoid: 1.3.6.1.4.1.601.1.1.2.1.2.3.0
    accept_errors: true
    scan_interval: 15
  - platform: snmp
    name: "Datum Tymserve 2100 gpsAllinView"
    # DESCRIPTION "Satellites in view packet from GPS."
    # Example: Auto 3-D Sats: 3 28 27 16 9 26 31
    host: 192.168.0.19
    # version: 1
    community: public
    baseoid: 1.3.6.1.4.1.601.1.1.2.1.3.5.0
    accept_errors: true
    scan_interval: 60

template:
  - sensor:
    - name: template_datum_tymserve_2100_tymingstatus
      state: >
        {% if is_state("sensor.datum_tymserve_2100_tymingstatus", "Tracking") %}
          Tracking
        {% elif is_state("sensor.datum_tymserve_2100_tymingstatus", "Flywheeling") %}
          Flywheeling
        {% else %}
          Error
        {% endif %}
      icon: >
        {% if is_state("sensor.datum_tymserve_2100_tymingstatus", "Tracking") %}
          mdi:satellite-variant
        {% else %}
          mdi:alert-circle
        {% endif %}
  - sensor:
    - name: template_datum_tymserve_2100_tymingmode
      state: >
        {% set states = states("sensor.datum_tymserve_2100_tymingmode") | int %}
        {% set text = {
          1: "Timecode",
          2: "Freerun",
          3: "Internal",
          7: "GPS"} %}
        {{ text[states] if states in text.keys() else "Error" }}
      icon: >
        {% if is_state("sensor.datum_tymserve_2100_tymingmode", "7") %}
          mdi:satellite-variant
        {% else %}
          mdi:alert-circle
        {% endif %}
  - sensor:
    - name: template_datum_tymserve_2100_gpsallinview
      state: >
        {% set original_state = states("sensor.datum_tymserve_2100_gpsallinview") %}
        {{ original_state | replace("Auto 3-D Sats: ", "") }}
      icon: mdi:satellite-variant

Then I added it to my dashboard like this:

          - square: false
            type: grid
            cards:
              - type: tile
                entity: sensor.template_datum_tymserve_2100_tymingstatus
                name: tymingStatus
                icon: ''
                color: green
                show_entity_picture: true
                hide_state: false
                vertical: false
                icon_tap_action:
                  action: more-info
                features_position: bottom
              - type: tile
                entity: sensor.template_datum_tymserve_2100_tymingmode
                name: tymingMode
                icon: ''
                color: green
                show_entity_picture: true
                hide_state: false
                state_content: state
                vertical: false
                icon_tap_action:
                  action: more-info
                features_position: bottom
              - type: tile
                entity: sensor.template_datum_tymserve_2100_gpsallinview
                name: gpsAllinView
                icon: ''
                color: green
                show_entity_picture: true
                hide_state: false
                state_content: state
                vertical: false
                icon_tap_action:
                  action: more-info
                features_position: bottom
            columns: 2
            title: Tymserve 2100

Which looks like this:

Is HA also an NMS :slight_smile: ?

The SNMP sensor platform supports value_template, so you can do it directly in the sensor.