Controlling a HomeBridge accessory via Moonraker

I run a HomeBridge server on my local network to control various accessories around the house. Eventually I do plan on swapping to HomeBridge + HomeAssistant but for now - by itself - I'm happy with the functionality I have.

I recently upgraded my Ender3 to run Klipper firmware and use Fluidd to control my 3D printer. I used to run a plugin in Octoprint to control the power board the printer is connected to but no such plugin exists for Klipper.

Moonraker offers power management, however HomeBridge is not offered by default and requires setting up a generic HTTP device.

HomeBridge offers a basic REST API for interacting with accessories when HomeBridge is running in Insecure Mode (Started with homebridge -I).

Getting aid & iid from HomeBridge

This is a little bit of a hassle since the aid & iid reported in the HomeBridge UI are sometimes not actually the ones you need to use.

Hitting GET http://xxx.xxx.xxx.xxx:52626/accessories will return a list of all the accessories in your network. Include the Authorization header with 8-digit number on HomeBridges main screen as the value.

> GET /accessories HTTP/1.1
> Host: 127.0.0.1:52626
> User-Agent: insomnia/2023.5.8
> authorization: 643-36-418
> Accept: */*

Through poking around in here I found my 3D printer's aid & iid. In the HomeBridge UI it reports as aid: 3 and iid: 9 but I found that the actual iid was different - iid: 10.

You can hit the URL GET http://xxx.xxx.xxx.xxx:52626/characteristics?id=3.10 (with your actual aid & iid) to get the characteristics for your accessory. It should look something like this, with value representing on/off:

> GET /characteristics?id=3.10 HTTP/1.1
> Host: 127.0.0.1:52626
> User-Agent: insomnia/2023.5.8
> authorization: 643-36-418
> Accept: */*
{
	"characteristics": [
		{
			"aid": 3,
			"iid": 10,
			"value": 1
		}
	]
}

Finally, you can test setting this value by hitting PUT http://xxx.xxx.xxx.xxx:52626/characteristics

> PUT /characteristics HTTP/1.1
> Host: 127.0.0.1:52626
> User-Agent: insomnia/2023.5.8
> Content-Type: Application/json
> authorization: 643-36-418
> Accept: */*
> Content-Length: 50

| {"characteristics":[{"aid":3,"iid":10,"value":1}]}

Moonraker power configuration

The following is the moonraker configuration for controlling the HomeBridge accessory.

Ensure you:

[power printer]
type: http
on_url: http://xxx.xxx.xxx.xxx:52626/characteristics
off_url: http://xxx.xxx.xxx.xxx:52626/characteristics
status_url: http://xxx.xxx.xxx.xxx:52626/characteristics?id=<aid>.<iid>
locked_while_printing: true
off_when_shutdown: true
request_template:
    {% do http_request.add_header("authorization", "XXX-XX-XXX") %}
    {% if command == "on" %}
        {log_debug("Turning printer on")}
        {% do http_request.set_body({"characteristics":[{"aid":<aid>,"iid":<iid>,"value":1}]}) %}
        {% do http_request.set_method("PUT") %}
        {% do http_request.send() %}
    {% elif command == "off" %}
        {log_debug("Turning printer off")}
        {% do http_request.set_body({"characteristics":[{"aid":<aid>,"iid":<iid>,"value":0}]}) %}
        {% do http_request.set_method("PUT") %}
        {% do http_request.send() %}
    {% elif command == "status" %}
        {log_debug("Getting printer status")}
        {% do http_request.set_method("GET") %}
        {% do http_request.send() %}
    {% endif %}
    
response_template:
    {% if command in ["on", "off"] %}
        {% do async_sleep(1.0) %}
        {% do http_request.set_method("GET") %}
        {% do http_request.set_body(None) %}
        {% do http_request.set_url(urls.status) %}
        {% do http_request.send() %}
    {% endif %}

    {% set resp = http_request.last_response().json() %}    
    {% set val = resp.characteristics[0].value %}
    {% if val == 0 %}
        off
    {% elif val == 1 %}
        on
    {% else %}
        off 
    {% endif %}


This page was originally published 27 Jun 2024

Updated 477 days ago (27 Jun 2024)