Dear all,
I’m trying to create an MQTT template sensor to display the connected hosts on my home network in HA. I prepared a script that sends data (generated by nmap and converted to JSON using ) via MQTT in JSON format. This part is working fine.
However, in the Developer Tools - Template editor I can’t seem to extract the correct information from it: Since the ‘nmap’ output is not uniform for each host, I need to do some validation on them. Here is a sample JSON and my Jinja script (which is not working):
{%
set myData =
{
"host": [
{
"status": {
"@state": "up",
"@reason": "arp-response",
"@reason_ttl": "0"
},
"address": [
{
"@addr": "192.168.1.5",
"@addrtype": "ipv4"
},
{
"@addr": "64:64:4A:E2:DF:94",
"@addrtype": "mac"
}
],
"hostnames": null,
"times": {
"@srtt": "894",
"@rttvar": "5000",
"@to": "100000"
}
},
{
"status": {
"@state": "up",
"@reason": "arp-response",
"@reason_ttl": "0"
},
"address": [
{
"@addr": "192.168.1.10",
"@addrtype": "ipv4"
},
{
"@addr": "00:15:99:85:84:A5",
"@addrtype": "mac",
"@vendor": "Samsung Electronics"
}
],
"hostnames": {
"hostname": {
"@name": "Samsung.local",
"@type": "PTR"
}
},
"times": {
"@srtt": "664",
"@rttvar": "5000",
"@to": "100000"
}
},
{
"status": {
"@state": "up",
"@reason": "arp-response",
"@reason_ttl": "0"
},
"address": [
{
"@addr": "192.168.1.82",
"@addrtype": "ipv4"
},
{
"@addr": "B8:27:EB:DB:E3:34",
"@addrtype": "mac",
"@vendor": "Raspberry Pi Foundation"
}
],
"hostnames": {
"hostname": {
"@name": "chopin.local",
"@type": "PTR"
}
},
"times": {
"@srtt": "728",
"@rttvar": "5000",
"@to": "100000"
}
},
{
"status": {
"@state": "up",
"@reason": "arp-response",
"@reason_ttl": "0"
},
"address": [
{
"@addr": "192.168.1.222",
"@addrtype": "ipv4"
},
{
"@addr": "00:24:8C:66:7F:54",
"@addrtype": "mac",
"@vendor": "Asustek Computer"
}
],
"hostnames": {
"hostname": {
"@name": "test.local",
"@type": "PTR"
}
},
"times": {
"@srtt": "675",
"@rttvar": "5000",
"@to": "100000"
}
},
{
"status": {
"@state": "up",
"@reason": "localhost-response",
"@reason_ttl": "0"
},
"address": {
"@addr": "192.168.1.111",
"@addrtype": "ipv4"
},
"hostnames": {
"hostname": {
"@name": "havm.local",
"@type": "PTR"
}
}
}
]
}
%}
{% for host in myData.host %}
{% if host.hostnames.hostname is defined %}
{% set hostName = host.hostnames.hostname['@name'] %}
{% else %}
{% set hostName = "-" %}
{% endif %}
{% for address in host.address %}
{% set a = address | first %}
{{ a }}
{% endfor %}
{{ hostName }} - {{ ipv4Address | default ("***") }}
{% endfor %}
The specific problem is that most hosts have their ip address in a list format, i.e.:
"address": [
{
"@addr": "192.168.1.222",
"@addrtype": "ipv4"
},
{
"@addr": "00:24:8C:66:7F:54",
"@addrtype": "mac",
"@vendor": "Asustek Computer"
}
],
but the last one is just this much:
"address": {
"@addr": "192.168.1.111",
"@addrtype": "ipv4"
},
How can I test for, say, the type of myData.host[x].address
?
Also: why does this work in itself (in Template editor):
{{ myData.host[3].address[0]['@addr'] }}
{{ myData.host[4].address['@addr'] }}
but not when iterating through them later on?
Any pointer to the right direction is much appreciated!!