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
Wow perfect! thanks for taking time helping! Much appriciated
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!
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
I will put your suggestion to the list of what to do
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 `-`
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
Opened
Good work!
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?
Possible, but it would become even uglier with code
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
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]