Fetch sensor data from JSON, local URL


Im trying to get sensors from my PoE switch which delivers status with json, but i cant figure out how to do it.
I would like to get sensors for each port, with as minimal configuration as possible.

Any hints?

The Json is fecthed from a URL in my LAN and is in this format:

[1,'1',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[2,'2',' ','Yes','disabled','low','usage',17,4.06,'','Delivering','0','80','0','1'],
[3,'3',' ','Yes','disabled','low','usage',17,1.30,'','Delivering','0','80','0','1'],
[4,'4',' ','Yes','disabled','low','usage',17,5.40,'','Delivering','0','80','0','1'],
[5,'5',' ','Yes','disabled','low','usage',17,3.00,'','Delivering','3','80','0','1'],
[6,'6',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[7,'7',' ','Yes','disabled','low','usage',17,2.80,'','Delivering','3','80','0','1'],
[8,'8',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[9,'9',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[10,'10',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[11,'11',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[12,'12',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[13,'13',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[14,'14',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[15,'15',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[16,'16',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[17,'17',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[18,'18',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[19,'19',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[20,'20',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[21,'21',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[22,'22',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[23,'23',' ','No','disabled','low','usage',17,0.00,'','Disabled','0','80','0','1'],
[24,'24',' ','Yes','disabled','low','usage',17,4.20,'','Delivering','0','80','0','1']

Yuk. Whoever designed that doesn’t understand structured data (EDIT: or the JSON spec). Do you happen to know what each item in the list for each port is, and which ones do you want?

Example, making some assumptions:

  - resource: YOUR_URL
    scan_interval: 60
      - name: Port 1 power
        value_template: "{{ (value.translate({39:34})|from_json)[0][8] }}"
        unit_of_measurement: 'W'
      - name: Port 2 power
        value_template: "{{ (value.translate({39:34})|from_json)[1][8] }}"
        unit_of_measurement: 'W'
      - name: Switch total port power
        value_template: "{{ (value.translate({39:34})|from_json)|map(attribute=8)|sum|round(2) }}"
        unit_of_measurement: 'W'
      - name: Switch ports delivering count
        value_template: "{{ (value.translate({39:34})|from_json)|selectattr(10,'eq','Delivering')|list|count }}"

The first two sensors above assume the JSON is always published in port order. If you don’t want to rely on that, and assuming the first item in the inner lists is the port number, you can instead use:

        value_template: "{{ ((value.translate({39:34})|from_json)|selectattr(0,'eq',1)|first)[8] }}"

…with the 1 being the port number.

This is a situation where the json_attributes_template functionality of the MQTT sensor would be useful, allowing you to transform that unstructured list into a dictionary, in turn allowing you to extract the other values into attributes. Unfortunately, that feature is only on the MQTT sensor and the boss devs have indicated that there’s unlikely to be a change to that situation.

EDIT: updated the above away from value_json having actually tried it in a real rest sensor rather than via the template editor, which is less fussy. The single quotes were causing issues, which the translate fixes: it’s a shorthand replacement of single with double quotes.

You can transform that JSON anyway you like without MQTT. Use command line sensor and JQ to parse JSON to something more desired.

Awesome, exactly what i was looking for.
Yep, that JSON is not very nice. But its not the latest equipment, its a HP 2520 24G PoE, a really nice switch in all other aspects. Perhaps JSON was not as well documented and structured when the switch was built.

Until now i had an external script parsing the data and sending it to MQTT, but its a bit messy and i dont want external solutions.

Since i already have an external script i know how the data is structured.


Ps. where can i read up on the translate() function?

1 Like