Skip to content
Get the appGet app

openHAB

openHAB can update Aivi from a rule whenever an item changes. The one catch: openHAB’s built-in HTTP actions send GET, PUT, POST, and DELETE, but not the PATCH request Aivi expects. The rule below uses Java’s own HTTP client instead, from the JavaScript Scripting add-on.

This setup is tested against openHAB 5.1.

This example keeps a Gauge widget with the slug living-room in sync with a temperature item.

  1. In the Aivi app’s Widgets tab, create a widget with the Gauge template and the slug living-room. Copy your API token from the Settings tab.

  2. In openHAB, open Settings > Add-on Store > Automation and install JavaScript Scripting.

  3. Use an existing Number item, or define one in conf/items/aivi.items:

    Number LivingRoom_Temperature "Living room temperature [%.1f °C]"
  4. Save the rule as conf/automation/js/aivi.js, with your item name, slug, and token filled in:

    // Keeps an Aivi Gauge widget in sync with an openHAB Number item.
    // openHAB's HTTP actions have no PATCH, so this uses Java's HTTP client.
    const { rules, triggers } = require("openhab");
    const AIVI_TOKEN = "[token]";
    const ITEM = "LivingRoom_Temperature";
    const SLUG = "living-room";
    const HttpClient = Java.type("java.net.http.HttpClient");
    const HttpRequest = Java.type("java.net.http.HttpRequest");
    const HttpResponse = Java.type("java.net.http.HttpResponse");
    const URI = Java.type("java.net.URI");
    const client = HttpClient.newHttpClient();
    rules.JSRule({
    name: "Aivi: update the living-room widget",
    triggers: [triggers.ItemStateChangeTrigger(ITEM)],
    execute: (event) => {
    const body = JSON.stringify({
    content: {
    template: "gauge",
    value: Number.parseFloat(event.newState),
    min_value: 10,
    max_value: 35,
    unit: "°C",
    label: "Living room",
    icon: "thermometer.medium",
    },
    });
    const request = HttpRequest.newBuilder(URI.create(`https://api.getaivi.app/widget/${SLUG}`))
    .header("Authorization", `Token ${AIVI_TOKEN}`)
    .header("Content-Type", "application/json")
    .method("PATCH", HttpRequest.BodyPublishers.ofString(body))
    .build();
    const response = client.send(request, HttpResponse.BodyHandlers.ofString());
    if (response.statusCode() >= 300) {
    console.warn(`Aivi update failed: ${response.statusCode()} ${response.body()}`);
    }
    },
    });
  5. openHAB loads the file on its own. The log shows Adding rule: Aivi: update the living-room widget, and the widget updates the next time the item changes.

To show something else, change the content to another widget template. To drive a Live Activity instead, send to /activity/{slug} with a state and a Live Activity template; see Update an activity.

Failed updates are logged as Aivi update failed: <status> <details>:

  • 401: the token is missing or mistyped.
  • 402: the token is valid, but the account has no active subscription.
  • 404: no widget with that slug exists. Create it in the app first.
  • 422: the content doesn’t fit the template, for example an item state of NULL or UNDEF that isn’t a number.

See Errors for the full list.