[Custom component] AsusRouter integration

Nice thanks!
Is it possible to sort by name?

Hi. Yes. Not in the nicest looking way (because of the limited template possibilities of HA), but possible:

{% set ns = namespace(devices = []) %}
{# Process the list of strings into the list of dicts #}
{% for item in state_attr('sensor.rt_ax88u_connected_devices', 'devices') %}
  {%- set attrs = item.split("/") %}
  {%- set mac = attrs[0] %}
  {%- set ip = attrs[1] %}
  {%- set name = item.replace((mac + "/" + ip + "/"), "") %}
  {%- set device = [dict(mac = mac, ip = ip, name = name)] %}
  {%- set ns.devices = ns.devices + device %}
{% endfor %}
{# Sort #}
{% set ns.devices = ns.devices|sort(attribute="name", case_sensitive=true) %}
{# Output #}
{% for device in ns.devices %}
  {{- device.name}}
{% endfor %}

Just change the last part after {# Output #} to change what exactly is displayed

1 Like

Wow perfect! thanks for taking time helping! Much appriciated :smiley:

Greetings,

First, thanks for creating and maintaining such an awesome Add-On.

I do have one question (which could be a Feature Request as well) regarding Device Trackers - is there any way to stop the integration from loading the connected device entities (and/or device trackers)?

I purely want to utilize the monitoring function of the Integration to view router stats - I really don’t have a use for viewing connected devices as entities/device trackers, and it tends to complicate things (for me, specifically) within Home Assistant.

Thanks again!

Hello,

Thanks for your feedback! :+1:

Unfortunately, currently, it is not possible to disable device trackers.

There is a plan to make integration more customizable, so users can select what of the sensors (and device trackers) they want to monitor and which switches to control. Because we are already at almost 40 total possible entities :slightly_smiling_face:

I will put your suggestion to the list of what to do

2 Likes

Thank you!

Hello all!
Thanks for this addon, as well as above “sort by name” thing!
…it’s funny, but when i put lines below in developer tools—> templates it shows my devices perfectly, but when i put it into my sensors template sensor remains “unknown” after HA reboot… any clues what i’m doing wrong?

- platform: template
  sensors:
    asus_devices:
      friendly_name: Asus devices
      value_template: >
        {% set ns = namespace(devices = []) %}
        {# Process the list of strings into the list of dicts #}
        {% for item in state_attr('sensor.rt_ax88u_connected_devices', 'devices') %}
        {%- set attrs = item.split("/") %}
        {%- set mac = attrs[0] %}
        {%- set ip = attrs[1] %}
        {%- set name = item.replace((mac + "/" + ip + "/"), "") %}
        {%- set device = [dict(mac = mac, ip = ip, name = name)] %}
        {%- set ns.devices = ns.devices + device %}
        {% endfor %}
        {# Sort #}
        {% set ns.devices = ns.devices|sort(attribute="name", case_sensitive=true) %}
        {# Output #}
        {% for device in ns.devices %}
        {{- device.name}}
        {% endfor %}

Hey,

If you want to save this list to a sensor state, you can use the following YAML as an example

template:
  - sensor:
      - name: Asus devices
        unique_id: asus_devices
        state: >
          {% set ns = namespace(devices = []) %}
          {# Process the list of strings into the list of dicts #}
          {% for item in state_attr('sensor.rt_ax88u_connected_devices', 'devices') %}
            {%- set attrs = item.split("/") %}
            {%- set mac = attrs[0] %}
            {%- set ip = attrs[1] %}
            {%- set name = item.replace((mac + "/" + ip + "/"), "") %}
            {%- set device = [dict(mac = mac, ip = ip, name = name)] %}
            {%- set ns.devices = ns.devices + device %}
          {% endfor %}
          {# Sort #}
          {% set ns.devices = ns.devices|sort(attribute="name", case_sensitive=true) %}
          {# Output #}
          {% for device in ns.devices %}
            {{- device.name}}
          {% endfor %}

But if you are going to use it to display in the frontend, I suggest don’t create a new entity, but just using any template card. This way you will avoid saving repeated data

thanks, i guess that using template card will be better indeed. I just tried with sensor to see if it works (since i’m still learning…).
I also tried above in new format in templates, as you suggest, but sensor doesn’t even show after templates reload, while after HA restart does show, but again as unknown, no errors in logs… Funny thing…
Maybe one question: how can i achieve “line break” in template card? I mean, if i use above code in templates card, many of them doesn’t show each entry in new line as they do in developer tools…?

Line brakes and whitespaces can be truncated using those minuses after the opening tags or before the closing tags. Like {{- in the {{- device.name}} (spoiler #1). If you would leave only {{, you would get one more line (#2). Both snapshots are with Mushrooms Template Card. Could be, other cards are showing differently (even though they should not).

#1 - with `-`

image

#2 - without `-`

1 Like

I see…thanks. I also use mushroom, but i just found out that your suggestion only works in “Secondary info” and i used primary info, where all data gets written in single line no matter what i do… i guess it’s designed this way…

I was just working on trying to figure out a way to display and sort the Connected Devices attribute, but after coming up with absolutely nothing, I came back to this post and found your code! Thank you, thank you, thank you!

I, too, use Mushroom Cards and was able to customize their template card to show the full list of devices, but I modified your output code a bit to show the IP Address and MAC Address of each device. I am now going to try to figure out if I can incorporate this into the ‘Fold Entity Row’ custom card so I can collapse the list since the list of my connected devices is rather large (70+ connected devices). I’ll post back if I can figure it out… in the meantime, here is the Mushroom Template Card code that I am using (Note that the Entity for the Integration is changed from “asus_router” to “warpcore” and should be changed to your respective name):

type: custom:mushroom-template-card
primary: ''
secondary: |-
  {% set ns = namespace(devices = []) %}
  {# Process the list of strings into the list of dicts #}
  {% for item in state_attr('sensor.warpcore_connected_devices', 'devices') %}
    {%- set attrs = item.split("/") %}
    {%- set mac = attrs[0] %}
    {%- set ip = attrs[1] %}
    {%- set name = item.replace((mac + "/" + ip + "/"), "") %}
    {%- set device = [dict(mac = mac, ip = ip, name = name)] %}
    {%- set ns.devices = ns.devices + device %}
  {% endfor %}
  {# Sort #}
  {% set ns.devices = ns.devices|sort(attribute="name", case_sensitive=true) %}
  {# Output #}
  {% for device in ns.devices %}
    {{- device.name + ' - ' + device.ip + ' - ' + device.mac}}
  {% endfor %}
icon: ''
fill_container: false
multiline_secondary: true
badge_icon: ''
layout: horizontal
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none

I wasn’t able to figure out how to use the ‘Fold Entity Row’ card to collapse the list, however I was able to utilize the ‘Mushroom Chip’ card using a ‘Template’ and ‘Action’ Chip along with the standard ‘Conditional Card’ wrapped around the ‘Mushroom Template’ card (all wrapped inside of the ‘Vertical-Stack-In-Card’) to accomplish the same thing. Plus, I incorporated the actual device count within the ‘Mushroom Template Chip’ as a quick-view of connected devices. See attached images.

The cards used to do this are:

  • Vertical-Stack-In-Card:
    • Mushroom Chip Card (Template + Action Chips)
    • Conditional Card:
      • Mushroom Template Card

Closed
ASUS Router - Home Assistant 1

Opened
ASUS Router - Home Assistant 2

1 Like

Good work! :+1:

A “not-so-important” question: is it possible to add tab to output? I mean, i also tried same output (name, ip, mac), but since names are not same length it gets harder to read ip and mac, so it would be nice if adding tab were possible. Or, perhaps, aligning name to left and ip+mac to right…

Now i’m getting picky, right? :blush:

Possible, but it would become even uglier with code :slightly_smiling_face:

Maybe, it could be written better, but this is a fast-working solution.

{% set ns = namespace(devices = [], max_length = 0, max_tabs = 0) %}
{# Process the list of strings into the list of dicts #}
{% for item in state_attr('sensor.rt_ax88u_connected_devices', 'devices') %}
  {%- set attrs = item.split("/") %}
  {%- set mac = attrs[0] %}
  {%- set ip = attrs[1] %}
  {%- set name = item.replace((mac + "/" + ip + "/"), "") %}
  {%- set device = [dict(mac = mac, ip = ip, name = name)] %}
  {%- set ns.devices = ns.devices + device %}
  {# Save the maximum length of device names #}
  {%- if name|length > ns.max_length %}
    {%- set ns.max_length = name|length %}
  {%- endif %}
{% endfor %}
{# Sort #}
{%- set ns.devices = ns.devices|sort(attribute="name", case_sensitive=true) %}
{%- set ns.max_tabs = (ns.max_length / 8)|int %}
{# Output #}
{% for device in ns.devices %}
  {{- device.name }}
  {%- set tabs = (device.name|length / 8) | int %}
  {%- for tab in ['\t']|batch(ns.max_tabs - tabs + 1, '\t') %}
    {%- for el in tab %}
      {{- el }}
    {%- endfor %}
  {%- endfor %}
  {{- device.ip + '\t' + device.mac }}
{% endfor %}
Snapshot

image

I use the Flex table card for another use cases, but that probably could be used for what you are trying to achieve. It’s quite flexible, you can add more columns, sort, filter, etc…


@Vaskivskyi thanks! That’s VERY nice look!
But, it seems that mushroom card must have some sort of bug…? since that nice look is only in developer tools, while in mushroom template card those extra tabs are not shown, so it gets from your nice look above to this (note i changed yours “…batch(ns.max_tabs - tabs + 1” to “…batch(ns.max_tabs - tabs + 3” for more space between name and ip, but result is the same):

Ii did some searching, but it sems that “template cards” are pretty scarse for HA, since i can’t find anything usefull rather that mushroom card.

@EdwardTFN thanks, i’ll check out. Although quick look doesn’t look proimising, since sensor “sensor.rt_ax88u_connected_devices” has all entities under one single attribute (devices),so it’s hard to easy them. But, card will prove usefull in the future.

Try this:

type: custom:flex-table-card
title: Connected devices
entities:
  include: sensor.rt_ax88u_connected_devices
columns:
  - data: devices
    name: MAC
    modify: x.split('/')[0]
  - data: devices
    name: IP
    modify: x.split('/')[1]
  - data: devices
    name: Name
    modify: x.split('/')[2]

It worked for me:

Just noticed you want the columns in the reverse order, so try this instead:

type: custom:flex-table-card
title: Connected devices
entities:
  include: sensor.rt_ax88u_connected_devices
columns:
  - data: devices
    name: Name
    modify: x.split('/')[2]
  - data: devices
    name: IP
    modify: x.split('/')[1]
  - data: devices
    name: MAC
    modify: x.split('/')[0]