Contents
A Shelly device of generation 3 or 4, or a Gen2 Pro device, integrates the fill level through a script. The script runs on the Shelly, fetches the fill level from the sensor and writes it to a virtual component that belongs to this Shelly device. It is fetched over HTTP, or over MQTT if the sensor runs in power saving mode. The same script can switch off the Shelly's relay when the fill level falls below a limit (e.g. a pump). The sensor cannot be added to the Shelly app as a device of its own, because Shelly is not a hub for third-party sensors.
In this guide, <Hauptthema> stands for whatever is entered on the sensor under Connections → Main topic. Replace it everywhere with your own value.
Requirements
- A Shelly device with scripts and virtual components: Gen3, Gen4 and the Pro devices of Gen2. Not suitable: Gen1, Gen2 “Plus” (scripts yes, virtual components no) and the Shelly Wall Display (no scripts).
- Current firmware on the Shelly.
- Sensor (firmware 4.2.8) and Shelly on the same network; the sensor with a fixed IP address.
- For the MQTT version only: an MQTT broker on the network (e.g. Mosquitto).
Setting up the sensor
- HTTP version: Just check that
http://<IP des Sensors>/measurementsshows measured values in the browser, and leave authentication on the sensor (under System) switched off (or append the credentials to the address). - MQTT version: Enter the broker on the sensor as described in the user manual.
Setup
-
Open the Shelly's web interface (
http://<IP des Shelly>). -
Create a virtual component: in the area for virtual components, Create virtual component, type Number. If you cannot find that option, this call in the browser creates it:
http://<IP des Shelly>/rpc/Virtual.Add?type="number"&config={"name":"Fuellstand","min":0,"max":100,"meta":{"ui":{"unit":"%"}}}Setting Value Type numberName FuellstandMinimum 0Maximum 100Unit %The response gives the number of the component,
{"id": 200}when created for the first time. You need this number in step 4. -
Scripts → Create script, enter a name (e.g.
fuellstand), Create. -
Paste in this code and adjust the lines marked with
<<<:// Senvolon Level Sensor -> Shelly (HTTP) let SENSOR_URL = "http://192.168.1.50/measurements"; // <<< ANPASSEN: IP des Sensors let NUMBER_ID = 200; // <<< ANPASSEN: ID der virtuellen Komponente let INTERVAL_S = 60; // Abfrage alle 60 s let SWITCH_OFF_BELOW = -1; // <<< optional: z. B. 10 = Relais 0 unter 10 % ausschalten; -1 = nie let level = Virtual.getHandle("number:" + NUMBER_ID); function onReply(result, error_code, error_message) { if (error_code !== 0 || !result || result.code !== 200) { console.log("Sensor nicht erreichbar:", error_code, error_message); return; } let d; try { d = JSON.parse(result.body); } catch (e) { console.log("Antwort ist kein JSON"); return; } if (typeof d.rel_level !== "number") { console.log("Kein Fuellstand in der Antwort"); return; } if (level) level.setValue(d.rel_level); console.log("Fuellstand", d.rel_level, "%"); if (d.rel_level < SWITCH_OFF_BELOW) { Shelly.call("Switch.Set", { id: 0, on: false }); } } function poll() { Shelly.call("HTTP.GET", { url: SENSOR_URL, timeout: 10 }, onReply); } if (!level) console.log("Virtuelle Komponente number:" + NUMBER_ID + " fehlt"); Timer.set(INTERVAL_S * 1000, true, poll); poll(); -
Save, then Start. In the script list, switch on Run on startup so the script survives a restart of the Shelly.
Quick test
- After the start, the console below the editor shows
Fuellstand 50 %(with your value), then again every minute. - The virtual component “Fuellstand” shows the same value as the sensor's main page.
- If it says “Sensor nicht erreichbar” there: check the IP address and switch off the login on the sensor.
Optional
Switching off a relay (e.g. a pump): Set SWITCH_OFF_BELOW to the limit in percent, for example 10. The script then switches relay 0 off on every query below this limit. It is not switched back on; that is up to you.
MQTT instead of HTTP (required if the sensor runs in power saving mode): The Shelly must be connected to the same broker as the sensor. On the Shelly, enter the following under the MQTT settings and then restart it:
| Setting | Value |
|---|---|
Enable MQTT (enable) |
on |
Server (server) |
<Adresse des Brokers>:1883 |
User / password (user, pass) |
only if the broker requires them |
Then use this script instead of the one above:
// Senvolon Level Sensor -> Shelly (MQTT)
let TOPIC = "stat/<Hauptthema>/REL_LEVEL"; // <<< ANPASSEN: <Hauptthema> ersetzen
let NUMBER_ID = 200; // <<< ANPASSEN: ID der virtuellen Komponente
let SWITCH_OFF_BELOW = -1; // <<< optional: z. B. 10 = Relais 0 unter 10 % ausschalten; -1 = nie
let level = Virtual.getHandle("number:" + NUMBER_ID);
function onMessage(topic, message) {
let v;
try {
v = JSON.parse(message);
} catch (e) {
return; // leere Meldung: gerade kein Messwert
}
if (typeof v !== "number") return;
if (level) level.setValue(v);
console.log("Fuellstand", v, "%");
if (v < SWITCH_OFF_BELOW) {
Shelly.call("Switch.Set", { id: 0, on: false });
}
}
if (!level) console.log("Virtuelle Komponente number:" + NUMBER_ID + " fehlt");
MQTT.subscribe(TOPIC, onMessage);
The sensor sends the fill level as retained. The script therefore gets the last value immediately at startup.
Limitations
- The fill level is not a device of its own in the Shelly app, but a value on the Shelly device on which the script runs.
- In power saving mode, the sensor responds over HTTP only when it is awake, so use the MQTT version.
- If the sensor fails, the last value stays. There is no “unreachable” indicator, only the line in the console.
- At most 10 virtual components per Shelly device.
- MQTT version: if the script starts before the Shelly is connected to the broker, or if MQTT is off on the Shelly, it aborts. In that case, check MQTT and restart the script.
Verification status
The guide is based on Shelly's documentation.