Extract JSON data to sensor

To say i’m lost is an understatement.

I have some JSON data which i believe is correctly formatted:

{
    "port": 1,
    "state": 1,
    "link_status": 6,
    "TxGoodPkt": 145382505,
    "TxBadPkt": 0,
    "RxGoodPkt": 354555444,
    "RxBadPkt": 0
  },
  {
    "port": 2,
    "state": 1,
    "link_status": 6,
    "TxGoodPkt": 141498890,
    "TxBadPkt": 0,
    "RxGoodPkt": 62863391,
    "RxBadPkt": 0
  },
  {
    "port": 3,
    "state": 1,
    "link_status": 0,
    "TxGoodPkt": 0,
    "TxBadPkt": 0,
    "RxGoodPkt": 0,
    "RxBadPkt": 0
  },
  {
    "port": 4,
    "state": 1,
    "link_status": 0,
    "TxGoodPkt": 0,
    "TxBadPkt": 0,
    "RxGoodPkt": 0,
    "RxBadPkt": 0
  },
  {
    "port": 5,
    "state": 1,
    "link_status": 0,
    "TxGoodPkt": 0,
    "TxBadPkt": 0,
    "RxGoodPkt": 0,
    "RxBadPkt": 0
  },
  {
    "port": 6,
    "state": 1,
    "link_status": 0,
    "TxGoodPkt": 0,
    "TxBadPkt": 0,
    "RxGoodPkt": 0,
    "RxBadPkt": 0
  },
  {
    "port": 7,
    "state": 1,
    "link_status": 6,
    "TxGoodPkt": 1090698305,
    "TxBadPkt": 0,
    "RxGoodPkt": 628825401,
    "RxBadPkt": 0
  },
  {
    "port": 8,
    "state": 1,
    "link_status": 6,
    "TxGoodPkt": 617099708,
    "TxBadPkt": 0,
    "RxGoodPkt": 915982349,
    "RxBadPkt": 0
  }
]

I would like to extract just the port 1, link status data to a sensor attribute.

I have so far:

 - platform: file
    file_path: /config/python_scripts/log10.json
    name: tplinkswitch
    value_template: '{{ value_json["1"][1]["link_status"]}}'

All help is appreciated.

hmm, try this one:

sensor:
  - platform: rest
    resource: http://your-server/log10.json  # Replace with the correct URL
    name: tplinkswitch
    value_template: "{{ value_json[0].link_status }}"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt

the thing is u cant extract data from deep nested data, you need to create or work with template sensors.

regards :slight_smile:

many thanks

my json file is in the config/python_scripts folder - would that need to be moved to the www folder?

yes, that should work.

i did that too with testing my json data retrieval.
should look like this:

http://your-server:8123/local/log10.json

worked a treat!! thanks

i’m glad i could help ^^

have a nice day !

one final question, what would I need to change in

value_template: "{{ value_json[0].link_status }}"

To get the data for port 2? I tried:

value_template: "{{ value_json[1].link_status }}"

But it returns the same information

That will be fine if you can guarantee that the port 1 data is first in the list. If you can’t, use this:

    value_template: "{{ (value_json|selectattr('port','eq',1)|list|first)['link_status'] }}"

you can also do it very different, do you have the json information?
if u can post it here so i can create you what you need for all entities.

[{"port": 1, "state": 1, "link_status": 6, "TxGoodPkt": 145382505, "TxBadPkt": 0, "RxGoodPkt": 354555444, "RxBadPkt": 0}, {"port": 2, "state": 1, "link_status": 6, "TxGoodPkt": 141498890, "TxBadPkt": 0, "RxGoodPkt": 62863391, "RxBadPkt": 0}, {"port": 3, "state": 1, "link_status": 0, "TxGoodPkt": 0, "TxBadPkt": 0, "RxGoodPkt": 0, "RxBadPkt": 0}, {"port": 4, "state": 1, "link_status": 0, "TxGoodPkt": 0, "TxBadPkt": 0, "RxGoodPkt": 0, "RxBadPkt": 0}, {"port": 5, "state": 1, "link_status": 0, "TxGoodPkt": 0, "TxBadPkt": 0, "RxGoodPkt": 0, "RxBadPkt": 0}, {"port": 6, "state": 1, "link_status": 0, "TxGoodPkt": 0, "TxBadPkt": 0, "RxGoodPkt": 0, "RxBadPkt": 0}, {"port": 7, "state": 1, "link_status": 6, "TxGoodPkt": 1090698305, "TxBadPkt": 0, "RxGoodPkt": 628825401, "RxBadPkt": 0}, {"port": 8, "state": 1, "link_status": 6, "TxGoodPkt": 617099708, "TxBadPkt": 0, "RxGoodPkt": 915982349, "RxBadPkt": 0}]

just for context - the port 2 on the switch keeps defaulting to 100mf rather than 1000mf. I want to set up a notification for when it changes so i can try to understand what else is happening or has happened to cause it. Runs on 1000mf full duplex without issue for days and then randomly reverts to 100mf.

This provides the same data for each entity?

  - platform: rest
    resource: http://xxx.xxx.xxx.xxx:8123/local/log10.json
    name: tplinkswitch
    value_template: "{{ (value_json|selectattr('port','eq',1)|list|first)['link_status'] }}"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt
  - platform: rest
    resource: http://xxx.xxx.xxx.xxx:8123/local/log10.json
    name: tplinkswitch1
    value_template: "{{ (value_json|selectattr('port','eq',2)|list|first)['link_status'] }}"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt

U can try this :slight_smile:

sensor:
  - platform: rest
    resource: http://your-server:8123/local/log10.json
    name: your_sensor_name
    json_attributes_path: "$[*]"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt

sensor:
  - platform: template
    sensors:
      port:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['port'] }}"
      state:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['state'] }}"
      link_status:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['link_status'] }}"
      TxGoodPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['TxGoodPkt'] }}"
      TxBadPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['TxBadPkt'] }}"
      RxGoodPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['RxGoodPkt'] }}"
      RxBadPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', '0')['RxBadPkt'] }}"

or this one

sensor:
  - platform: rest
    resource: http://your-server:8123/local/log10.json
    name: your_sensor_name
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt

sensor:
  - platform: template
    sensors:
      port:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'port') }}"
      state:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'state') }}"
      link_status:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'link_status') }}"
      TxGoodPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'TxGoodPkt') }}"
      TxBadPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'TxBadPkt') }}"
      RxGoodPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'RxGoodPkt') }}"
      RxBadPkt:
        value_template: "{{ state_attr('sensor.your_sensor_name', 'RxBadPkt') }}"

i will check your question after this works :slight_smile:

here the automation:

automation:
  - alias: Notify Port Speed Change
    initial_state: true
    trigger:
      - platform: state
        entity_id: sensor.your_switch_port_speed  # Replace with your actual sensor e.g. the template sensor we just created.
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
      - condition: template
        value_template: "{{ trigger.to_state.state == '100MF' }}"
    action:
      - service: notify.notify  # Replace with your preferred notification service
        data:
          message: "Port speed changed to 100MF."

  - alias: Reset Port Speed Change Notification
    initial_state: true
    trigger:
      - platform: state
        entity_id: sensor.your_switch_port_speed  # Replace with your actual sensor
    condition:
      - condition: template
        value_template: "{{ trigger.to_state.state == '1000MF' }}"
    action:
      - service: notify.notify  # Replace with your preferred notification service
        data:
          message: "Port speed reverted to 1000MF."

This fails. State unknown. Log suggests the sensor is limited to 255 characters.

  - platform: rest
    resource: http://192.168.1.37:8123/local/log10.json
    name: tplinkswitch
    json_attributes_path: "$[*]"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt
  - platform: template
    sensors:
      port:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['port'] }}"
      state:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['state'] }}"
      link_status:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['link_status'] }}"
      TxGoodPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['TxGoodPkt'] }}"
      TxBadPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['TxBadPkt'] }}"
      RxGoodPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['RxGoodPkt'] }}"
      RxBadPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch', '0')['RxBadPkt'] }}"
  - platform: rest
    resource: http://192.168.1.37:8123/local/log10.json
    name: tplinkswitch1
    json_attributes_path: "$[*]"
    json_attributes:
      - port
      - state
      - link_status
      - TxGoodPkt
      - TxBadPkt
      - RxGoodPkt
      - RxBadPkt
  - platform: template
    sensors:
      port:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['port'] }}"
      state:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['state'] }}"
      link_status:
        value_template: "{{ state_attr('sensor.tplinkswitc1h', '1')['link_status'] }}"
      TxGoodPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['TxGoodPkt'] }}"
      TxBadPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['TxBadPkt'] }}"
      RxGoodPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['RxGoodPkt'] }}"
      RxBadPkt:
        value_template: "{{ state_attr('sensor.tplinkswitch1', '1')['RxBadPkt'] }}"

ok i will try now with your json.

i will post the full working code.

You need to add a value_template to the rest sensor, otherwise its state is the complete response. Something like:

value_template: "OK"

Why not just:

automation:
  - alias: Notify Port Speed Change
    initial_state: true
    trigger:
      - platform: state
        entity_id: sensor.your_switch_port_speed  # Replace with your actual sensor e.g. the template 
        to: "100MF"

Avoids two unnecessary template conditions. You’re not using AI to generate these responses are you?

No i’m not.
this is how i setup my automations all the time, i just copy pasted mine and edited it.
why u ask ?

there are so many ways just to create an automation, it just depends how you like it…