Bambu Lab X1 X1C MQTT

I have a P1P so that will require very different handling by the looks of it. Not clear how well you can track state if there’s no direct way to poll for it. But my integration building experience is nil so I’ll need something to boot strap off and something similar-ish to help me understand what’s possible.

Not a lot of spare time at the moment either so while I’d kind of like to learn how node red works given I see lots of good things written about it, probably not something I’ll have to time to learn first.

1 Like

Now that I’m more familiar with mqtt explorer I see I missed the response to this in the rapid spew. It did work once I stripped the outer {“payload”: } off it.

{"info":{"command":"get_version","sequence_id":"20004","module":[{"name":"ota","sw_ver":"01.01.01.00","hw_ver":"OTA","sn":""},{"name":"esp32","sw_ver":"00.02.10.39","hw_ver":"AP04","sn":""},{"name":"mc","sw_ver":"00.00.12.53","hw_ver":"MC07","sn":""},{"name":"th","sw_ver":"00.00.04.60","hw_ver":"TH09","sn":""},{"name":"ams/0","sw_ver":"00.00.05.75","ota_ver":"00.00.00.00","hw_ver":"AMS08","sn":""}],"result":"success","reason":""}}

I’m trying to replicate your progress with my P1P. I tried the mqtt bridge approach but after adjusting my local mosquitto config (running as a service), I don’t see any evidence in the output it even attempts to connect to the P1P mqtt. I added the explicit device//report (for in) and /request (for out) since the P1P doesn’t support general access. Does mosquitto perhaps not handle the fact the P1P doesn’t behave normally?

What’s the status of your integration not needing to use an bridge? That would make the integration much easier to configure anyway.

Ah, just found your separate HACS repo. I hadn’t realized it was separate from your devtest one.

Yeah, my suspicion is that most of the commands to get status work with p1p too. Wonder if there is one that can trigger all the data to come back, either as individual messages or single. I’d begin to be worried for regularly polling for all the data though if the ESP is powerful enough with the printer. Might be best to have a stripped down one that only gets the changing updates the P1P reports, + any AMS stuff.

Could poll for versions on an hourly basis (and on connection probably), you pretty much always get temperature changes as individual messages (unless stable), not sure when the AMS stuff comes in, probably whenever it does an action.

I think with the custom integration it should be possible to configure it for both a P1P, X1 and X1C, probably need a config flow dropdown to select which one (X1 and X1C basically the same, just naming of stuff will be different), where P1P will only get a subset of sensors and likely not update as often. To add onto this, I’m pretty sure you really need to know the correct topic and can’t scan for them on the P1P so likely would be beneficial to add SerialNumber to the config flow for all printers, rather than rely on auto detection of topic.

I got @greghesp’s HACS integration installed and tried to modify it to pass the serial up front to subscribe to the specific report channel as the P1P mqtt requires (rather than wildcard) but as best I can tell I’m not getting any messages back during the initial connection attempt even with that change. I see the ‘On Connected: Connected to Broker’ on a background thread but no messages arrive in the 5 seconds before it gives up on the attempt. Kinda stumped.

Also is there a way to make test changes without having to restart home assistant or test outside of home assistant for a more rapid inner loop as I learn how this works?

If you use Docker, then just clone my repo into a folder somewhere. I put mine in a folder called Custom Components: GitHub - greghesp/ha-bambulab

I then have another folder somewhere called ha-dev and in there I have a structure that looks like this:
image

Then in the docker-compose file I have

version: "3.3"
services:
 hass:
  image: homeassistant/home-assistant:beta
  container_name: homeassistant
  restart: unless-stopped # To reboot the container when the host comes back up from restarts.
  ports:
   - 8123:8123
  volumes:
   - ./hass_dev:/config
   - ../Custom Components/ha-bambulab/custom_components/bambu_lab:/config/custom_components/bambu_lab

Then to run docker-compose up in that folder. This will then map the hass_dev folder to the home assistant config folder, and also map the cloned ha-bambulab repo into the custom components folder.

When you make changes, you can just restart the Docker container which will only take a few seconds

1 Like

Repeating my mqtt explorer setup I see it defaulted to adding some channels to the connection and it gets immediately disconnected with those specified. Remove them and it can stay connected but doesn’t receive anything and I can’t see any option to subscribe to a topic after the fact - only in the initial advanced settings for the connection.

So I suspect that the mqtt being used in the integation is trying to do things that a normal mqtt server would handle but the stripped down custom P1P mqtt server can’t. I need to work out if there’s an equivalent on_disconnect callback I can use to confirm I’m getting immediately disconnected.

You should be able to add in an on_disconnect method and pass it a callback function

How do I find the expected parameters for on_disconnect? How did you learn about / how to use the HA mqtt APIs?

I have it all set up and functioning as it should, the only thing i don’t see is the custom svg file for filament on my phone (android) dashboard, i see it on my computer dashboard and its the same dashboard.
Thank you for all your hard work

Probably a caching issue then. I’d suggest clearing the app cache then restarting it. If not, it will probably work at some point. Ran into this issue myself, darn HA caching!

Aha. Found the documentation! mqttclient.onconnectionlost() | Dev Center (electricimp.com)
Hmm, maybe not it doesn’t exactly match. Found this which has the right method names:
Paho Python MQTT Client-Understanding The Loop (steves-internet-guide.com)

Looks like this is indeed the mqtt implementation python is using:
paho-mqtt · PyPI

I got the on_disconnect method to be called but it’s only being called after the connect attempt times out without having received any messages. So I am connected but subscribing to the correct topic isn’t getting me anything.

image

Success! Moving the subscribe to the topic into the on_connect method looks like it fixed a race condition.

1 Like

Got most of the existing sensors in @greghesp’s integration working.

Need to work out what’s up with the fans. Chamber and target temperatures never get set as the P1P doesn’t publish those (probably doesn’t even have a chamber temperature sensor?).

Update: Fans was a dumb bug on my part. Have current stage working now too although it and speed don’t initialize until they change and the P1P sends out an update for them.

2 Likes

Yeah, I feel like for the P1P most of the sensors should be marked as unavailable until an update comes for them, or they get a default value of like 0 or something.

Also, is that cooling fan trying to cool the sun :laughing:

Would be nice for the P1P work you do to put it into a pull request. It’s probably best practice to treat all sensors as “unavailable until we get an update” regardless of printer, and just for the P1P there are some sensors we never even make probably.

I’m trying to set this up so I can trigger prints I manually send to the printers via FTP but the nodered plugin is broken for HA :frowning:
Is there a way to run nodered externally then connect to HA or am I stuck until it gets fixed?

Do you mean the NodeRed Addon if you’re using HassOS? NodeRed can be run externally for sure, easiest would be in a docker container. I myself run Home-Assistant-Core instead of HassOS so I am forced to have NodeRed, MQTT, EspHome etc all externally.

Also FYI - the nodered flow doesn’t have anything to send and start prints, though now that I think about it, it seems like it would be possible, just tedious to use with HA. Pretty simple to get a node in NR to list the files from the printer FTP, and I know way earlier in the thread there is the command to start a print from a file + task name, so it is possible.

Yep, I plan to work out a good way to hide the sensors that don’t exist (or aren’t published) on the P1P. I’m just happy to get things working :slight_smile:. Trying to decide if the code should try and auto-detect X1 vs P1P and just do the right thing or require an explicit choice given that the P1P requires the serial number to be provided by the user. I’m leaning towards the explicit choice so that the extra serial number requirement can be skipped by X1 owners.

The fans were just a slip as I simplified some code and instead of it being the intended ‘new raw value or previous raw value if the value isn’t in this specific payload’ converted into a percentage it became previous percentage converted (multiplied) to a percentage again. So all the payloads coming through without fan values cause the number to exponentially increase.

Once I’ve got it tidied up and I have confirmed (as best I can without one) that I haven’t broken any X1 scenarios with my changes, I’ll do a pull request for review.

1 Like

Is there a good way of having a sensor report N/A as well as a numerical value (not as the same time)? Both for cases where someone tries to query a sensor the P1P simply doesn’t have/publish or for cases where it has decided to stop publishing. I see, for instance, that now the printer has completely cooled down it’s still publishing bed temperature somewhat regularly but has stopped publishing nozzle temperature so after restarting home assistant that’s just sat at the default of 0C that I set it to. I’d like to start it at something like ‘N/A’ and maybe time it out back to N/A if it hasn’t been seen for a while.

More sensors are working than I thought. Target temperatures are being set. Maybe that’s only done during the first pre-print phase since they didn’t seem to be being pushed mid-print:

Not sure what’s up with that 5C chamber temperature. It’s definitely toastier than that in my house and even toastier still inside the printer enclosure!

I’ve been slowly working on some of that. My constant hangup is file uploads. I haven’t found “friendly” solution as of yet. If anybody has a great idea as how to upload a file through HA (that’ll wind up in a NR HTTP In node), I’m all ears.

1 Like