I’m hoping someone can assist, I’m attempting to setup home/floor based presence by determining what client is connected to what AP. However I am struggling some what with the template, please could someone assist?
What I would like to do is create a sensor that tells me what AP I am connected to and when I’m connected to no AP its shows in the front end as away or disconnected or something other than unknown. Below is the attributes that i have exposed from the Unifi APs
Filter Entity = device_tracker.google_pixel_xl_user]
State = home
Attributes =
source_type: router
latitude: 51.60615
longitude: -2.521689
gps_accuracy: 0
scanner: UnifiScanner
rx_rate: 400000
tx_rate: 400000
rssi: 35
signal: -61
noise: -106
radio_proto: ac
is_wired: false
ap_mac: 78:8a:20:4b:09:83
last_seen: 1542223810
uptime: 6186
ip: 10.0.20.17
oui: Htc f
friendly_name: Google Pixel XL User
I have created a template sensor that pulls the ap_mac address into the State and then I have created a further template sensor to identify which AP is which MAC address and to show as offline when no AP connected.
- platform: template
sensors:
unifi_connected_ap:
value_template: '{{states.device_tracker.google_pixel_xl_user.attributes.ap_mac}}'
user_connected_ap:
friendly_name: 'User associated to'
value_template:{% if is_state('sensor.unifi_connected_ap', 'b4:fb:e4:21:79:1c') %}
'OFFICE_WIFI'
{% elif is_state('sensor.unifi_connected_ap', '78:8a:20:4b:09:83') %}
'LIVING_ROOM_WIFI'
{% else %}
'Offline'
{% endif %}
However I am getting this error??
Configuration invalid
Error loading /config/configuration.yaml: while scanning for the next token found character '%' that cannot start any token in "/config/custom_config/sensors/network_sensors/unifi_presence.yaml", line 10, column 26
Please could someone point me in the right direction?
1 Like
Edit: Looks Like I have fixed it, I just needed to type it all out Just needed it change my quotes and then enclose it in single quotes.
value_template: '{% if is_state("sensor.unifi_connected_ap", "b4:fb:e4:21:79:1c") %}
"OFFICE_WIFI"
{% elif is_state("sensor.unifi_connected_ap", "78:8a:20:4b:09:83") %}
"LIVING_ROOM_WIFI"
{% else %}
"Offline"
{% endif %}'
I’m not sure if anyone can suggest any edits to condense the template down rather than having to create a sensor just to query the MAC address from the AP?
1 Like
Typically the way that is written is with multi-line YAML:
value_template: >
{% if is_state("sensor.unifi_connected_ap", "b4:fb:e4:21:79:1c") %}
"OFFICE_WIFI"
{% elif is_state("sensor.unifi_connected_ap", "78:8a:20:4b:09:83") %}
"LIVING_ROOM_WIFI"
{% else %}
"Offline"
{% endif %}
1 Like
So the next step would be to template this even further so that a sensor is created for each device that is:
- “home” and
- where the scanner attribute is “UnifiScanner”
so it doesn’t require definition of a (better: 2) sensor for each device separately.
But I guess that’s out of reach for the sensor definition itself.
@chairstacker Are you able to give any insight on how that might be achieved?
@pnbruckner Thanks for you comments, I have changed the config and learnt something, on a side note you don’t need the friendly names in quotes otherwise the “” show up in the front end
value_template: >
{% if is_state("sensor.user_unifi_ap", "b4:fb:e4:21:79:1c") %}
OFFICE_WIFI
{% elif is_state("sensor.user_unifi_ap", "78:8a:20:4b:09:83") %}
LIVING_ROOM_WIFI
{% else %}
Offline
{% endif %}
Ah, yes, of course. I just copied that part verbatim since I was focusing on the logic and the use of multi-line YAML. Should have noticed that.
@pnbruckner Now I’m attempting to put this in a single sensor using map()
This code passes the ‘Check Files’ in Hass but just returns unknown in the front end… Work in progress
value_template: >
{% if state(states.device_tracker.google_pixel_xl.attributes.ap_mac | map('b4:fb:e4:21:79:1c')) %}
OFFICE_WIFI
{% elif is_state(states.device_tracker.google_pixel_xl.attributes.ap_mac | map('78:8a:20:4b:09:83')) %}
LIVING_ROOM_WIFI
{% else %}
Offline
Not sure what I’m missing yet… More documentation to read
I believe what you’re looking for is:
value_template: >
{% if is_state_attr("device_tracker.google_pixel_xl", "ap_mac", "b4:fb:e4:21:79:1c") %}
OFFICE_WIFI
{% elif is_state_attr("device_tracker.google_pixel_xl", "ap_mac", "78:8a:20:4b:09:83") %}
LIVING_ROOM_WIFI
{% else %}
Offline
{% endif %}
2 Likes
Pretty sure this is not possible straight away, I’d be surprised if you could define sensors ‘on the fly’, i.e. based on a list of known_devices that might change.
But I think it should be possible to create a ‘user_connected_ap’ sensor for each device that’s supposed to be tracked, e.g. as an input_text, and then assign the WAP it’s associated to based on a highly templated automation that’s triggered by a change of the ap_mac attribute.
Maybe the last part could even be done using conditional cards in Lovelace.
But that’s all beyond my capabilities at the moment.
@pnbruckner You are Amazing!!! Thank you very much
That is working perfectly and now I know how to pull attributes from a sensor I can go away and template lots of things. Next on the list is floor presence based off of AP and RSSI
1 Like
Hi @noodlemctwoodle and @pnbruckner,
I have set this up in my system, but was having an issue when a device goes offline - it seemed to be hanging on to the last AP it’s been logged onto, i.e. it never went to status ‘Offline’. This created some strange issues when I started showing the results of the query above in my Lovelace interface:
# Show which device is connected to which WAP
- type: custom:monster-card
show_empty: false
card:
type: glance
title: OFFICE_WIFI
show_state: false
show_name: false
column_width: 20%
filter:
include:
- entity_id: "*user_connected_ap*"
state: "OFFICE_WIFI"
- type: custom:monster-card
show_empty: false
card:
type: glance
title: LIVING_ROOM_WIFI
show_state: false
show_name: false
column_width: 20%
filter:
include:
- entity_id: "*user_connected_ap*"
state: "LIVING_ROOM_WIFI"
I have wrapped the code above in a few lines that fix the problem:
value_template: >
{% if is_state("device_tracker.google_pixel_xl", "home") %}
{% if is_state_attr("device_tracker.google_pixel_xl", "ap_mac", "b4:fb:e4:21:79:1c") %}
OFFICE_WIFI
{% elif is_state_attr("device_tracker.google_pixel_xl", "ap_mac", "78:8a:20:4b:09:83") %}
LIVING_ROOM_WIFI
{% else %}
Offline
{% endif %}
{% else %}
Offline
{% endif %}