# openHAB iPhone Widgets and Live Activities with Aivi

> Keep an Aivi widget or Live Activity in sync with an openHAB item using a JavaScript Scripting rule, since openHAB's HTTP actions cannot send PATCH.

Source: https://docs.getaivi.app/guides/openhab/

openHAB can update Aivi from a rule whenever an item changes. The one catch: openHAB’s built-in [HTTP actions](https://www.openhab.org/docs/configuration/actions.html#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.

## Keep a widget in sync with an item

This example keeps a [Gauge widget](https://docs.getaivi.app/widget-templates/#gauge-template) 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`:

   ```text
   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:

   ```js
   // 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/widgets/${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.

The token sits in plain text in the rule file, so keep `conf/automation` readable only by the openHAB user.

To show something else, change the `content` to another [widget template](https://docs.getaivi.app/widget-templates/). To drive a Live Activity instead, send to `/activities/{slug}` with a `state` and a Live Activity template; see [Update an activity](https://docs.getaivi.app/api-reference/#update-an-activity).

## Troubleshooting

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](https://docs.getaivi.app/api-reference/#errors) for the full list.
