Octoprint: add link to last recorded timelapse

A nice addition to the Octoprint component would be to add the link to the last recorded timelapse (e.g. http://ip/downloads/timelapse/filename.mpg) in the attributes or as an entity, so the timelapse can be used to send a notification with video after completion of the print job.

How does Octoprint store the images? I have it up and running, but haven’t attached a camera yet. Octoprint is running on my Ubuntu server and the printer is connected directly to that server.

looked further into it. The timelapses are available through the API (http://docs.octoprint.org/en/master/api/timelapse.html). The returned JSON of /api/timelapse looks like this:

{
  "config": {
    "fps": 25, 
    "minDelay": 5.0, 
    "postRoll": 0, 
    "retractionZHop": 0.0, 
    "type": "zchange"
  }, 
  "enabled": true, 
  "files": [
    {
      "bytes": 458752, 
      "date": "2019-08-02 22:10", 
      "name": "CE3_cc2531_20190802213732.mpg", 
      "size": "448.0KB", 
      "url": "/downloads/timelapse/CE3_cc2531_20190802213732.mpg"
    }, 
    {
      "bytes": 442368, 
      "date": "2019-08-02 21:24", 
      "name": "CE3_cc2531_20190802205509.mpg", 
      "size": "432.0KB", 
      "url": "/downloads/timelapse/CE3_cc2531_20190802205509.mpg"
    }, 
    {
      "bytes": 1794048, 
      "date": "2019-08-01 03:09", 
      "name": "CE3_cc2531_double_20190731183238.mpg", 
      "size": "1.7MB", 
      "url": "/downloads/timelapse/CE3_cc2531_double_20190731183238.mpg"
    }, 
    {
      "bytes": 1800192, 
      "date": "2019-08-02 23:10", 
      "name": "CE3_cc2531_20190802221102.mpg", 
      "size": "1.7MB", 
      "url": "/downloads/timelapse/CE3_cc2531_20190802221102.mpg"
    }
  ]
}

For some reason Octoprint doesn’t order the timelapses by default, so we have to sort when we get the json value.

To get the link to the latest timelapse put this in sensors.yaml

- platform: rest
  name: Octoprint Timelapse URL
  resource: http://octopi.local/api/timelapse?apikey=YOUR_OCTOPRINT_API_KEY
  value_template: "{{(value_json.files | sort(attribute='date'))[-1].url }}"

- platform: template
  sensors:
    octoprint_timelapse_url_full:
      value_template: >-
        {% set url = states.sensor.octoprint_timelapse_url.state %}
        {% set prefix = "http://IPADDRESS" %}
        {% set suffix = "?apikey=YOUR_OCTOPRINT_API_KEY" %}
        {{ [prefix, url, suffix] | join("") }}

# Optional if you want to use this info also
- platform: rest
  name: Octoprint Timelapse Name
  resource: http://octopi.local/api/timelapse?apikey=YOUR_OCTOPRINT_API_KEY
  value_template: "{{ (value_json.files | sort(attribute='date'))[-1].name  }}"

- platform: rest
  name: Octoprint Timelapse Date
  resource: http://octopi.local/api/timelapse?apikey=YOUR_OCTOPRINT_API_KEY
  value_template: "{{(value_json.files | sort(attribute='date'))[-1].date }}"

This will give you a full url at sensor.octoprint_timelapse_url_full, theoretically you should be able to push that to a camera, but I just download as is so I’m not sure of the camera code.

1 Like