# Example dashboard for the Managed Switch (SNMP) integration.
# Settings > Dashboards > Add dashboard > New dashboard from scratch, then the three dots >
# Edit dashboard > three dots > Raw configuration editor, and paste this.
# It finds every switch and port by itself, in any language; nothing to adapt.
title: Switches
views:
  - title: Ports
    path: ports
    icon: mdi:switch
    cards:
      - type: markdown
        title: Which device is on which port
        content: |
          {#- Names come from the devices themselves: escape what Markdown would read as table, link or image. -#}
          {%- macro md(v) -%}{{ (v ~ '') | replace('\r', ' ') | replace('\n', ' ') | replace('\\', '\\\\') | replace('|', '\\|') | replace('[', '\\[') | replace(']', '\\]') | replace('!', '\\!') | replace('<', '&lt;') | replace('>', '&gt;') | replace('`', '\\`') | replace('*', '\\*') | replace('_', '\\_') }}{%- endmacro -%}
          {% set ports = states.sensor
             | selectattr('entity_id', 'in', integration_entities('managed_switch'))
             | selectattr('attributes.device_count', 'defined')
             | sort(attribute='attributes.if_index') | list %}
          {% set ns = namespace(switches=[]) %}
          {% for port in ports %}{% set ns.switches = ns.switches + [device_id(port.entity_id)] %}{% endfor %}
          {% for switch in ns.switches | unique %}
          ### {{ md(device_attr(switch, 'name_by_user') or device_attr(switch, 'name')) }}

          | Port | Link | Speed | Device |
          | --- | :---: | ---: | --- |
          {% for port in ports if device_id(port.entity_id) == switch -%}
          | [{{ md(port.attributes.port) }}](/history?entity_id={{ port.entity_id }}) | {{ '🟢' if port.attributes.link else '⚪' }} | {{ (port.attributes.speed ~ ' Mbit/s') if port.attributes.speed else '' }} | {{ md(port.state) if port.state not in ['unknown', 'unavailable'] else '' }} |
          {% endfor %}
          {% endfor %}
