Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. flachdachdecker

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    F
    • Profile
    • Following 0
    • Followers 0
    • Topics 8
    • Posts 24
    • Best 2
    • Groups 1

    flachdachdecker

    @flachdachdecker

    Starter

    2
    Reputation
    20
    Profile views
    24
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    flachdachdecker Follow
    Starter

    Best posts made by flachdachdecker

    • Zigbee LAN coordinator CC2652P SMLIGHT SLZB-0 einbinden

      Hallo Leute,

      ich spiele schon ein paar Tage mit dem Zigbee LAN coordinator CC2652P SMLIGHT SLZB-0 herum - leider ohne Erfolg.

      Die URL des coordinators ist vom IObroker Host wunderbar über curl erreichbar.

      Der Zigbee Adapter mag aber nicht:

      zigbee.0
      2022-05-29 15:10:00.179 error Error herdsman start
      zigbee.0
      2022-05-29 15:10:00.179 error Failed to start Zigbee
      zigbee.0
      2022-05-29 15:10:00.178 error Starting zigbee-herdsman problem : "Error while opening socket"
      zigbee.0
      2022-05-29 15:10:00.174 info Starting Zigbee npm ...
      zigbee.0
      2022-05-29 15:10:00.173 info Try to reconnect.
      zigbee.0
      2022-05-29 15:09:50.175 info Installed Version: iobroker.zigbee@1.6.18

      Als "comm-Adaper" habe ich tcp://192.168.5.101:80 (IP des Coordinators) gewählt, als Typ alle verfügbaren Typen durchgespielt.

      Hat jemand einen Tipp für mich wie ich das Gerät in den IObroker (evtl. mit einem anderem Adapter) einbinden kann?

      Grüße,
      Flachdachdecker

      posted in Hardware
      F
      flachdachdecker
    • iobroker -> MQTT -> HomeAssistent AutoDiscovery

      Hallo liebes IObroker Forum,

      aus diversen Gründen habe ih neben meinem geliebten IOB auch noch eine HomeAssistant Installation laufen.

      Mit dem folgenden Script ist es möglich IObroker states AUTOMATISCH via Autodiscovery im HomeAssistent sichtbar zu machen - ohne umtändlich in YAML files rumfuhrwerken zu müssen und ohne im IObrocker an jedem State das pub & sub zu aktivieren. .

      Dazu setzt man einfach an einem State die Funktion "homeassistent_enabled":

      3223858b-7619-4b90-80ab-0dfea6e6016f-image.png

      Das Script scannt nach diesen Objekten und published sie via MQTT (ein Broker entweder in HA oder extern ist notwendig) an den HA:
      55fed696-d532-4bc3-9ade-6de972b0e000-image.png

      Im JS-Adapter muss als zusätzliches Modul "mqtt" geladen werden:
      9b9b56c8-ba87-4a84-aed6-678cc384e892-image.png

      Hier das script (Probleme siehe unten):

      // Required libraries
      const mqtt = require('mqtt');
      
      // Configuration
      const MQTT_BROKER_URL = 'mqtt://<IP>:<PORT>'; // Replace with your broker's address
      const MQTT_USERNAME = '<username>'; // Replace with your MQTT username
      const MQTT_PASSWORD = '<password>'; // Replace with your MQTT password
      const DEBUG = true; // Set to true for debugging output
      
      // MQTT client setup
      const mqttClient = mqtt.connect(MQTT_BROKER_URL, {
          username: MQTT_USERNAME,
          password: MQTT_PASSWORD
      });
      
      mqttClient.on('connect', () => {
          console.log('Connected to MQTT broker');
      });
      
      mqttClient.on('error', (err) => {
          console.error('MQTT connection error:', err);
      });
      
      // Function to log debug information
      function debugLog(message) {
          if (DEBUG) {
              console.log(message);
          }
      }
      
      // Function to determine the type of a state
      function determineStateType(stateObj) {
          if (typeof stateObj.val === 'boolean') {
              return 'switch';
          } else if (typeof stateObj.val === 'number') {
              return 'sensor';
          } else if (typeof stateObj.val === 'string') {
              return 'text';
          } else {
              return 'unknown';
          }
      }
      
      // Function to publish MQTT discovery messages
      function publishMQTTDiscovery(id, stateObj, deviceName) {
          const baseTopic = `homeassistant`;
          const deviceId = id.replace(/\./g, '_');
          const stateType = determineStateType(stateObj);
      
          let configTopic;
          let configPayload;
      
          switch (stateType) {
              case 'switch':
                  configTopic = `${baseTopic}/switch/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id, // Use the provided deviceName or fallback to id
                      state_topic: `${baseTopic}/switch/${deviceId}/state`, 
                      command_topic: `${baseTopic}/switch/${deviceId}/set`,
                      payload_on: true,
                      payload_off: false,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id, // Use deviceName for the device itself
                      },
                  };
                  break;
              case 'sensor':
                  configTopic = `${baseTopic}/sensor/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id,
                      state_topic: `${baseTopic}/sensor/${deviceId}/state`,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id,
                      },
                  };
                  break;
              case 'text':
                  configTopic = `${baseTopic}/text/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id,
                      state_topic: `${baseTopic}/text/${deviceId}/state`,
                      command_topic: `${baseTopic}/text/${deviceId}/set`,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id,
                      },
                  };
                  break;
              default:
                  debugLog(`Unknown state type for ${id}`);
                  return;
          }
      
          mqttClient.publish(configTopic, JSON.stringify(configPayload), { retain: true });
      
          // Publish initial state
          const stateTopic = configPayload.state_topic;
          mqttClient.publish(stateTopic, JSON.stringify(stateObj.val));
      
          // Subscribe to command topic if applicable
          if (configPayload.command_topic) {
              mqttClient.subscribe(configPayload.command_topic);
              mqttClient.on('message', (topic, message) => {
                  if (topic === configPayload.command_topic) {
                      try {
                          // Check if the message is JSON
                          let newValue;
                          if (message.toString().startsWith('{') || message.toString().startsWith('[')) {
                              newValue = JSON.parse(message.toString());
                          } else {
                              // Handle non-JSON payloads (e.g., "true", "false", "42")
                              newValue = message.toString().trim().toLowerCase();
                              if (newValue === 'true') newValue = true;
                              else if (newValue === 'false') newValue = false;
                              else if (!isNaN(newValue)) newValue = parseFloat(newValue);
                          }
      
                          setState(id, newValue); // Update ioBroker state
                      } catch (err) {
                          console.error(`Failed to process MQTT message on ${topic}:`, err);
                      }
                  }
              });
          }
      
          debugLog(`Published MQTT discovery for ${id}: ${JSON.stringify(configPayload)}`);
      }
      
      
      function scanAndPublish() {
          const homeAssistantDevices = getObject('enum.functions.homeassistent_enabled');
      
          if (homeAssistantDevices) {
              console.log('Devices with homeassistent_enabled function:');
              homeAssistantDevices.common.members.forEach(deviceId => {
                  const deviceObj = getObject(deviceId); // Fetch the device object
                  const stateObj = getState(deviceId); // Fetch the state of the device
                  
                  if (deviceObj && stateObj) {
                      console.log(`- ${deviceObj.common.name} (${deviceId})`);
                      // Pass the device name as an additional argument
                      publishMQTTDiscovery(deviceId, stateObj, deviceObj.common.name);
                  } else {
                      console.log(`Skipping device ${deviceId}: Unable to retrieve object or state.`);
                  }
              });
          } else {
              console.log('No devices found with homeassistent_enabled function');
          }
      }
      
      
      // Run the script periodically
      schedule('*/1 * * * *', () => {
        debugLog('Scanning for states with homeassistent_enabled...');
      scanAndPublish();
      });
      
      

      Soweit so gut - ein Problemchen habe ich noch:
      Wenn im HA einen der "switches" betätige schaltet es im IOB wie es soll, die Statusrückmeldung kommt aber entweder nicht im HA an oder bleibt nicht erhalten. Die Icons im HA werden also nicht blau (was für aktiviert steht). Hier ein Debug der herauskommt wenn ich im HA dem "sonoff_0_zwstecker_1_weihnachtsbaum_power" auf "true" schalte:

      javascript.1	08:09:13.401	info	Start JavaScript script.js.in_Entwicklung.MQTT2 (Javascript/js)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Scanning for states with homeassistent_enabled...
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Devices with homeassistent_enabled function:
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: - Lüfter Elternbad.STATE (hm-rpc.0.LEQ0233457.13.STATE)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for hm-rpc.0.LEQ0233457.13.STATE: {"name":"Lüfter Elternbad.STATE","state_topic":"homeassistant/switch/hm-rpc_0_LEQ0233457_13_STATE/state","command_topic":"homeassistant/switch/hm-rpc_0_LEQ0233457_13_STATE/set","payload_on":true,"payload_off":false,"unique_id":"hm-rpc_0_LEQ0233457_13_STATE","device":{"identifiers":["hm-rpc_0_LEQ0233457_13_STATE"],"name":"Lüfter Elternbad.STATE"}}
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: - Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE (hm-rpc.2.000C9A49A7D845.1.ACTUAL_TEMPERATURE)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for hm-rpc.2.000C9A49A7D845.1.ACTUAL_TEMPERATURE: {"name":"Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE","state_topic":"homeassistant/sensor/hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE/state","unique_id":"hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE","device":{"identifiers":["hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE"],"name":"Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE"}}
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: - ZwStecker_1_Weihnachtsbaum POWER (sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER)
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"name":"ZwStecker_1_Weihnachtsbaum POWER","state_topic":"homeassistant/switch/sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER/state","command_topic":"homeassistant/switch/sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER/set","payload_on":true,"payload_off":false,"unique_id":"sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER","device":{"identifiers":["sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER"],"name":"ZwStecker_1_Weihnachtsbaum POWER"}}
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: - On/off state of the switch (zigbee.0.4c97a1fffe27ff06.state)
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for zigbee.0.4c97a1fffe27ff06.state: {"name":"On/off state of the switch","state_topic":"homeassistant/switch/zigbee_0_4c97a1fffe27ff06_state/state","command_topic":"homeassistant/switch/zigbee_0_4c97a1fffe27ff06_state/set","payload_on":true,"payload_off":false,"unique_id":"zigbee_0_4c97a1fffe27ff06_state","device":{"identifiers":["zigbee_0_4c97a1fffe27ff06_state"],"name":"On/off state of the switch"}}
      javascript.1	08:09:13.407	info	script.js.in_Entwicklung.MQTT2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions
      javascript.1	08:09:13.408	info	script.js.in_Entwicklung.MQTT2: Connected to MQTT broker
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.342	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.344	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.384	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.384	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      

      Hat dazu jemand eine Idee?

      Und hat jemand weitere Ideen was man verbessern könnte?

      posted in JavaScript
      F
      flachdachdecker

    Latest posts made by flachdachdecker

    • RE: [gelöst] node.js updaten

      Danke an euch beide - das war es. Ich habe immer auf "buanet/iobroker" und nicht auf ein bestimmtes versionstag geziehlt.

      posted in Docker
      F
      flachdachdecker
    • [gelöst] node.js updaten

      Hallo zusammen,
      wie kann ich eigentlich node.js updaten?

      Über das gui wird es nicht angeboten. Mit dem docker Container scheint es auch nicht mit zu kommen.

      Ich frage weil der Mqtt-Client sich gerne auf Version 3.0 updaten würde, welche aber Bode 20 fordert. Screenshot_20250423_163219_Firefox.png

      posted in Docker
      F
      flachdachdecker
    • iobroker -> MQTT -> HomeAssistent AutoDiscovery

      Hallo liebes IObroker Forum,

      aus diversen Gründen habe ih neben meinem geliebten IOB auch noch eine HomeAssistant Installation laufen.

      Mit dem folgenden Script ist es möglich IObroker states AUTOMATISCH via Autodiscovery im HomeAssistent sichtbar zu machen - ohne umtändlich in YAML files rumfuhrwerken zu müssen und ohne im IObrocker an jedem State das pub & sub zu aktivieren. .

      Dazu setzt man einfach an einem State die Funktion "homeassistent_enabled":

      3223858b-7619-4b90-80ab-0dfea6e6016f-image.png

      Das Script scannt nach diesen Objekten und published sie via MQTT (ein Broker entweder in HA oder extern ist notwendig) an den HA:
      55fed696-d532-4bc3-9ade-6de972b0e000-image.png

      Im JS-Adapter muss als zusätzliches Modul "mqtt" geladen werden:
      9b9b56c8-ba87-4a84-aed6-678cc384e892-image.png

      Hier das script (Probleme siehe unten):

      // Required libraries
      const mqtt = require('mqtt');
      
      // Configuration
      const MQTT_BROKER_URL = 'mqtt://<IP>:<PORT>'; // Replace with your broker's address
      const MQTT_USERNAME = '<username>'; // Replace with your MQTT username
      const MQTT_PASSWORD = '<password>'; // Replace with your MQTT password
      const DEBUG = true; // Set to true for debugging output
      
      // MQTT client setup
      const mqttClient = mqtt.connect(MQTT_BROKER_URL, {
          username: MQTT_USERNAME,
          password: MQTT_PASSWORD
      });
      
      mqttClient.on('connect', () => {
          console.log('Connected to MQTT broker');
      });
      
      mqttClient.on('error', (err) => {
          console.error('MQTT connection error:', err);
      });
      
      // Function to log debug information
      function debugLog(message) {
          if (DEBUG) {
              console.log(message);
          }
      }
      
      // Function to determine the type of a state
      function determineStateType(stateObj) {
          if (typeof stateObj.val === 'boolean') {
              return 'switch';
          } else if (typeof stateObj.val === 'number') {
              return 'sensor';
          } else if (typeof stateObj.val === 'string') {
              return 'text';
          } else {
              return 'unknown';
          }
      }
      
      // Function to publish MQTT discovery messages
      function publishMQTTDiscovery(id, stateObj, deviceName) {
          const baseTopic = `homeassistant`;
          const deviceId = id.replace(/\./g, '_');
          const stateType = determineStateType(stateObj);
      
          let configTopic;
          let configPayload;
      
          switch (stateType) {
              case 'switch':
                  configTopic = `${baseTopic}/switch/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id, // Use the provided deviceName or fallback to id
                      state_topic: `${baseTopic}/switch/${deviceId}/state`, 
                      command_topic: `${baseTopic}/switch/${deviceId}/set`,
                      payload_on: true,
                      payload_off: false,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id, // Use deviceName for the device itself
                      },
                  };
                  break;
              case 'sensor':
                  configTopic = `${baseTopic}/sensor/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id,
                      state_topic: `${baseTopic}/sensor/${deviceId}/state`,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id,
                      },
                  };
                  break;
              case 'text':
                  configTopic = `${baseTopic}/text/${deviceId}/config`;
                  configPayload = {
                      name: deviceName || id,
                      state_topic: `${baseTopic}/text/${deviceId}/state`,
                      command_topic: `${baseTopic}/text/${deviceId}/set`,
                      unique_id: deviceId,
                      device: {
                          identifiers: [deviceId],
                          name: deviceName || id,
                      },
                  };
                  break;
              default:
                  debugLog(`Unknown state type for ${id}`);
                  return;
          }
      
          mqttClient.publish(configTopic, JSON.stringify(configPayload), { retain: true });
      
          // Publish initial state
          const stateTopic = configPayload.state_topic;
          mqttClient.publish(stateTopic, JSON.stringify(stateObj.val));
      
          // Subscribe to command topic if applicable
          if (configPayload.command_topic) {
              mqttClient.subscribe(configPayload.command_topic);
              mqttClient.on('message', (topic, message) => {
                  if (topic === configPayload.command_topic) {
                      try {
                          // Check if the message is JSON
                          let newValue;
                          if (message.toString().startsWith('{') || message.toString().startsWith('[')) {
                              newValue = JSON.parse(message.toString());
                          } else {
                              // Handle non-JSON payloads (e.g., "true", "false", "42")
                              newValue = message.toString().trim().toLowerCase();
                              if (newValue === 'true') newValue = true;
                              else if (newValue === 'false') newValue = false;
                              else if (!isNaN(newValue)) newValue = parseFloat(newValue);
                          }
      
                          setState(id, newValue); // Update ioBroker state
                      } catch (err) {
                          console.error(`Failed to process MQTT message on ${topic}:`, err);
                      }
                  }
              });
          }
      
          debugLog(`Published MQTT discovery for ${id}: ${JSON.stringify(configPayload)}`);
      }
      
      
      function scanAndPublish() {
          const homeAssistantDevices = getObject('enum.functions.homeassistent_enabled');
      
          if (homeAssistantDevices) {
              console.log('Devices with homeassistent_enabled function:');
              homeAssistantDevices.common.members.forEach(deviceId => {
                  const deviceObj = getObject(deviceId); // Fetch the device object
                  const stateObj = getState(deviceId); // Fetch the state of the device
                  
                  if (deviceObj && stateObj) {
                      console.log(`- ${deviceObj.common.name} (${deviceId})`);
                      // Pass the device name as an additional argument
                      publishMQTTDiscovery(deviceId, stateObj, deviceObj.common.name);
                  } else {
                      console.log(`Skipping device ${deviceId}: Unable to retrieve object or state.`);
                  }
              });
          } else {
              console.log('No devices found with homeassistent_enabled function');
          }
      }
      
      
      // Run the script periodically
      schedule('*/1 * * * *', () => {
        debugLog('Scanning for states with homeassistent_enabled...');
      scanAndPublish();
      });
      
      

      Soweit so gut - ein Problemchen habe ich noch:
      Wenn im HA einen der "switches" betätige schaltet es im IOB wie es soll, die Statusrückmeldung kommt aber entweder nicht im HA an oder bleibt nicht erhalten. Die Icons im HA werden also nicht blau (was für aktiviert steht). Hier ein Debug der herauskommt wenn ich im HA dem "sonoff_0_zwstecker_1_weihnachtsbaum_power" auf "true" schalte:

      javascript.1	08:09:13.401	info	Start JavaScript script.js.in_Entwicklung.MQTT2 (Javascript/js)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Scanning for states with homeassistent_enabled...
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Devices with homeassistent_enabled function:
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: - Lüfter Elternbad.STATE (hm-rpc.0.LEQ0233457.13.STATE)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for hm-rpc.0.LEQ0233457.13.STATE: {"name":"Lüfter Elternbad.STATE","state_topic":"homeassistant/switch/hm-rpc_0_LEQ0233457_13_STATE/state","command_topic":"homeassistant/switch/hm-rpc_0_LEQ0233457_13_STATE/set","payload_on":true,"payload_off":false,"unique_id":"hm-rpc_0_LEQ0233457_13_STATE","device":{"identifiers":["hm-rpc_0_LEQ0233457_13_STATE"],"name":"Lüfter Elternbad.STATE"}}
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: - Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE (hm-rpc.2.000C9A49A7D845.1.ACTUAL_TEMPERATURE)
      javascript.1	08:09:13.405	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for hm-rpc.2.000C9A49A7D845.1.ACTUAL_TEMPERATURE: {"name":"Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE","state_topic":"homeassistant/sensor/hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE/state","unique_id":"hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE","device":{"identifiers":["hm-rpc_2_000C9A49A7D845_1_ACTUAL_TEMPERATURE"],"name":"Termostat Wohnzimmer:1.ACTUAL_TEMPERATURE"}}
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: - ZwStecker_1_Weihnachtsbaum POWER (sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER)
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"name":"ZwStecker_1_Weihnachtsbaum POWER","state_topic":"homeassistant/switch/sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER/state","command_topic":"homeassistant/switch/sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER/set","payload_on":true,"payload_off":false,"unique_id":"sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER","device":{"identifiers":["sonoff_0_ZwStecker_1_Weihnachtsbaum_POWER"],"name":"ZwStecker_1_Weihnachtsbaum POWER"}}
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: - On/off state of the switch (zigbee.0.4c97a1fffe27ff06.state)
      javascript.1	08:09:13.406	info	script.js.in_Entwicklung.MQTT2: Published MQTT discovery for zigbee.0.4c97a1fffe27ff06.state: {"name":"On/off state of the switch","state_topic":"homeassistant/switch/zigbee_0_4c97a1fffe27ff06_state/state","command_topic":"homeassistant/switch/zigbee_0_4c97a1fffe27ff06_state/set","payload_on":true,"payload_off":false,"unique_id":"zigbee_0_4c97a1fffe27ff06_state","device":{"identifiers":["zigbee_0_4c97a1fffe27ff06_state"],"name":"On/off state of the switch"}}
      javascript.1	08:09:13.407	info	script.js.in_Entwicklung.MQTT2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions
      javascript.1	08:09:13.408	info	script.js.in_Entwicklung.MQTT2: Connected to MQTT broker
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.342	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367340,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.343	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.343	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.344	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.384	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	silly	States user redis pmessage sonoff.0.*/sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	stateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      sonoff.0	08:09:27.384	debug	onStateChange sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER: {"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      javascript.1	08:09:27.384	silly	States user redis pmessage */sonoff.0.ZwStecker_1_Weihnachtsbaum.POWER:{"val":true,"ack":false,"ts":1734419367341,"q":0,"c":"script.js.in_Entwicklung.MQTT2","from":"system.adapter.javascript.1","user":"system.user.admin","lc":1734419367340}
      

      Hat dazu jemand eine Idee?

      Und hat jemand weitere Ideen was man verbessern könnte?

      posted in JavaScript
      F
      flachdachdecker
    • RE: Gelöst: states nach funktionen ausprinten

      Danke euch beiden!

      falls jemand anders sucht - so gehts:

      const homeAssistantDevices = getObject('enum.functions.homeassistent_enabled');
       
      if (homeAssistantDevices) {
          console.log('Devices with homeassistent_enabled function:');
          homeAssistantDevices.common.members.forEach(deviceId => {
              const deviceObj = getObject(deviceId);
              console.log(`- ${deviceObj.common.name} ${deviceId}`);
         });
      } else {
          console.log('No devices found with homeassistent_enabled function');
      }
      
      posted in JavaScript
      F
      flachdachdecker
    • Gelöst: states nach funktionen ausprinten

      Hallo Zusammen,

      ich stehe mal wieder auf dem Schlauch.

      Ich haben eine Function angelegt und member reingepackt:

      e2b5d0b4-9e29-4dd9-9ae6-d0805469d067-image.png

      Nun will ich diese via getEnums anzeigen:

      const homeAssistantDevices = getEnums('enum.functions.homeassistent_enabled');
      
      if (homeAssistantDevices && homeAssistantDevices.members) {
          console.log('Devices with homeassistent_enabled function:');
          homeAssistantDevices.members.forEach(deviceId => {
              const deviceObj = getObject(deviceId);
              console.log(`- ${deviceObj.common.name} ${deviceId}`);
         });
      } else {
          console.log('No devices found with homeassistent_enabled function');
      }
      

      Leider findet mein schickes (von chatGPT und Perplexity) unterstützes Script die Geräte nicht:

      javascript.0	18:21:03.077	info	Stopping script script.js.in_Entwicklung.Skript_122
      javascript.0	18:21:03.164	info	Start JavaScript script.js.in_Entwicklung.Skript_122 (Javascript/js)
      javascript.0	18:21:03.175	info	script.js.in_Entwicklung.Skript_122: No devices found with homeassistent_enabled function
      javascript.0	18:21:03.175	info	script.js.in_Entwicklung.Skript_122: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions
      

      Kann mir jemand helfen?

      posted in JavaScript
      F
      flachdachdecker
    • RE: [gelöst] JS + Zigbee ZG-101ZL - Event immer doppelt.

      @paul53 said in JS + Zigbee ZG-101ZL - Event immer doppelt.:

      , val: true

      Paul, wie immer ist auf dich Verlass! Vielen Dank, das hat geholfen!

      posted in JavaScript
      F
      flachdachdecker
    • RE: [gelöst] JS + Zigbee ZG-101ZL - Event immer doppelt.
      on({id: 'zigbee.0.a4c1384eb9282fc6.single'/*single*/}, function (obj) {
          log("button 3 pressed (single) "+ obj.state.val);
      }); 
      
      on({id: 'zigbee.0.a4c1384eb9282fc6.double'/*double*/}, function (obj) {
          log("button 3 pressed (double) "+ obj.state.val);
      }); 
      

      -> einmal "Klick"

      javascript.0	17:17:09.551	info	script.js.in_Entwicklung.Zigbee_Button: button 3 pressed (single) true
      javascript.0	17:17:09.804	info	script.js.in_Entwicklung.Zigbee_Button: button 3 pressed (single) false
      

      -> doppel "Klick"

      javascript.0	17:17:11.783	info	script.js.in_Entwicklung.Zigbee_Button: button 3 pressed (double) true
      javascript.0	17:17:12.041	info	script.js.in_Entwicklung.Zigbee_Button: button 3 pressed (double) false
      
      posted in JavaScript
      F
      flachdachdecker
    • RE: [gelöst] JS + Zigbee ZG-101ZL - Event immer doppelt.

      Nachtrag: ich habe 2 weitere Buttons dieses Typs angebunden - mit deim gleichen Verhalten.

      Also entweder "muss das so" oder meine Zigbee-Config ist murks.

      posted in JavaScript
      F
      flachdachdecker
    • [gelöst] JS + Zigbee ZG-101ZL - Event immer doppelt.

      Hallo Zusammen,

      ich mache gerade meine ersten Gehversuche mit Zigbee auf IObroker mit dem v1.10.3 Zigbee Adapter.

      An meinen DECONS II habe ich einen ZG-101ZL angelernt.

      Mein Problem ist, dass bei folgendem Code immer 2x das Event ausgeführt wird - in diesem Fall erscheint 2x "button pressed" in der Console.

      on({id: 'zigbee.0.a4c1389b51ba52b2.on'/*on*/}, function (obj) {
          log("button pressed");
      });
      

      Es ist dabei egal ob ich *.on, *.single, oder *.double anwähle.

      javascript.0	16:08:17.639	info	script.js.in_Entwicklung.Zigbee_Button: button pressed
      javascript.0	16:08:17.896	info	script.js.in_Entwicklung.Zigbee_Button: button pressed
      

      Habt ihr dazu eine Idee?

      posted in JavaScript
      F
      flachdachdecker
    • RE: [gelöst] MQTT topic auf server schreiben

      @haus-automatisierung wunderbar - mit dem "MQTT Broker/Client"-Adapter hats funktioniert 🙂 Danke!

      posted in Einsteigerfragen
      F
      flachdachdecker
    Community
    Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
    The ioBroker Community 2014-2023
    logo