Skip to content

Commit ee92cc3

Browse files
committed
Update / Modification from the production live system: 2026-03-19 21:00
1 parent ee89e99 commit ee92cc3

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

iobroker/40_Beleuchtung/Schlafzimmer/automatisches_bettlicht.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* ==============================================================================
33
* ioBroker Script: Automatische Bettbeleuchtung
44
* ==============================================================================
5-
* @version 1.1.0
5+
* @version 1.1.3
66
* @author Sanweb
77
* @license MIT License
88
* @description
@@ -32,12 +32,14 @@
3232
// ============================================
3333
const DP = {
3434
links: {
35-
ledCmd: 'mqtt.1.zigbee2mqtt.Bettlicht_Rosie.set', // MQTT Set-Datenpunkt für links
35+
ledCmd: 'mqtt.1.zigbee2mqtt.Bettlicht Rosie.set', // ioBroker ID (ohne Unterstrich)
36+
ledTopic: 'zigbee2mqtt/Bettlicht Rosie/set', // Echtes MQTT Topic
3637
motion: 'zigbee2mqtt.0.0xa4c1387d7ee56494.presence', // Zigbee Bewegungsmelder
3738
matte: 'hm-rpc.0.001E1D899E94B0.1.STATE' // Homematic Kontaktmatte
3839
},
3940
rechts: {
40-
ledCmd: 'mqtt.1.zigbee2mqtt.Bettlicht_Alex.set', // MQTT Set-Datenpunkt für rechts
41+
ledCmd: 'mqtt.1.zigbee2mqtt.Bettlicht Alex.set', // ioBroker ID (ohne Unterstrich)
42+
ledTopic: 'zigbee2mqtt/Bettlicht Alex/set', // Echtes MQTT Topic
4143
motion: 'zigbee2mqtt.0.0xa4c138f5aeea45b6.presence', // Zigbee Bewegungsmelder
4244
matte: 'hm-rpc.0.001E1D899E922D.1.STATE' // Homematic Kontaktmatte
4345
},
@@ -75,11 +77,6 @@
7577
// Hilfsfunktionen: Logging & Timer Cleanup
7678
// ============================================
7779

78-
/**
79-
* Loggt eine Nachricht mit dem konfigurierten Präfix
80-
* @param {string} msg - Die Log-Nachricht
81-
* @param {'info' | 'warn' | 'error' | 'debug' | 'silly'} [level='info'] - Das ioBroker Log-Level
82-
*/
8380
function scriptLog(msg, level = 'info') {
8481
log(CONFIG.logPrefix + msg, level);
8582
}
@@ -106,30 +103,36 @@
106103
// ============================================
107104
scriptLog('Führe Systemprüfung der Datenpunkte durch...', 'info');
108105

109-
// MQTT Datenpunkte prüfen und ggf. automatisch anlegen
110-
const mqttDPs = [DP.links.ledCmd, DP.rechts.ledCmd];
111-
for (const id of mqttDPs) {
112-
if (!existsState(id)) {
113-
scriptLog(`MQTT Datenpunkt ${id} fehlt. Versuche Erstellung in fremdem Namensraum...`, 'warn');
106+
// MQTT Datenpunkte prüfen und ggf. automatisch mit exaktem Topic anlegen
107+
const mqttTargets = [
108+
{ id: DP.links.ledCmd, topic: DP.links.ledTopic },
109+
{ id: DP.rechts.ledCmd, topic: DP.rechts.ledTopic }
110+
];
111+
112+
for (const target of mqttTargets) {
113+
if (!existsState(target.id)) {
114+
scriptLog(`MQTT Datenpunkt ${target.id} fehlt. Wird mit Ziel-Topic "${target.topic}" angelegt...`, 'warn');
114115

115-
setObject(id, {
116+
setObject(target.id, {
116117
type: 'state',
117118
common: {
118-
name: id.split('.').pop(),
119+
name: 'set',
119120
type: 'string',
120121
role: 'state',
121122
read: true,
122123
write: true,
123124
desc: 'Automatisch angelegt durch Bettlicht-Skript'
124125
},
125-
native: {}
126+
native: {
127+
topic: target.topic // <--- HIER IST DIE MAGIE: Das exakte MQTT Topic für den Adapter!
128+
}
126129
}, function(err) {
127130
if (err) {
128-
scriptLog(`FEHLER beim Anlegen von ${id}: ${err} -> Bitte in den JavaScript-Instanz-Einstellungen "Erlaube das Kommando setObj" aktivieren!`, 'error');
131+
scriptLog(`FEHLER beim Anlegen von ${target.id}: ${err}`, 'error');
129132
} else {
130-
scriptLog(`Erfolgreich angelegt: ${id}`, 'info');
133+
scriptLog(`Erfolgreich angelegt: ${target.id}`, 'info');
131134
// Startwert setzen
132-
setState(id, JSON.stringify({ state: "OFF" }), true);
135+
setState(target.id, JSON.stringify({ state: "OFF" }), true);
133136
}
134137
});
135138
}
@@ -176,16 +179,13 @@
176179
const ledCmdId = DP[side].ledCmd;
177180
const payloadString = JSON.stringify({ state: state ? "ON" : "OFF" });
178181

179-
// Letzter gesendeter Befehl (da .set kein echter Status-DP ist, enthält er nur den letzten String)
180182
const rawCurrent = existsState(ledCmdId) ? getState(ledCmdId)?.val : null;
181183

182184
if (!CONFIG.forceSend && rawCurrent === payloadString) {
183185
debugLog(`Licht ${side}: bereits ${state ? 'AN' : 'AUS'}, blockiert durch Schonung`);
184186
return;
185187
}
186188

187-
// WICHTIG: Das 'false' als 3. Parameter ist das ack-Flag.
188-
// false = Befehl an Hardware senden (Command)
189189
setState(ledCmdId, payloadString, false);
190190
scriptLog(`Licht ${side}: ${state ? 'AN' : 'AUS'} (MQTT Payload: ${payloadString})`, 'info');
191191
}
@@ -205,7 +205,6 @@
205205
return;
206206
}
207207

208-
// Logik: Matte = leer UND Motion = true
209208
if (isMatteEmpty && isMotion) {
210209
setLight(side, true);
211210

@@ -228,7 +227,6 @@
228227
// ============================================
229228
['links', 'rechts'].forEach(side => {
230229

231-
// Trigger: Bewegung
232230
on({ id: DP[side].motion, change: 'any' }, function(obj) {
233231
const motionVal = parseBool(obj.state?.val);
234232
const isOccupied = getMatteOccupiedState(getState(DP[side].matte)?.val);
@@ -238,7 +236,6 @@
238236
processSide(side, motionVal, isOccupied, nachtVal);
239237
});
240238

241-
// Trigger: Kontaktmatte
242239
on({ id: DP[side].matte, change: 'any' }, function(obj) {
243240
const isOccupied = getMatteOccupiedState(obj.state?.val);
244241
const motionVal = parseBool(getState(DP[side].motion)?.val);

0 commit comments

Comments
 (0)