Extract json data from command_line / rest sensor

I would like to have a sensor which displays the files on the SD card of my 3D printer, but I can’t figure out how. I can make an api request which outputs the following:

{
  "files": [
    {
      "date": 1591453558,
      "display": "CE5_Pi_Cam_Z_Endstop.gcode",
      "gcodeAnalysis": {
        "dimensions": {
          "depth": 180.0,
          "height": 34.92,
          "width": 166.70000000000002
        },
        "estimatedPrintTime": 7536.558174762483,
        "filament": {
          "tool0": {
            "length": 4016.72203,
            "volume": 0.0
          }
        },
        "printingArea": {
          "maxX": 176.8,
          "maxY": 200.0,
          "maxZ": 35.04,
          "minX": 10.1,
          "minY": 20.0,
          "minZ": 0.12
        }
      },
      "hash": "06c9ae65ec729df8ab4b8b03ac0a63c973e62879",
      "name": "CE5_Pi_Cam_Z_Endstop.gcode",
      "origin": "local",
      "path": "CE5_Pi_Cam_Z_Endstop.gcode",
      "prints": {
        "failure": 1,
        "last": {
          "date": 1591453578.822522,
          "success": false
        },
        "success": 0
      },
      "refs": {
        "download": "http://192.168.178.39/downloads/files/local/CE5_Pi_Cam_Z_Endstop.gcode",
        "resource": "http://192.168.178.39/api/files/local/CE5_Pi_Cam_Z_Endstop.gcode"
      },
      "size": 2277366,
      "statistics": {
        "averagePrintTime": {},
        "lastPrintTime": {}
      },
      "type": "machinecode",
      "typePath": [
        "machinecode",
        "gcode"
      ]
    },
    {
      "date": 1591379374,
      "display": "CE5_xyzCalibration_cube.gcode",
      "gcodeAnalysis": {
        "dimensions": {
          "depth": 180.0,
          "height": 19.9,
          "width": 121.298
        },
        "estimatedPrintTime": 2384.219693455974,
        "filament": {
          "tool0": {
            "length": 1589.3133400000038,
            "volume": 0.0
          }
        },
        "printingArea": {
          "maxX": 131.398,
          "maxY": 200.0,
          "maxZ": 20.0,
          "minX": 10.1,
          "minY": 20.0,
          "minZ": 0.1
        }
      },
      "hash": "13671a6c21c07b538913021168d612d3f09f6928",
      "name": "CE5_xyzCalibration_cube.gcode",
      "origin": "local",
      "path": "CE5_xyzCalibration_cube.gcode",
      "prints": {
        "failure": 5,
        "last": {
          "date": 1591445605.652215,
          "success": false
        },
        "success": 0
      },
      "refs": {
        "download": "http://192.168.178.39/downloads/files/local/CE5_xyzCalibration_cube.gcode",
        "resource": "http://192.168.178.39/api/files/local/CE5_xyzCalibration_cube.gcode"
      },
      "size": 708151,
      "statistics": {
        "averagePrintTime": {},
        "lastPrintTime": {}
      },
      "type": "machinecode",
      "typePath": [
        "machinecode",
        "gcode"
      ]
    }
  ],
  "free": 27635871744,
  "total": 31120502784
}

Now I would like to collect all the data of the ‘display’ dictionary, so the sensor would output something like this:

- CE5_Pi_Cam_Z_Endstop.gcode,
- CE5_xyzCalibration_cube.gcode

I’ve tried to create these sensors but they only output one dictonary (read: CE5_Pi_Cam_Z_Endstop.gcode or CE5_xyzCalibration_cube.gcode instead of both)

  - platform: command_line
    command: "curl http://192.168.178.39/api/files -H 'X-Api-Key: XXX'"
    name: "Octoprint files list6"
    value_template: "{{ value_json.files[1].display }}"

  - platform: command_line
    command: "curl http://192.168.178.39/api/files -H 'X-Api-Key: XXX'"
    name: "Octoprint files list7"
    value_template: "{{ value_json.files[2].display }}"

Unfortunately it was not possible to create a template sensor beacause the output of the original sensor exceeded the maximum of 255 characters. Os is there a way to bypass the maximum characters in a sensor of 255?

How about something like this:

  - platform: command_line
    command: "curl http://192.168.178.39/api/files -H 'X-Api-Key: XXX'"
    name: "Octoprint files list6"
    value_template: >
      {% for file in value_json.files -%}
      {% if not loop.first %}, {% endif %}{{ file.display }}
      {%- endfor %}
1 Like

Worked like a charm, thanks for your reply!

1 Like