Extract JSON data to sensor

About the json data you posted, sadly u can’t retrieve the data in one go and use a template.
that was much easier… but with this data u can’t

i did alter my own code to fit within your code but it doesn’t work for you.

u will need to use the json_attributes_path:

ill check if i find out a way to do this.

It shouldn’t do. Try using the rest integration instead. At the top level:

rest:
  - resource: http://your-server:8123/local/log10.json
    sensor:
      - name: "Port 1"
        value_template: "{{ (value_json|selectattr('port','eq',1)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==1)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 2"
        value_template: "{{ (value_json|selectattr('port','eq',2)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==2)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 3"
        value_template: "{{ (value_json|selectattr('port','eq',3)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==3)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 4"
        value_template: "{{ (value_json|selectattr('port','eq',4)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==4)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 5"
        value_template: "{{ (value_json|selectattr('port','eq',5)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==5)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 6"
        value_template: "{{ (value_json|selectattr('port','eq',6)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==6)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 7"
        value_template: "{{ (value_json|selectattr('port','eq',7)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==7)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"
      - name: "Port 8"
        value_template: "{{ (value_json|selectattr('port','eq',8)|list|first)['state'] }}"
        json_attributes_path: "$.[?(@.port==8)]"
        json_attributes: 
          - "port"
          - "state"
          - "link_status"
          - "TxGoodPkt"
          - "TxBadPkt"
          - "RxGoodPkt"
          - "RxBadPkt"

what i’ve done in the meantime is edit my python code thats extracting the json and removing the first port reference and attributes. So port 2 is always at the top.

But I like your version more because I can see all the ports regardless. I’ll give it go now.

second problem is i’m using

pip install requests at command prompt each time I reboot. Any method to automate those command line installations at boot.

That should be a new topic.

well what u can do is this as well

rest:
  - authentication: basic
    resource_template: http://localhost:8123/local/test10.json
    method: GET
    scan_interval: 80000
    sensor:
    - name: tplink_p1
      unique_id: 8542222220572352
      json_attributes_path: "$.[0]"
      value_template: "ok"
      json_attributes:
        - "port" 
        - "state"
        - "link_status"
        - "TxGoodPkt"
        - "TxBadPkt"
        - "RxGoodPkt"
        - "RxBadPkt"
    - name: tplink_p2
      unique_id: 8542222220572353
      json_attributes_path: "$.[1]"
      value_template: "ok"
      json_attributes:
        - "port" 
        - "state"
        - "link_status"
        - "TxGoodPkt"
        - "TxBadPkt"
        - "RxGoodPkt"
        - "RxBadPkt"


you only need to change this line for each call

json_attributes_path: “$.[0]”

0=1
1=2
2=3

etc.

thanks what do these reference? just a random number?

i created just a random number, u can also just delete that line.
i use them in my code.

regards

1 Like

this worked perfectly. thank you.

1 Like

You could, assuming the ports are always in the correct order in the JSON.

My json_attributes_path and value_template (optional, but puts the port state in the sensor state rather than a blanket 'ok') actually look up the correct port, so if the device decides to mix up the order it’ll still work.

@dharvey24: Unique IDs are a good idea: I would use them in production and only omitted them here for brevity. I use this site to generate them: https://www.uuidgenerator.net/

They allow for customisation in the UI and assigning to an area.

1 Like

yes but according to his json output it does not put all the states in value_template.
so normally it stores the data in the sensor itself where i can use that sensor to create template sensors with my data.

that’s why i copied my code in the beginning to use template sensors instead when the data has been retrieved, but had no knowledge of how his json would look like.

But u right yours does have a sort of fail safe if the data decides to mix up, so if i was @dharvey24 in this case i would use your code as shown in post #22

in my instance it doesn’t mix up the results, it would be the same each and every time. However its useful to understand how this works for other applications. @Troon and @malosaa thank you for taking the time to assist. I’m now just sorting out the notifications which I can do. For those following to install the requests and bs4 modules for python I used an automation and shell_command line to install them on startup.