I’m trying to finetune fing API output to my need. In particular there is a problem with easy sorting by IP address of device. I would like to solve it by adding to original json output one node with sort order (actually just lasy segment of IP address, but as initeger). Currentlu output of fing sensor looks like that (credit goes to @RoyOltmans for his work on fing):
I’d like to replicate this takinig as source data from this sensor and adding one more key to json output, that would be just last segment of IP address. For this I created following template:
As you can see it renders proper json oputput with exception of adding ‘order’ key and translating list of IP addresses to single address.
BUT this just looks like valid json, this is text, not json object that could be easily iterated in template. Seems there is some extra step missing with conversion. I found some references to jinja filters tojson
of to_json
or even from_json
but using them either renders error or changes output of template to something for sure different than json.
Here is full sensor code for reference:
- name: ordered_network_devices
state: 'on'
attributes:
details: >
{% set dev_list = namespace(devs = '') %}
{% set json_string = '' %}
{% set devices = state_attr('sensor.all_network_devices', 'devices') %}
{%- for device in devices -%}
{%- set order = '{"order":"' + (device.ip[0] | default('N/A')).split('.')[3] + '",' -%}
{%- set mac = '"mac":"' + (device.mac | default('N/A')) + '",' -%}
{%- set ip = '"ip":"' + (device.ip[0] | default('N/A')) + '",' -%}
{%- set state = '"state":"' + (device.state | default('N/A')) + '",' -%}
{%- set name = '"name":"' + (device.name | default('N/A')) + '",' -%}
{%- set type = '"type":"' + (device.type | default('N/A')) + '",' -%}
{%- set first_seen = '"firts_seen":"' + (device.first_seen | default('N/A')) + '",' -%}
{%- set last_changed = '"last_changed":"' + (device.last_changed | default('N/A')) + '"' -%}
{%- set json_string = json_string + order + mac + ip + state + name + type + first_seen + last_changed + '},' -%}
{% set dev_list.devs = dev_list.devs + json_string %}
{% endfor %}
{{ dev_list.devs }}
and output it renders in developer toole:
Any hionts on how to get around this problem?