Spoolman Updater – Automate Filament Tracking

Thnx for the PR :slight_smile: Merged as we speak

1 Like

Hi all,
I have a A1 with AMS Lite and got the api and page working, but to be sincere I am very confused now on the creation of the automatation itself.
The A1 does not have sensor.a1_active_tray_index only sensor.a1_active_tray
Is there anyone with A1’s that got this working and can share the automation?

Thanks a lot

Did you read the readme? GitHub - marcokreeft87/spoolman-updater: The Spoolman Updater API provides endpoints to manage spool updates, including tracking filament usage and material details. This API is designed to work with Home Assistant and other automation systems.

Like Marco says, please read the read me fully Depending on the Bambu Lab integration version, you need to follow either instructions

1 Like

Hi guys, not gonna lie that I read but sometimes my head does not process just as well what I read.
After this, I read more 3 times and understood better, thanks!
I have now everything set up and just need to test a new print :slight_smile:

Thanks again.

2 Likes

It is to bad that a change was needed in everything due to the Bambu integration but it should work :slight_smile: Fingers crossed that it also works for you!

1 Like

@Lucrezia
I have this error all time it finish printing:
Error rendering data template: ValueError: Template error: int got invalid input ‘’ when rendering template ‘{{ trigger.to_state.state | int }}’ but no default was specified

infos on last printing were updated but not weight

I also have to say that filament_usage_meter is always to zero, it collects data but always to zero.

Hi, please recheck all the variables in the sensors and automations. I had the same issue and I missed one entity with wrong name that caused this.

  • platform: template
    sensors:
    bambulab_filament_usage:
    unique_id: b954300e-d3a2-44ab-948f-39c30b2f0c00
    friendly_name: “Bambu Lab Filament Usage”
    value_template: “{{ states(‘sensor.bambu_h2d_grammatura_stampa’) | float(0) / 100 * states(‘sensor.bambu_h2d_progressi_di_stampa’) | float(0) }}”
    availability_template: >-
    {% if is_state(‘sensor.bambu_h2d_grammatura_stampa’, ‘unknown’)
    or is_state(‘sensor.bambu_h2d_grammatura_stampa’, ‘unavailable’) %}
    false
    {% else %}
    true
    {% endif %}

    bambulab_ams1_tray_index:
      friendly_name: "Bambu Lab AMS1 Tray Index"
      value_template: >-
        {% set trays = [1,2,3,4] %}
        {% for tray in trays %}
          {% set eid = 'sensor.bambu_h2d_ams_1_tray_' ~ tray %}
          {% if state_attr(eid, 'active') in [true, 'true', 'True'] %}
            {{ tray }}
            {% break %}
          {% endif %}
        {% endfor %}
      availability_template: >-
        {{ expand([
          'sensor.bambu_h2d_ams_1_tray_1',
          'sensor.bambu_h2d_ams_1_tray_2',
          'sensor.bambu_h2d_ams_1_tray_3',
          'sensor.bambu_h2d_ams_1_tray_4'
        ]) | selectattr('attributes.active','defined') | list | count > 0 }}
    
  • id: ‘1756972720618’
    alias: Bambulab - Update Spool (with last_tray helper + debug )
    description: Update spool
    triggers:

    • entity_id: sensor.bambulab_ams1_tray_index
      id: tray
      trigger: state
    • entity_id: sensor.bambu_h2d_fase_corrente
      to:
      • finished
      • idle
        id: print_end
        trigger: state
        actions:
    • choose:
      • conditions:
        • condition: trigger
          id: tray
          sequence:
        • target:
          entity_id: input_number.bambulab_last_tray
          data:
          value: ‘{{ trigger.to_state.state | int }}’
          action: input_number.set_value
        • data:
          message: ‘Tray change: input_number.bambulab_last_tray → {{ trigger.to_state.state }}’
          level: info
          action: system_log.write
      • conditions:
        • condition: trigger
          id: print_end
          sequence:
        • variables:
          tray_number: ‘{{ states(“input_number.bambulab_last_tray”) | int(0) }}’
          tray_sensor: sensor.bambu_h2d_ams_1_tray_{{ tray_number }}
          tray_weight: ‘{{ states(“sensor.bambulab_filament_usage_meter”) | float(0) | round(2) }}’
          tag_uid: ‘{{ state_attr(tray_sensor, “tag_uid”) }}’
          material: ‘{{ state_attr(tray_sensor, “type”) }}’
          name: ‘{{ state_attr(tray_sensor, “name”) }}’
          color: ‘{{ state_attr(tray_sensor, “color”) }}’
        • data:
          message: ‘Filament usage meter raw: {{ states(“sensor.bambulab_filament_usage_meter”) }}’
          level: warning
          action: system_log.write
        • data:
          message: ‘Print end trigger. Last tray: {{ tray_number }} ({{ name }} - {{ material }}) Weight: {{ tray_weight }}g Color: {{ color }} Tag UID: {{ tag_uid }}’
          level: info
          action: system_log.write
        • data:
          filament_name: ‘{{ name }}’
          filament_material: ‘{{ material }}’
          filament_tag_uid: ‘{{ tag_uid }}’
          filament_used_weight: ‘{{ tray_weight }}’
          filament_color: ‘{{ color }}’
          filament_active_tray_id: ‘{{ tray_sensor | replace(“sensor.”, “”) }}’
          action: rest_command.bambulab_update_spool
        • delay: “00:00:05”
        • target:
          entity_id: sensor.bambulab_filament_usage_meter
          data:
          value: ‘0’
          action: utility_meter.calibrate
          mode: single

rest_command:
bambulab_update_spool:
url: “http://192.168.2.116:8088/Spools
method: POST
headers:
Content-Type: “application/json”
payload: >
{
“name”: “{{ filament_name }}”,
“material”: “{{ filament_material }}”,
“tag_uid”: “{{ filament_tag_uid }}”,
“used_weight”: {{ filament_used_weight | int }},
“color”: “{{ filament_color }}”,
“active_tray_id”: “{{ filament_active_tray_id }}”
}

utility_meter:
bambulab_filament_usage_meter:
unique_id: 148d1e2d-87b2-4883-a923-a36a2c9fa0ac
source: sensor.bambu_h2d_grammatura_stampa
cycle: weekly

input_number:
bambulab_last_tray:
name: “BambuLab Last Tray”
min: 0
max: 6
step: 1

Apologies for the late response, I didn’t receive any notification.

That the weight is always zero, could be correct as you sensors, tray index and so on, are all incorrect or did you rename your Bambu within Home Assistant to bambu_h2d?
If you fully renamed your printer to bambu_h2d, I will do a full check what you put in here.

If you did not change the name, you need to check your sensor as normally the printer comes into Home Assistant somehing like this: H2D_RANDOM BUNCH OF NUMBERS (H2D_3789719874387264)

where I have to check? only in automations? or in configuration?
I renamed to bambu_h2d

If you renamed it in HA to bambu_h2d, than it is ok. You say it fails on: {{ trigger.to_state.state | int }} ? Can you check if you have set your helper the correct way?
Seeing that you have set your HA not to English, please check if your sensor attributes are in English or not.
For example: sensor.bambu_h2d_grammatura_stampa does this have the attribute unknown and unavailable or is it set to your language in HA?

it has always a value… in history

I beginning to think that the H2D isn’t acting the same as the H2S. The difference between the P1S and H2S is that the active tray was cleared before the current stage for printing was set to finished. Hence, I needed to re-write this script.

With the script for the H2S, as also stated on Git, it most likely will also work for the H2D, as the H2S is his little brother.
But seeing your problem, trigger.to_state.state | int is not being filled and is returning empty, I suspect that might be the problem. As I don’t have the H2D, I am not able to test it.

Can you please try the steps stated on Git that is used for the P1S? Let me know if this solves the issue, then I can also make the adjusted on Git.

Sry if this is obvious for everyone except me but: Does this also work without bambulab filament spools? I found many approaches to track filament usage from bambu ams but most of them only work with bambu filament spools. Or there’s openspoolman which also works with other spools but requires you to put a spare bambu rfid tag on a spool and then scan it with your phone using nfc. Also the container needs to run with https which I failed to set up.

Edit: I now set everything up, all sensors show correct/sane values but opening http://192.168.1.111:8088 only shows me the external spool and if I click on “select” nothing happens:

I have to add that I’m using the ha-bambulab integration in german language so most sensors have different names. For example the name of ams slot 1 is:sensor.ID_PRINTER_ams_1_slot_1 not sensor.ID_PRINTER_ams_1_tray_1.
Could this be the problem? I changed everything for homeassistant accordingly but is it maybe somehow “hardcoded” in the docker container?

But it doesn’t explain the second problem, nothing happening if I hit “select”. Opening http://192.168.1.111:8088/spools shows me a lot of text with filament info from spoolman, so I guess there’s at least some connection to spoolman.

No sorry :wink: Yes, this works with every filament brand as long as you have set them up in Spoolman.

If the other spools don’t show up, you have not entered the correct variable(s) for your AMS tray sensor. At least, that is most of the case. Language doesn’t matter, the correct sensor name does matter.
Can you please share your variables that you have setup within your docker?

  spoolman-updater:
    image: marcokreeft/spoolman-updater
    container_name: spoolman-updater
    environment:
      - PGID=${PGID}
      - PUID=${PUID}
      - TZ=${TZ}
      - APPLICATION__HOMEASSISTANT__URL=http://192.168.1.111:8123
      - APPLICATION__HOMEASSISTANT__TOKEN=********************
      - APPLICATION__SPOOLMAN__URL=http://192.168.1.111:7912
      - APPLICATION__HOMEASSISTANT__TRAYENTITIES__0=p1p_01p09a3a0501154_ams_1_slot_1
      - APPLICATION__HOMEASSISTANT__TRAYENTITIES__1=p1p_01p09a3a0501154_ams_1_slot_2
      - APPLICATION__HOMEASSISTANT__TRAYENTITIES__2=p1p_01p09a3a0501154_ams_1_slot_3
      - APPLICATION__HOMEASSISTANT__TRAYENTITIES__3=p1p_01p09a3a0501154_ams_1_slot_4
      #- APPLICATION__HOMEASSISTANT__AMSENTITIES__0=p1p_01p09a3a0501154_ams_1
      - APPLICATION__HOMEASSISTANT__EXTERNALSPOOLENTITY=p1p_01p09a3a0501154_externalspool_externe_spule_2
    ports:
      - "8088:8080"

This is my docker compose file.
I now noticed that after a print a new Spool was created (probably default behavior if none is manually defined?) so seems everything works except for displaying ams slots and choosing filament for them.

I see you also opened the issue on Github, which is closed by @marcokreeft87 as it is indeed not an issue but mis-configuration.
Did you get your trayentities to work now?

I finally got it to work :slight_smile: Unable to select Spools in WebUi · Issue #14 · marcokreeft87/spoolman-updater · GitHub

That was my problem with openspoolman, as there is nowhere a tutorial for that. I don’t even know what exactly is needed to get scanning via nfc or camera working, I mean does the container have to be reachable via https? Or the whole host? I tried this with openspoolman using a container named swag but I failed to do so.
Is there no other way to get any scanning to work? Maybe with a different browser, or by making it an app?

Now that I got it working I’m thinking of ways how filament loading could be automated.
I read about the rfid tags on the bambu spools and was wondering if they could somehow be used for other spools too.
Can the rfid tags be read somehow by the ams and the result be grabbed? Like say, scanning an rfid tag gets some number, that can be set in spoolman.
I have plenty spare rfid tags, any way I could use them?
A workflow could be: Stick a spare rfid tag on some new spool, put it in the ams so it is scanned. Copy the result to a field in the correct spool in spoolman. Then every time that spool is placed in the ams, the rfid scan result can be matched to spoolman spool.

The alternative with making qrcode stickers in spoolman and scan them with spoolman-updater also sounds good, but my knowledge isn’t enough to set up that https stuff. I wish there’d be a simpler way…I also have one of there fancy barcode handscanners, if that could be an alternative.