greghesp / ha-bambulab

A Home Assistant Integration for Bambu Lab Printers
1k stars 85 forks source link

[Bug] Integration writes data every 5 seconds, even when printer is off #594

Closed osirisinferi closed 2 weeks ago

osirisinferi commented 1 month ago

Describe the bug

Today, when looking at some of the graphs generated by the integration, that when my printer was offline (it only runs so often), the flat line of the graph had data points every 5 seconds. In other integrations (e.g. my ZigBee device), when a device is offline, the line is indeed flat, but does not have any data points.

I don't think it makes much sense to add data points every 5 seconds for a device that's offline. It makes my database grow larger and larger without a good reason, which is not a great thing to have if you're storing your HA DB on a device which has limited write cycles to begin with.

To Reproduce

  1. Fire up your printer
  2. Make it do some stuff
  3. Turn it off
  4. After a while, notice the flat line of any graph keeps making data entries every 5 seconds

Expected Behaviour

No added data entries when device is offline.

What device are you using?

X1C

Diagnostic Output

{
  "home_assistant": {
    "installation_type": "Home Assistant Core",
    "version": "2024.6.2",
    "dev": false,
    "hassio": false,
    "virtualenv": true,
    "python_version": "3.12.3",
    "docker": false,
    "arch": "aarch64",
    "timezone": "Europe/Amsterdam",
    "os_name": "Linux",
    "os_version": "6.1.21-v8+",
    "run_as_root": false
  },
  "custom_components": {
    "bambu_lab": {
      "documentation": "https://github.com/greghesp/ha-bambulab",
      "version": "2.0.22",
      "requirements": []
    },
    "smart_thermostat": {
      "documentation": "https://github.com/ScratMan/HASmartThermostat",
      "version": "2021.11.1",
      "requirements": []
    },
    "mhz19": {
      "documentation": "https://www.home-assistant.io/integrations/mhz19",
      "version": "1.0",
      "requirements": [
        "pmsensor==0.4"
      ]
    },
    "generic_thermostat": {
      "documentation": "https://www.home-assistant.io/integrations/generic_thermostat",
      "version": "2021.9.7",
      "requirements": []
    },
    "thermal_comfort": {
      "documentation": "https://github.com/dolezsa/thermal_comfort/blob/master/README.md",
      "version": "2.2.2",
      "requirements": []
    }
  },
  "integration_manifest": {
    "domain": "bambu_lab",
    "name": "Bambu Lab",
    "codeowners": [
      "greghesp",
      "AdrianGarside"
    ],
    "config_flow": true,
    "dependencies": [
      "device_automation",
      "ffmpeg",
      "mqtt"
    ],
    "documentation": "https://github.com/greghesp/ha-bambulab",
    "iot_class": "local_push",
    "issue_tracker": "https://github.com/greghesp/ha-bambulab/issues",
    "ssdp": [
      {
        "st": "urn:bambulab-com:device:3dprinter:1"
      }
    ],
    "version": "2.0.22",
    "is_built_in": false
  },
  "setup_times": {},
  "data": {
    "config_entry": {
      "data": {
        "device_type": "X1C",
        "serial": "**REDACTED**"
      },
      "disabled_by": null,
      "domain": "bambu_lab",
      "entry_id": "e6d3495d8449f60b64fa2ab827c73a50",
      "minor_version": 1,
      "options": {
        "region": "",
        "email": "",
        "username": "",
        "name": "",
        "host": "192.168.178.203",
        "local_mqtt": true,
        "auth_token": "",
        "access_code": "**REDACTED**",
        "usage_hours": 85.74999999999999
      },
      "pref_disable_new_entities": false,
      "pref_disable_polling": false,
      "source": "user",
      "title": "**REDACTED**",
      "unique_id": null,
      "version": 2
    },
    "push_all": {
      "ams": {
        "ams": [
          {
            "humidity": "5",
            "id": "0",
            "temp": "21.1",
            "tray": [
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "161616FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "0",
                "nozzle_temp_max": "280",
                "nozzle_temp_min": "240",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "161616FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFB99",
                "tray_sub_brands": "",
                "tray_type": "ABS",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "2850E0FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "1",
                "nozzle_temp_max": "270",
                "nozzle_temp_min": "220",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "2850E0FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFG99",
                "tray_sub_brands": "",
                "tray_type": "PETG",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "00000000"
                ],
                "ctype": 0,
                "drying_temp": "70",
                "drying_time": "8",
                "id": "2",
                "nozzle_temp_max": "220",
                "nozzle_temp_min": "190",
                "remain": 25,
                "tag_uid": "1C8C9F5400000100",
                "tray_color": "00000000",
                "tray_diameter": "1.75",
                "tray_id_name": "S05-C0",
                "tray_info_idx": "GFS05",
                "tray_sub_brands": "Support for PLA",
                "tray_type": "PLA-S",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "250",
                "xcam_info": "000000000000000000000000"
              },
              {
                "bed_temp": "0",
                "bed_temp_type": "0",
                "cali_idx": -1,
                "cols": [
                  "F72323FF"
                ],
                "ctype": 0,
                "drying_temp": "0",
                "drying_time": "0",
                "id": "3",
                "nozzle_temp_max": "240",
                "nozzle_temp_min": "190",
                "remain": -1,
                "tag_uid": "0000000000000000",
                "tray_color": "F72323FF",
                "tray_diameter": "0.00",
                "tray_id_name": "",
                "tray_info_idx": "GFL99",
                "tray_sub_brands": "",
                "tray_type": "PLA",
                "tray_uuid": "00000000000000000000000000000000",
                "tray_weight": "0",
                "xcam_info": "000000000000000000000000"
              }
            ]
          }
        ],
        "ams_exist_bits": "1",
        "ams_exist_bits_raw": "1",
        "insert_flag": true,
        "power_on_flag": false,
        "tray_exist_bits": "f",
        "tray_is_bbl_bits": "f",
        "tray_now": "1",
        "tray_pre": "1",
        "tray_read_done_bits": "f",
        "tray_reading_bits": "0",
        "tray_tar": "1",
        "version": 21
      },
      "ams_rfid_status": 2,
      "ams_status": 768,
      "aux_part_fan": true,
      "bed_target_temper": 70.0,
      "bed_temper": 70.0,
      "big_fan1_speed": "0",
      "big_fan2_speed": "10",
      "cali_version": 0,
      "chamber_temper": 37.0,
      "command": "push_status",
      "cooling_fan_speed": "7",
      "ctt": 0,
      "device": {
        "fan": 55200,
        "nozzle": {
          "0": {
            "info": 136,
            "temp": 16711935
          },
          "info": 231
        }
      },
      "fail_reason": "0",
      "fan_gear": 11796582,
      "filam_bak": [],
      "force_upgrade": false,
      "gcode_file": "/data/Metadata/plate_1.gcode",
      "gcode_file_prepare_percent": "100",
      "gcode_state": "RUNNING",
      "heatbreak_fan_speed": "13",
      "hms": [],
      "home_flag": 6407583,
      "hw_switch_state": 1,
      "ipcam": {
        "agora_service": "disable",
        "ipcam_dev": "1",
        "ipcam_record": "disable",
        "mode_bits": 2,
        "resolution": "720p",
        "rtsp_url": "**REDACTED**",
        "timelapse": "disable",
        "tutk_server": "disable"
      },
      "job_id": "0",
      "layer_num": 16,
      "lights_report": [
        {
          "mode": "on",
          "node": "chamber_light"
        },
        {
          "mode": "flashing",
          "node": "work_light"
        }
      ],
      "maintain": 3,
      "mc_percent": 55,
      "mc_print_error_code": "0",
      "mc_print_stage": "2",
      "mc_print_sub_stage": 0,
      "mc_remaining_time": 56,
      "net": {
        "conf": 16,
        "info": [
          {
            "ip": 3417483456,
            "mask": 16777215
          },
          {
            "ip": 0,
            "mask": 0
          }
        ]
      },
      "nozzle_diameter": "0.4",
      "nozzle_target_temper": 255.0,
      "nozzle_temper": 255.0,
      "nozzle_type": "hardened_steel",
      "online": {
        "ahb": false,
        "ext": false,
        "version": 4
      },
      "print_error": 0,
      "print_gcode_action": 0,
      "print_real_action": 0,
      "print_type": "local",
      "profile_id": "0",
      "project_id": "0",
      "queue_est": 0,
      "queue_number": 0,
      "queue_sts": 0,
      "queue_total": 0,
      "s_obj": [],
      "sdcard": true,
      "sequence_id": "2021",
      "spd_lvl": 2,
      "spd_mag": 100,
      "stg": [
        2,
        14,
        1,
        8
      ],
      "stg_cur": 0,
      "subtask_id": "0",
      "subtask_name": "TopLeft",
      "task_id": "9856",
      "total_layer_num": 33,
      "upgrade_state": {
        "ahb_new_version_number": "",
        "ams_new_version_number": "",
        "consistency_request": false,
        "dis_state": 0,
        "err_code": 0,
        "ext_new_version_number": "",
        "force_upgrade": false,
        "idx": 4,
        "idx1": 0,
        "lower_limit": "00.00.00.00",
        "message": "",
        "module": "",
        "new_version_state": 2,
        "ota_new_version_number": "",
        "progress": "0",
        "sequence_id": 0,
        "sn": "**REDACTED**",
        "status": "IDLE"
      },
      "upload": {
        "file_size": 0,
        "finish_size": 0,
        "message": "Good",
        "oss_url": "",
        "progress": 0,
        "sequence_id": "0903",
        "speed": 0,
        "status": "idle",
        "task_id": "",
        "time_remaining": 0,
        "trouble_id": ""
      },
      "vt_tray": {
        "bed_temp": "0",
        "bed_temp_type": "0",
        "cali_idx": -1,
        "cols": [
          "00000000"
        ],
        "ctype": 0,
        "drying_temp": "0",
        "drying_time": "0",
        "id": "254",
        "nozzle_temp_max": "0",
        "nozzle_temp_min": "0",
        "remain": 0,
        "tag_uid": "0000000000000000",
        "tray_color": "00000000",
        "tray_diameter": "0.00",
        "tray_id_name": "",
        "tray_info_idx": "",
        "tray_sub_brands": "",
        "tray_type": "",
        "tray_uuid": "00000000000000000000000000000000",
        "tray_weight": "0",
        "xcam_info": "000000000000000000000000"
      },
      "wifi_signal": "-67dBm",
      "xcam": {
        "allow_skip_parts": false,
        "buildplate_marker_detector": true,
        "first_layer_inspector": true,
        "halt_print_sensitivity": "medium",
        "print_halt": true,
        "printing_monitor": true,
        "spaghetti_detector": true
      },
      "xcam_status": "0"
    },
    "get_version": {
      "command": "get_version",
      "module": [
        {
          "flag": 3,
          "hw_ver": "N/A",
          "name": "ota",
          "sn": "**REDACTED**",
          "sw_ver": "01.08.02.00"
        },
        {
          "flag": 0,
          "hw_ver": "AMS08",
          "name": "ams/0",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.06.44"
        },
        {
          "flag": 0,
          "hw_ver": "MC07",
          "name": "mc",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.27.26"
        },
        {
          "flag": 0,
          "hw_ver": "SMC01",
          "name": "sm",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.27.26"
        },
        {
          "flag": 0,
          "hw_ver": "TH09",
          "name": "th",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.07.12"
        },
        {
          "flag": 0,
          "hw_ver": "AP05",
          "name": "ap",
          "sn": "**REDACTED**",
          "sw_ver": "00.00.32.39"
        }
      ],
      "sequence_id": "20010"
    }
  }
}

Log Extracts

No response

Other Information

No response

greghesp commented 1 month ago

I think this is down to how the integration is designed, and how HA writes data.

@AdrianGarside any ideas? I think this may be due to the integration always being active, rather than erroring if it cannot connect

osirisinferi commented 3 weeks ago

I've disabled the integration while the printer is not operational. Not ideal, but at least my database won't get spammed with useless data all the time.

Would like to see a better solution though.

AdrianGarside commented 3 weeks ago

I'll have to check but 5 seconds doesn't ring a bell. Once we stop sending data HA itself will start polling us for data and I haven't found a way to suppress that (it's pointless). Is it all data points, or just some?

AJolly commented 3 weeks ago

This might be related - it HAMMERS my logs.
home-assistant.log Whats interesting is the rate is not consistent: image

suneworld commented 3 weeks ago

I see the same and HA finally crashes after some time

AdrianGarside commented 3 weeks ago

What version of the integration are you using?

AdrianGarside commented 3 weeks ago

Back to the original issue. There was a 60s poll timer that kicks in when the printer is offline - but that wouldn't explain a 5s logging update. I'm going to get rid of the poll as it's been unnecessary since the very earliest days of the integration. But I can confirm that other than that there is no integration activity occuring when the printer is offline. I'll need to check actual sensors next to see if HA itself is doing something weird to pad out the history of values.

AdrianGarside commented 2 weeks ago

@osirisinferi the graphs show unchanged values every 5 minutes when the printer is on too. The integration is not generating updates that explain this. It seems to be a home assistant behavior.

osirisinferi commented 2 weeks ago

But why would other integrations like deCONZ not have regular updates (5 seconds or minutes doesn't really matter, it's too much for something that is offline).

My temperature/humidity graphs from deCONZ show flat lines without any updates if the sensor doesn't send any updates. So I'm not sure I agree with your deduction that it must be a HA behaviour, as I don't recognise this from other integrations.

greghesp commented 2 weeks ago

I cannot reproduce this. My printer for example, has been turned off this week. You can see where I turned it off, it then flatlines, with no updates until the end of the graph when I turned it back on.

Are you physically turning the printer off at the socket?

image image
osirisinferi commented 2 weeks ago

There seems to be a discrepancy between the graphs in my dashboard at /lovelace/3d-printer (original dashboard name, I know) and the graphs as shown in /history. The ha-bambulab integration does not show these regular updates in /history, but in the dashboard graph, there's a data point to "hover" over every 5 minutes even though the printer is 100 % off (no power). deCONZ behaves differently in the dashboard though. Not sure what to believe any longer.

I've tried looking at the entries in the SQL database, but that used to be easier some years ago. Nowadays I have a hard time finding the correct data.

I'll just leave it at this. Just to make sure I'll disable the integration if I don't use the printer for a longer amount of time and re-enable it when I see fit. The integration also keeps spamming my local network every second for local LAN printer detection, which, while a minor thing and not problematic at all, would also be disabled. So win-win.

If I find some "hard evidence", I might come back to this potential issue :slightly_smiling_face:

greghesp commented 2 weeks ago

I assume you mean this graph?

image

I think what you're seeing is the Home Assistant Recorder itself in that graph. It writes to the database every 5 minutes, so on that graph you see the datapoint every 5 minutes, but in the history, if the data never changes, it doesn't show it as a data point: https://www.home-assistant.io/integrations/recorder/

AdrianGarside commented 2 weeks ago

When I look at the popup history for any numerical sensor for any integration I have on my production home assistant I see data points listed every 5 minutes as I hover around. If I click the show more button and do the same on that graph, I see data points only when the value changed. As far as I can tell this a consistent home assistant behavior and not anything specific to this integration. There was a 60s poll update that dates back to the very initial implementation of the integration. It was has long been unnecessary and I removed it in the latest (v2.0.25) update. That poll timer only triggered if the integration didn't send updates for more than the 60s poll timer - which would only happen if the printer was off (the printers are chatty and update things like wifi strength very regularly). That is the only thing I can think of that could explain a differing behavior when offline vs online. But it's neither 5s nor 5 minutes.