How to get printer temperature value in the plugin?

It's my first time to develop a plugin using python Could anyone tell me how to get the printer temperature? Thanks.

Per the docs:

It's probably something like...

import requests
API_ENDPOINT = "http://localhost/api/printer?exclude=state,sd"
PARAMS = {X-Api-Key: MYAPIKEY}
r = requests.get(url = API_ENDPOINT, params = PARAMS)
data = r.json()
t0 = data["temperature"]["tool0"]["actual"]

It's way easier than @OutsourcedGuru's recommendation. In python your plugin gets an injected property called _printer. So if you call self._printer.get_current_temperatures() it will return a dictionary of all related temperatures reported by your printer.

https://docs.octoprint.org/en/master/modules/printer.html#octoprint.printer.PrinterInterface.get_current_temperatures

3 Likes

That's actually what I use, too. I was afraid that foosel would suggest otherwise.

Thank you so much. @jneilliii @OutsourcedGuru I'll try the get_current_temperatures() function first since it looks more like C++ and I'm only familiar with C++. Thanks.

That's very ominous, why would she suggest otherwise when it's in the docs (even current devel docs)?

I thought she preferred users to use the REST API over the internal workings.

Uhm.... no, not even remotely :sweat_smile: There's docs for internal API and stuff so that people know what to use. The REST API is for external stuff, would be kinda backwards (and wasteful and bad) to go through a network connection when you are running in the same process even.

Kinda confused what makes you think that this would be my opinion :thinking::sweat_smile:

I've been on here for a long time and I seem to remember you saying that at some point. I dunno.

(For what it's worth, I use the internal API, cross my fingers that it won't change over versions and drive on.)