Device tracker sorting by ip

Hi there,

playing around for some time now I had the idea to ask my router for “home” devices after having trouble with nmap (wrong or missing names).

I have some code for a markdown card, but sorting by ip gives me a headache.

{%- for state in states.device_tracker
|selectattr('entity_id', 'match','device_tracker.*')  
|selectattr('state', 'eq', 'home')
|selectattr('attributes.source_type', 'eq', 'router')
|sort(attribute='attributes.ip')
-%}
{{state.attributes.ip}} | {{state.name|lower}} | {{state.attributes.mac}}
{% endfor %}

This works, but sorts in the wrong way.

192.168.111.1
192.168.111.100
192.168.111.2
192.168.111.20
192.168.111.21
192.168.111.24
192.168.111.254
192.168.111.28
192.168.111.29
192.168.111.3
192.168.111.36

and so on.

How can I split the ip address inside the for-loop and order them by the last octet?

Thanks.

IP addresses can actually be converted to integers, which could help sorting it better.

OK, I know. But how to in this case?

Ended up using a flexible table card.

type: custom:flex-table-card
strict: true
clickable: true
sort_by: ip+
title: Network Devices
entities:
  exclude:
    - unknown_device
  include: device_tracker.*
columns:
  - name: Name
    data: name
  - name: IP
    data: ip
  - data: state
    name: Status
    modify: (x == "home" && x) || null
    hidden: true

Nevertheless it would be nice to know the other solution :slight_smile:

Not sure if this is helpful but I managed to get this sorted:

{% set ips = ["192.168.111.1","192.168.111.100","192.168.111.2","192.168.111.20","192.168.111.21","192.168.111.24","192.168.111.254","192.168.111.28","192.168.111.29","192.168.111.3","192.168.111.36"] | map('regex_replace','(\.)','') | map('int') | sort | map('replace','192168111','192.168.111.') %}

{%- for ip in ips -%}
  {{ ip }}
{% endfor %}

I assume this can be adapted to your code.
I don’t have any entities with IPs so I can’t test it on my entities, I had to make a list.

Yes, thats clear, but i want a dynamic list and can not split the attribute in sort.