I haven’t really seen anybody else post a method for classifying and tracking network devices like this so I thought I’d throw mine out there. I’d love to see other solutions for this so if you have 'em lay 'em on me please! I imagine someone is going to show me something that I wish I would have known before I went through all this…lol.
It is a bit cumbersome, but it works really well for me as it’s only my wife and I so we have a pretty stable set of device trackers. This could get hairy if you have a lot of devices or new devices popping up all the time. Part of the issue is that HA must be restarted to register any changes in the known_devices.yaml file. Hopefully this is a requirement that will improve in the future.
As an aside - if anyone could point out any discussions as to the direction device tracker discovery is going can you point them out please! I’d like to sandbag them. All I know is they are moving away from the known_devices.yaml file?
First a couple of screenshots so you can see what I’m trying to do.
Step 1. Name your and configure all your devices appropriately in known_devices.yaml and/or where your device is set up in your configuration.yaml file.
Example entry in known_device.yaml
dining_room_display_wifi:
icon: mdi:monitor-speaker
mac: 7C:D9:5C:14:C8:2F
name: Dining Room Display
picture:
track: true
Step 2. Create an entry for each device tracker entity you want to monitor to customize.yaml. You should have an entry for every device where track: = true. This is where you will create the create the custom entity attribute that will categorize your device_trackers. Because I use packages I was able to do this in a separate file to simplify things.
I use a few categories to classify my device trackers (Wired, Wireless, Bluetooth, Location, Ping). You can change these to whatever makes sense to you.
Example entry in customize.yaml
device_tracker.dining_room_display_wifi:
device_type: wireless
Step 3. Create template sensors to track each category of device tracker, plus sensors for unknown devices.
sensor:
- platform: template
sensors:
unknown_devices_total:
unique_id: unknown_devices_total
icon_template: mdi:router-wireless
value_template: "{{ states.device_tracker|selectattr('attributes.device_type','eq',null)|list|count }}"
attribute_templates:
devices: "{{ states.device_tracker|selectattr('attributes.device_type','eq',null)|map(attribute='entity_id')|list }}"
unknown_devices_online:
friendly_name: Unknown Devices
unique_id: unknown_devices_online
icon_template: mdi:devices
value_template: >
{{ states.device_tracker|selectattr('attributes.device_type','eq',null)
|selectattr('state','eq','home')|list|count -}}/{{- states('sensor.unknown_devices_total') }}
wireless_devices_total:
unique_id: wireless_devices_total
icon_template: mdi:router-wireless
value_template: "{{ states.device_tracker|selectattr('attributes.device_type','eq','wireless')|list|count }}"
wireless_devices_online:
friendly_name: Wireless Devices
unique_id: wireless_devices_online
icon_template: mdi:router-wireless
value_template: >
{{ states.device_tracker|selectattr('attributes.device_type','eq','wireless')
|selectattr('state','eq','home')|list|count -}}/{{- states('sensor.wireless_devices_total') }}
Step 4. Create a template binary sensor to trigger the alert.
binary_sensor:
- platform: template
sensors:
## Network Device Alert (unknown/new device)
network_device_alert:
unique_id: network_device_alert
value_template: "{{ states('sensor.unknown_devices_total')|int > 0 }}"
Step 5: Create an alert to notify you when there is a new device detected.
alert:
unknown_device:
name: Unknown Device
title: Unknown Network Device
message: >
{%- if states.sensor.unknown_devices_total.attributes.devices is defined -%}
{%- if states('sensor.unknown_devices_total')|int == 0 -%}
No unknown devices.
{%- else -%}
{%- for device in state_attr('sensor.unknown_devices_total','devices') -%}
- {{ device }}{%- if not loop.last -%}<br>{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
entity_id: binary_sensor.network_device_alert
state: 'on'
repeat: 60
can_acknowledge: true
skip_first: false
notifiers: mobile_app_jphone
Optional - Lovelace
To get your sensors to display like the screenshots at the beginning of this post you need two custom components. Add them using HACS.
These rows can then be added to added any entities card.
Example row for a device category.
type: custom:auto-entities
card:
type: custom:fold-entity-row
head: sensor.wireless_devices_online
sort:
method: state
ignore_case: true
filter:
include:
- attributes:
device_type: 'wireless'
options:
secondary_info: last-changed
Example entity row for unknown devices:
type: custom:auto-entities
card:
type: custom:fold-entity-row
head: sensor.unknown_devices_online
sort:
method: state
ignore_case: true
filter:
include:
- domain: device_tracker
options:
secondary_info: last-changed
exclude:
- attributes 1:
device_type: wired
- attributes 2:
device_type: wireless
- attributes 3:
device_type: location
- attributes 4:
device_type: ping
- attributes 5:
device_type: bluetooth
All put together in a package. Of course you’ll have to rename all the entities to your own.