Dynamic Picture Card

I am working on setting up octoprint. It is working well, but I have set up octoprint with the prusa slicer thumbnails, so I thought I would try pointing a picture card at the thumbnail and it actually works, kind of:

type: picture
image: >-
  http://192.168.1.112/plugin/prusaslicerthumbnails/thumbnail/printed_file.png
tap_action:
  action: none
hold_action:
  action: none

That shows the image, but the currently printing object isn’t always printed_file it is sensor.octoprint_print_file. And that file has .gcode at the end, instead of .png.

So here is the question, How do I make the image: parameter a template? I don’t see anything about it, and the little thing I tried did not work. (I tried just wrapping it with {{}}).

Am I missing something simple?

So, I figured this out a moment after I posted this. Here’s what I’m doing.

  1. I configured a camera with a snapshot url, which can point to a template:
# Testing a camera of the current prusa thumbnail
camera:
  - platform: generic
    name: "Wanhao Print Thumbnail"
    still_image_url: '{{ "http://192.168.1.112/plugin/prusaslicerthumbnails/thumbnail/" + states("sensor.octoprint_print_file").split(".")[0] + ".png" }}'
    limit_refetch_to_url_change: true
  1. I configured a picture entity card with the printing status and the camera feed (I might put this into a conditional, but I kind of like seeing it all the time):
type: picture-entity
camera_image: camera.wanhao_print_thumbnail
entity: binary_sensor.octoprint_printing
show_name: true

It looks pretty good:

Once it was done:

1 Like

How are you getting the sensor.octoprint_print_file? I don’t see any sensor from the Octoprint Integration that shows the currently printing file.

2 Likes

IIRC, I have a plugin in octoprint for publishing to mqtt. I also have a plugin called “home assistant discovery” in octoprint.

Thanks for the guidance. I got MQTT setup and working with my octoprint.

I did have trouble getting the camera to show anything. So I was messing around with the templet editor in the Developer tools and realized that my Prusa Slicer was outputting names with “0.2” or “0.15” based on the layer height. so the .split(".")[0] in the template was cutting off the rest of the filename after the first period.

For example:
File name cube_0.15mm_PETG_1h18m.gcode would become cube_0

However if I changed it to .split(".")[1] it would remove the first period but still remove the “.gcode” rusulting in cube_015mm_PETG_1h18m except it would not match up to the file on my octoprint.

After doing some searching I figured out that I could use the replace function instead of split. That way I could just replace the “gcode” with “png”.

This is how my camera is configured now:

camera:
- platform: generic
  name: "3D Print Thumbnail"
  still_image_url: http://192.168.1.105/plugin/prusaslicerthumbnails/thumbnail/{{ states("sensor.octoprint_print_file") | replace('gcode','png') }}
  limit_refetch_to_url_change: true

Thanks for the original post to get me started. Now the next project is to get the synced up the GCode Viewer to show in HA as a camera.

Looks like octoprint plugin changed behaviour, it no longer serves the .png file directly, but instead serves the .bgcode file, which is actually a .png if you download it. Anyway the camera plugin can’t deal with the image format mismatch, so here’s my workaround.

  • install the MQTT and HA discovery plugins to octoprint
  • register to MQTT on your HA (you have to copy user + password from HA MQTT config) - this will provide filename info
  • add shell command to pull the image to your confiuration.yaml. The command will save file with proper .png file type.
shell_command:
  3d_update_thumbnail_image: 'curl -o /config/www/prusa_mini_preview.png http://192.168.123.19/plugin/prusaslicerthumbnails/thumbnail/{{ states("sensor.octoprint_print_file")|urlencode }}'
  • add automation to run the command when printer starts printing (or define your own trigger)
automation
  - alias: 3d_octoprint_pull_thumbnail
    trigger:
    - platform: state
      entity_id:
      - sensor.octoprint_print_status
      to: Printing
    action:
    - service: shell_command.3d_update_thumbnail_image
  • add camera to serve static image
camera:
  - platform: local_file
    name: prusa_mini
    file_path: /config/www/prusa_mini_preview.png
  • restart homeassistant (yaml reload didn’t work for me)
  • add the camera to your dashboard
1 Like