Hi all,
I tried to convert state_attr('sensor.network_scanner', 'devices')
into another format.
It is exactly {d['mac']: {'ip': d['ip'], 'name': d['name']} for d in device_list}
in Python.
For example, converting from
[
{
"ip": "192.168.0.1",
"mac": "AA:AA:AA:AA:AA:AA",
"name": "Device A",
},
{
"ip": "192.168.0.2",
"mac": "BB:BB:BB:BB:BB:BB",
"name": "Device B",
},
]
to
{
"AA:AA:AA:AA:AA:AA": {
"ip": "192.168.0.1",
"name": "Device A",
},
"BB:BB:BB:BB:BB:BB": {
"ip": "192.168.0.2",
"name": "Device B",
},
}
The closest script I could get is {{ device_list | groupby('mac') }}
but it is still not key by mac address.
[
[
"AA:AA:AA:AA:AA:AA",
[
{
"ip": "192.168.0.1",
"mac": "AA:AA:AA:AA:AA:AA",
"name": "Device A",
}
]
],
[
"BB:BB:BB:BB:BB:BB",
[
{
"ip": "192.168.0.2",
"mac": "BB:BB:BB:BB:BB:BB",
"name": "Device B",
}
]
],
]
Another trial is selecting each field first but I do not know how to merge them into dict.
{% set mac = device_list | map(attribute='mac') | list %} <--- list of MAC address
{% set ip = device_list | map(attribute='ip') | list %} <--- list of IP address
{{ ???? }}
Any idea will be appreciated. Thanks!