Skip to content
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Standard: (Kein Skin)
  • Kein Skin
Einklappen
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. [Vorlage] Xiaomi Airpurifier 3H u.a. inkl. Token auslesen.

NEWS

  • Neuer Blogbeitrag: Monatsrückblick - Dezember 2025 🎄
    BluefoxB
    Bluefox
    11
    1
    324

  • Weihnachtsangebot 2025! 🎄
    BluefoxB
    Bluefox
    24
    1
    1.5k

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    9.5k

[Vorlage] Xiaomi Airpurifier 3H u.a. inkl. Token auslesen.

Geplant Angeheftet Gesperrt Verschoben JavaScript
474 Beiträge 50 Kommentatoren 101.6k Aufrufe 41 Watching
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • N NebuNC2

    Hallo Zusammen,

    ich bin einfach zu blöd... Versuche den 3H einzubinden und bekomme gar nichts hin...

    Habe es zwar hinbekommen das Skript einzubauen, bekomme aber folgende Fehlermeldung:
    script.js.MI_AIR_TRISTAN: Error: Cannot find module '/opt/iobroker/node_modules/iobroker.javascript/lib/../..//opt/iobroker/node_modules/iobroker.javascript/node_modules/node-mihome/lib/devices/'

    Obwohl ich über wget hinzugefügt habe, denke ich...
    wget.JPG

    Ich weiß auch gar nicht wo ich meine Logindaten eintragen muss... :cry:

    Könnt ihr helfen?

    Siggi0904S Offline
    Siggi0904S Offline
    Siggi0904
    schrieb am zuletzt editiert von
    #348

    @nebunc2 hast du node-mihome im javascript Editor aktiviert?
    514af006-aed4-46f6-a9bd-5e652d88779c-grafik.png

    Schau dir auch mal die heruntergeladene Datei an, dass das auch ein js Datei ist.

    Ansonsten bitte mal ein vollständiges log.

    N 1 Antwort Letzte Antwort
    0
    • Siggi0904S Siggi0904

      @nebunc2 hast du node-mihome im javascript Editor aktiviert?
      514af006-aed4-46f6-a9bd-5e652d88779c-grafik.png

      Schau dir auch mal die heruntergeladene Datei an, dass das auch ein js Datei ist.

      Ansonsten bitte mal ein vollständiges log.

      N Offline
      N Offline
      NebuNC2
      schrieb am zuletzt editiert von
      #349

      @siggi0904
      Ja, ist eingetragen:
      java.JPG

      Sollte eine JS Datei sein:
      datei.JPG

      Wie mache ich ein vollständiges Log ?

      N 1 Antwort Letzte Antwort
      0
      • N NebuNC2

        @siggi0904
        Ja, ist eingetragen:
        java.JPG

        Sollte eine JS Datei sein:
        datei.JPG

        Wie mache ich ein vollständiges Log ?

        N Offline
        N Offline
        NebuNC2
        schrieb am zuletzt editiert von NebuNC2
        #350

        5.2.2022, 21:38:59.105 [info ]: javascript.0 (1230) Stop script script.js.MI_AIR_TRISTAN
        5.2.2022, 21:39:26.457 [info ]: javascript.0 (1230) Start javascript script.js.MI_AIR_TRISTAN
        5.2.2022, 21:39:26.497 [error]: javascript.0 (1230) script.js.MI_AIR_TRISTAN: Error: Cannot find module '/opt/iobroker/node_modules/iobroker.javascript/lib/../../../device-miio'
        5.2.2022, 21:39:26.498 [error]: javascript.0 (1230) at script.js.MI_AIR_TRISTAN:1:16
        5.2.2022, 21:39:26.498 [error]: javascript.0 (1230) at script.js.MI_AIR_TRISTAN:129:3
        5.2.2022, 21:39:26.500 [info ]: javascript.0 (1230) script.js.MI_AIR_TRISTAN: registered 0 subscriptions and 0 schedules
        5.2.2022, 21:39:26.501 [error]: javascript.0 (1230) script.js.MI_AIR_TRISTAN: ReferenceError: module is not defined
        5.2.2022, 21:39:26.501 [error]: javascript.0 (1230) at script.js.MI_AIR_TRISTAN:3:1
        5.2.2022, 21:39:26.501 [error]: javascript.0 (1230) at script.js.MI_AIR_TRISTAN:129:3

        1 Antwort Letzte Antwort
        0
        • F fd

          @fd Für alle, die ein ähnliches Problem haben, dass sie das Skript leider nicht zum Laufen bekommen: Ich habe das ganze jetzt über den ham adapter gelöst.

          Folgendermaßen bin ich vorgegangen:

          1. Via SSH "homebridge" und "homebridge xiaomi fan" installieren (siehe z.B. https://github.com/merdok/homebridge-xiaomi-fan für weitere Anmerkungen)
          2. ham Adapter für den iobroker installieren
          3. Im ham Adapter unter "Main Settings" "Local Mode (...)" wählen
          4. Unter "Main Settings" "Additional NPM modules" "homebridge-xiaomi-fan" hinzufügen
          5. Im ham Adapter unter "Configuration File" die in https://github.com/merdok/homebridge-xiaomi-fan beschriebenen Eintragungen vornehmen. Bei mir sieht das zB so aus (mit einigen Dummies und ohne Sonos):
          {
            "bridge": {
              "name": "TEST",
              "username": "00:11:22:33:44:55",
              "port": 51826,
              "pin": "000-00-001"
            },
            "platforms": [
              {
                "name": "Config",
                "port": 8090,
                "auth": "form",
                "theme": "dark-mode",
                "tempUnits": "c",
                "lang": "auto",
                "sudo": false,
                "log": {
                  "method": "file",
                  "path": "/var/log/homebridge_ioB.log"
                },
                "platform": "config"
              },
              {
                "devices": [
                  {
                    "name": "Mi Smart Standing Fan 2",
                    "ip": "192.xxx.xxx.xxx",
                    "token": "xxxxx",
                    "model": "dmaker.fan.p18",
                    "pollingInterval": 10,
                    "buzzerControl": true,
                    "ledControl": true,
                    "naturalModeControl": true,
                    "sleepModeControl": true,
                    "moveControl": true,
                    "shutdownTimer": true,
                    "angleButtons": [
                      30,
                      60,
                      90,
                      120,
                      140
                    ],
                    "ioniserControl": false
                  }
                ],
                "platform": "xiaomifan"
              }
            ],
            "accessories": []
          }
          

          Diese Config-Datei kann mit anderen Einträgen (bei mir zB für Sonos) kombiniert werden. Den Token, das Modell und die IP Adresse des Ventilators kann einfach über die hier hinterlegte App ausgelesen werden: https://github.com/Maxmudjon/Get_MiHome_devices_token/releases
          6. den ham Adapter neu starten (passiert eigentlich automatisch)
          7. Unter Objects in ioBroker werden dann unter ham.0 die Ventilatorsettings und Steuermöglichkeiten angezeigt

          Wenn der Ventilator in der Xiaomi App auftaucht, sollte er dann auch in ioBroker verfügbar sein.

          Weitere Hinweise:

          • Ich hatte den Ventilator testweise im Fritzbox-Gästenetz, dann funktioniert es aber leider nicht, er muss scheinbar lokal im Netz gut erreichbar sein
          • Für mein Ventilatormodell sind alle Einstellungen in ioBroker vornehmbar, bis auf die exakte Fan Speed als Prozentangabe. Es gibt aber 4 Stufen, die gewählt werden können
          • Der Ventilator erscheint auch ohne weiteres in der Apple Home App und kann dort auch gesteuert werden, dort kann sogar die Fan Speed in Prozent geändert werden. Hierzu muss der ham Adapter mit Apple Home verbunden werden, das müsstet Ihr nochmals googlen (bei mir liefs wegen des Sonos bereits), ist aber recht einfach.
          • Wer der China-Cloud nicht vertraut, kann nach der Installtion des Ventilators in der Fritzbox den Internetzugang des Fans kappen. Er erscheint dann in der Xiaomi Home App als "offline"

          @Pittini nochmals vielen herzlichen Dank für das Trouble-Shooting mit Deinem Skript, leider musste ich doch den anderen Weg gehen, da die Fehlermeldung nicht wegzubekommen war. Insbesondere die Tatsache, dass der Ventilator über ham ohne China Cloud lauffähig ist, ist m.E. ein Vorteil.

          Damrak2022D Offline
          Damrak2022D Offline
          Damrak2022
          schrieb am zuletzt editiert von
          #351

          @fd sagte in [Vorlage] Xiaomi Airpurifier 3H u.a. inkl. Token auslesen.:

          https://github.com/merdok/homebridge-xiaomi-fan

          Super, obwohl ich bei iobroker noch ganz neu dabei bin, habe ich es mit Deiner Anleitung auf Anhieb geschafft, meinen Xiaomi Ventilator einzubinden.
          Nur eine Frage habe ich noch: Welches Widget, oder Icon kann ich nehmen um den Ventilator in meiner Navigation darzustellen und zu steuern? Was würdest Du mir empfehlen?

          Mit besten Grüßen
          Andy

          1 Antwort Letzte Antwort
          0
          • PeoplesP Offline
            PeoplesP Offline
            Peoples
            schrieb am zuletzt editiert von
            #352

            Mahlzeit,

            ich habe eben das Script ausprobiert läuft wunderbar lediglich die Motorumdrehungen werden nicht beschrieben.
            ich habe einen Xiaomi Air Purifier 3H - zhimi.airpurifier.mb3 und laut Script werden die Datenpunkte angelegt, nur ob die überhaupt übertragen werden ist die Frage.

            Zumindest in meiner Handy-App sehe ich da auch keinen Wert. Sollte es so sein dass hier keine Daten übertragen werden könnte man die Datenpunkte bei der Erzeugung weg lassen (würde da ein issu aufmachen) oder hat jemand den gleichen Luftreiniger und bekommt da Werte?

            Ich beantworte keine Fragen zu Themen via PN

            W 1 Antwort Letzte Antwort
            0
            • PeoplesP Peoples

              Mahlzeit,

              ich habe eben das Script ausprobiert läuft wunderbar lediglich die Motorumdrehungen werden nicht beschrieben.
              ich habe einen Xiaomi Air Purifier 3H - zhimi.airpurifier.mb3 und laut Script werden die Datenpunkte angelegt, nur ob die überhaupt übertragen werden ist die Frage.

              Zumindest in meiner Handy-App sehe ich da auch keinen Wert. Sollte es so sein dass hier keine Daten übertragen werden könnte man die Datenpunkte bei der Erzeugung weg lassen (würde da ein issu aufmachen) oder hat jemand den gleichen Luftreiniger und bekommt da Werte?

              W Online
              W Online
              Wildbill
              schrieb am zuletzt editiert von
              #353

              @peoples
              Mit der device-Datei im Anhang, die ich ursprünglich mal modifiziert habe, funktioniert es bei mir ohne Probleme. Ich meine aber, die hätte @Pittini auch auf seiner Github-Site so mit abgelegt.

              Gruss, Jürgen

              zhimi.airpurifier.mb3.js

              haselchenH 1 Antwort Letzte Antwort
              4
              • W Wildbill

                @peoples
                Mit der device-Datei im Anhang, die ich ursprünglich mal modifiziert habe, funktioniert es bei mir ohne Probleme. Ich meine aber, die hätte @Pittini auch auf seiner Github-Site so mit abgelegt.

                Gruss, Jürgen

                zhimi.airpurifier.mb3.js

                haselchenH Offline
                haselchenH Offline
                haselchen
                Most Active
                schrieb am zuletzt editiert von
                #354

                @wildbill

                Danke für die .js , damit geht es jetzt bei mir auch.
                Zwar nur Spielerei , aber man will es ja perfekt haben :)

                Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                1 Antwort Letzte Antwort
                0
                • L Offline
                  L Offline
                  loverz
                  schrieb am zuletzt editiert von
                  #355

                  Hallo,

                  ich weiß nicht ob ich hier richtig bin, aber kann ich hiermit meinen Luftbefeuchter OHNE Gateway über iobroker zum laufen bringen?
                  https://forum.iobroker.net/topic/17466/xiaomi-smartmi-air-humidifier2/121

                  1 Antwort Letzte Antwort
                  0
                  • L Offline
                    L Offline
                    LassMichIchMachDasSelberKaputt
                    schrieb am zuletzt editiert von LassMichIchMachDasSelberKaputt
                    #356

                    Hallo @Pittini , hallo Leute!

                    Ich habe erfolglos versucht das Skript zum laufen bekommen und hab mich hier mit der Hoffnung auf Hilfe angemeldet. Die Verbindung scheint zu funktionieren, jedoch enthalten die neuen Datenpunkte keine Daten (null).

                    Unbenannt.JPG

                    Außerdem bekomme ich die Fehlermeldung dass mein Fan 2 nicht unterstütz wird:


                    10.4.2022, 14:46:40.196 [info ]: javascript.0 (8702) Stop script script.js.Xiaomi.MiHomeScript
                    10.4.2022, 14:46:40.223 [info ]: javascript.0 (8702) Start javascript script.js.Xiaomi.MiHomeScript
                    10.4.2022, 14:46:40.240 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                    10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                    10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: You are already logged in, login canceled
                    10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Retrieving your in de registered MiHome Devices
                    10.4.2022, 14:46:40.387 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Found 5 MiHome Devices, those are:
                    10.4.2022, 14:46:40.387 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Videokamera
                    10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Mi Air Purifier 3/3H
                    10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Eve
                    10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Ventilator Schlafzimmer
                    10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Mi Electric Scooter Pro 2 series
                    10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now searching for supported Devices...
                    10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Device Mi Air Purifier 3/3H is supported, creating DataPoints if necessary
                    10.4.2022, 14:46:40.390 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Device Ventilator Schlafzimmer is supported, creating DataPoints if necessary
                    10.4.2022, 14:46:40.410 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now creating device for zhimi.airpurifier.mb3 /GEKÜRZT
                    10.4.2022, 14:46:40.411 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Created device {"_events": ......... GEKÜRZT
                    10.4.2022, 14:46:40.835 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Init Device# 0 - device="zhimi.airpurifier.mb3"
                    10.4.2022, 14:46:40.836 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now creating device for dmaker.fan.p18 /GEKÜRZT
                    10.4.2022, 14:46:40.838 [error]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Error: Model dmaker.fan.p18 is not supported
                    10.4.2022, 14:46:40.838 [error]: javascript.0 (8702) at CreateDevices (script.js.Xiaomi.MiHomeScript:910:36)
                    10.4.2022, 14:46:40.839 [error]: javascript.0 (8702) at main (script.js.Xiaomi.MiHomeScript:842:5)

                    Füge ich dann das Gerätescript per WGET ein bekomme ich stattdessen folgende Fehler:


                    14:48:39.638 info javascript.0 (10026) Start javascript script.js.Xiaomi.MiHomeScript
                    14:48:39.699 error javascript.0 (10026) script.js.Xiaomi.MiHomeScript: /opt/iobroker/node_modules/iobroker.javascript/node_modules/node-mihome/lib/devices/dmaker.fan.p18.js:7
                    14:48:39.704 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:3:16
                    14:48:39.704 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:1188:3
                    14:48:39.704 info javascript.0 (10026) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                    14:48:39.707 info javascript.0 (10026) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                    14:48:39.708 error javascript.0 (10026) script.js.Xiaomi.MiHomeScript: TypeError: Cannot read property 'miioProtocol' of undefined
                    14:48:39.708 error javascript.0 (10026) at Init (script.js.Xiaomi.MiHomeScript:865:12)
                    14:48:39.708 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:37:1
                    14:48:39.708 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:1188:3

                    haselchenH 1 Antwort Letzte Antwort
                    0
                    • L LassMichIchMachDasSelberKaputt

                      Hallo @Pittini , hallo Leute!

                      Ich habe erfolglos versucht das Skript zum laufen bekommen und hab mich hier mit der Hoffnung auf Hilfe angemeldet. Die Verbindung scheint zu funktionieren, jedoch enthalten die neuen Datenpunkte keine Daten (null).

                      Unbenannt.JPG

                      Außerdem bekomme ich die Fehlermeldung dass mein Fan 2 nicht unterstütz wird:


                      10.4.2022, 14:46:40.196 [info ]: javascript.0 (8702) Stop script script.js.Xiaomi.MiHomeScript
                      10.4.2022, 14:46:40.223 [info ]: javascript.0 (8702) Start javascript script.js.Xiaomi.MiHomeScript
                      10.4.2022, 14:46:40.240 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                      10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                      10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: You are already logged in, login canceled
                      10.4.2022, 14:46:40.242 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Retrieving your in de registered MiHome Devices
                      10.4.2022, 14:46:40.387 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Found 5 MiHome Devices, those are:
                      10.4.2022, 14:46:40.387 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Videokamera
                      10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Mi Air Purifier 3/3H
                      10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Eve
                      10.4.2022, 14:46:40.388 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Ventilator Schlafzimmer
                      10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Mi Electric Scooter Pro 2 series
                      10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now searching for supported Devices...
                      10.4.2022, 14:46:40.389 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Device Mi Air Purifier 3/3H is supported, creating DataPoints if necessary
                      10.4.2022, 14:46:40.390 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Device Ventilator Schlafzimmer is supported, creating DataPoints if necessary
                      10.4.2022, 14:46:40.410 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now creating device for zhimi.airpurifier.mb3 /GEKÜRZT
                      10.4.2022, 14:46:40.411 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Created device {"_events": ......... GEKÜRZT
                      10.4.2022, 14:46:40.835 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Init Device# 0 - device="zhimi.airpurifier.mb3"
                      10.4.2022, 14:46:40.836 [info ]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Now creating device for dmaker.fan.p18 /GEKÜRZT
                      10.4.2022, 14:46:40.838 [error]: javascript.0 (8702) script.js.Xiaomi.MiHomeScript: Error: Model dmaker.fan.p18 is not supported
                      10.4.2022, 14:46:40.838 [error]: javascript.0 (8702) at CreateDevices (script.js.Xiaomi.MiHomeScript:910:36)
                      10.4.2022, 14:46:40.839 [error]: javascript.0 (8702) at main (script.js.Xiaomi.MiHomeScript:842:5)

                      Füge ich dann das Gerätescript per WGET ein bekomme ich stattdessen folgende Fehler:


                      14:48:39.638 info javascript.0 (10026) Start javascript script.js.Xiaomi.MiHomeScript
                      14:48:39.699 error javascript.0 (10026) script.js.Xiaomi.MiHomeScript: /opt/iobroker/node_modules/iobroker.javascript/node_modules/node-mihome/lib/devices/dmaker.fan.p18.js:7
                      14:48:39.704 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:3:16
                      14:48:39.704 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:1188:3
                      14:48:39.704 info javascript.0 (10026) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                      14:48:39.707 info javascript.0 (10026) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                      14:48:39.708 error javascript.0 (10026) script.js.Xiaomi.MiHomeScript: TypeError: Cannot read property 'miioProtocol' of undefined
                      14:48:39.708 error javascript.0 (10026) at Init (script.js.Xiaomi.MiHomeScript:865:12)
                      14:48:39.708 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:37:1
                      14:48:39.708 error javascript.0 (10026) at script.js.Xiaomi.MiHomeScript:1188:3

                      haselchenH Offline
                      haselchenH Offline
                      haselchen
                      Most Active
                      schrieb am zuletzt editiert von
                      #357

                      @lassmichichmachdasselberkaputt

                      Moinsen,

                      Hast Du die Definitionsdatei von Github geladen?

                      Der Pfad ist falsch.

                      So lautet er (jedenfalls bei mir)

                      /opt/iobroker/node_modules/node-mihome/lib/devices

                      Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                      L 1 Antwort Letzte Antwort
                      0
                      • haselchenH haselchen

                        @lassmichichmachdasselberkaputt

                        Moinsen,

                        Hast Du die Definitionsdatei von Github geladen?

                        Der Pfad ist falsch.

                        So lautet er (jedenfalls bei mir)

                        /opt/iobroker/node_modules/node-mihome/lib/devices

                        L Offline
                        L Offline
                        LassMichIchMachDasSelberKaputt
                        schrieb am zuletzt editiert von
                        #358

                        @haselchen

                        Danke für die schnelle Antwort!

                        Ja, habe ich. Habe sie per wget direkt in das Verzeichnis kopiert. Das wird auch in der Anleitung erwähnt. Habe jetzt Probehalber das NPM Modul aus den JS Adapter Einstellungen herausegenommen und in dem Verzeichnis deinstalliert, anschließend in dem Verzeichnis das du nanntest wieder installiert und Gerätedatei hinzukopiert mit:

                        wget https://github.com/Pittini/iobroker-nodemihome/blob/main/DefinitionfilesForNode-Mihome/dmaker.fan.p18.js -P /opt/iobroker/node_modules/node-mihome/lib/devices/

                        Das neu starten des Scripts führt nun zu einem anderen Fehler :


                        10.4.2022, 17:31:34.264 [info ]: javascript.0 (47793) Stop script script.js.Xiaomi.MiHomeScript
                        10.4.2022, 17:31:34.299 [info ]: javascript.0 (47793) Start javascript script.js.Xiaomi.MiHomeScript
                        10.4.2022, 17:31:34.307 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                        10.4.2022, 17:31:34.308 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                        10.4.2022, 17:31:34.308 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: You are already logged in, login canceled
                        10.4.2022, 17:31:34.309 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Retrieving your in de registered MiHome Devices
                        10.4.2022, 17:31:34.435 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Found 5 MiHome Devices, those are:
                        10.4.2022, 17:31:34.435 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Videokamera
                        10.4.2022, 17:31:34.436 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Mi Air Purifier 3/3H
                        10.4.2022, 17:31:34.436 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Eve
                        10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Ventilator Schlafzimmer
                        10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Mi Electric Scooter Pro 2 series
                        10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Now searching for supported Devices...
                        10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Device Mi Air Purifier 3/3H is supported, creating DataPoints if necessary
                        10.4.2022, 17:31:34.438 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Device Ventilator Schlafzimmer is supported, creating DataPoints if necessary
                        10.4.2022, 17:31:34.750 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Now creating device for zhimi.airpurifier.mb3 /GEKÜRZT
                        10.4.2022, 17:31:34.751 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Created device {"_events": GEKÜRZT
                        10.4.2022, 17:31:35.823 [error]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: TypeError: Cannot read property 'forEach' of undefined
                        10.4.2022, 17:31:35.826 [error]: javascript.0 (47793) at CreateDevices (script.js.Xiaomi.MiHomeScript:924:17)
                        10.4.2022, 17:31:35.826 [error]: javascript.0 (47793) at main (script.js.Xiaomi.MiHomeScript:842:5)

                        Habe Node-mihome dort nun wieder deinstalliert

                        haselchenH 1 Antwort Letzte Antwort
                        0
                        • L LassMichIchMachDasSelberKaputt

                          @haselchen

                          Danke für die schnelle Antwort!

                          Ja, habe ich. Habe sie per wget direkt in das Verzeichnis kopiert. Das wird auch in der Anleitung erwähnt. Habe jetzt Probehalber das NPM Modul aus den JS Adapter Einstellungen herausegenommen und in dem Verzeichnis deinstalliert, anschließend in dem Verzeichnis das du nanntest wieder installiert und Gerätedatei hinzukopiert mit:

                          wget https://github.com/Pittini/iobroker-nodemihome/blob/main/DefinitionfilesForNode-Mihome/dmaker.fan.p18.js -P /opt/iobroker/node_modules/node-mihome/lib/devices/

                          Das neu starten des Scripts führt nun zu einem anderen Fehler :


                          10.4.2022, 17:31:34.264 [info ]: javascript.0 (47793) Stop script script.js.Xiaomi.MiHomeScript
                          10.4.2022, 17:31:34.299 [info ]: javascript.0 (47793) Start javascript script.js.Xiaomi.MiHomeScript
                          10.4.2022, 17:31:34.307 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                          10.4.2022, 17:31:34.308 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                          10.4.2022, 17:31:34.308 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: You are already logged in, login canceled
                          10.4.2022, 17:31:34.309 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Retrieving your in de registered MiHome Devices
                          10.4.2022, 17:31:34.435 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Found 5 MiHome Devices, those are:
                          10.4.2022, 17:31:34.435 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Videokamera
                          10.4.2022, 17:31:34.436 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Mi Air Purifier 3/3H
                          10.4.2022, 17:31:34.436 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Eve
                          10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Ventilator Schlafzimmer
                          10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Mi Electric Scooter Pro 2 series
                          10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Now searching for supported Devices...
                          10.4.2022, 17:31:34.437 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Device Mi Air Purifier 3/3H is supported, creating DataPoints if necessary
                          10.4.2022, 17:31:34.438 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Device Ventilator Schlafzimmer is supported, creating DataPoints if necessary
                          10.4.2022, 17:31:34.750 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Now creating device for zhimi.airpurifier.mb3 /GEKÜRZT
                          10.4.2022, 17:31:34.751 [info ]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: Created device {"_events": GEKÜRZT
                          10.4.2022, 17:31:35.823 [error]: javascript.0 (47793) script.js.Xiaomi.MiHomeScript: TypeError: Cannot read property 'forEach' of undefined
                          10.4.2022, 17:31:35.826 [error]: javascript.0 (47793) at CreateDevices (script.js.Xiaomi.MiHomeScript:924:17)
                          10.4.2022, 17:31:35.826 [error]: javascript.0 (47793) at main (script.js.Xiaomi.MiHomeScript:842:5)

                          Habe Node-mihome dort nun wieder deinstalliert

                          haselchenH Offline
                          haselchenH Offline
                          haselchen
                          Most Active
                          schrieb am zuletzt editiert von haselchen
                          #359

                          @lassmichichmachdasselberkaputt

                          Bei dir muss es woanders haken.
                          Ich habe das Modul in dem Adapter eingetragen.
                          ce95f93e-008c-460e-aee5-6695a5c95bca-grafik.png

                          Bei steht immer das du schon eingeloggt bist.
                          Hast du dich in der App mal ausgeloggt und dann das Skript neu gestartet?

                          Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                          L 1 Antwort Letzte Antwort
                          0
                          • haselchenH haselchen

                            @lassmichichmachdasselberkaputt

                            Bei dir muss es woanders haken.
                            Ich habe das Modul in dem Adapter eingetragen.
                            ce95f93e-008c-460e-aee5-6695a5c95bca-grafik.png

                            Bei steht immer das du schon eingeloggt bist.
                            Hast du dich in der App mal ausgeloggt und dann das Skript neu gestartet?

                            L Offline
                            L Offline
                            LassMichIchMachDasSelberKaputt
                            schrieb am zuletzt editiert von
                            #360

                            @haselchen

                            Habe ich grad noch Mal versucht. Der Hinweis dass ich schon eingeloggt bin kommt sobald ich das script das zweite Mal neu starte. Anscheinend hält node-mihome die Verbindung. So hatte ich es als erstes probiert, stand auch so in der Anleitung. Hab sie schon fünf Mal gelesen und kann immer noch nicht ausschließen dass ich nicht vielleicht etwas überlesen habe :sweat_smile:

                            Ich kriege auch für meinen 3H Luftfilter zwar Datenpunkte aber keine Werte (null)...

                            haselchenH 1 Antwort Letzte Antwort
                            0
                            • L LassMichIchMachDasSelberKaputt

                              @haselchen

                              Habe ich grad noch Mal versucht. Der Hinweis dass ich schon eingeloggt bin kommt sobald ich das script das zweite Mal neu starte. Anscheinend hält node-mihome die Verbindung. So hatte ich es als erstes probiert, stand auch so in der Anleitung. Hab sie schon fünf Mal gelesen und kann immer noch nicht ausschließen dass ich nicht vielleicht etwas überlesen habe :sweat_smile:

                              Ich kriege auch für meinen 3H Luftfilter zwar Datenpunkte aber keine Werte (null)...

                              haselchenH Offline
                              haselchenH Offline
                              haselchen
                              Most Active
                              schrieb am zuletzt editiert von
                              #361

                              @lassmichichmachdasselberkaputt

                              Da muss ich echt mal schwer nachdenken.
                              Ich meine, wenn das Skript aufgrund von Fehlern nicht weiterkommt, dass
                              es nicht läuft.
                              Kannst du die Datei manuell durch eine GUI in den device Ordner kopieren?

                              Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                              L 1 Antwort Letzte Antwort
                              0
                              • haselchenH haselchen

                                @lassmichichmachdasselberkaputt

                                Da muss ich echt mal schwer nachdenken.
                                Ich meine, wenn das Skript aufgrund von Fehlern nicht weiterkommt, dass
                                es nicht läuft.
                                Kannst du die Datei manuell durch eine GUI in den device Ordner kopieren?

                                L Offline
                                L Offline
                                LassMichIchMachDasSelberKaputt
                                schrieb am zuletzt editiert von LassMichIchMachDasSelberKaputt
                                #362

                                @haselchen

                                Ich hab grad keine GUI drauf. Aber per ls sehe ich ja dass die Datei dann ankommt. Und der Fan 2 sollte schon durch das script unterstützt sein oder?

                                5b1cc0b8-af6d-49c0-b9d4-7daec83d1e3c-image.png

                                Ich weiß aber dass das Gerätescript nicht in dem Ordner liegt bevor ich es hinein kopiere.

                                Hier noch ein Paar Systemdetails:

                                b98e2055-635b-413b-b349-8b11cefbae6f-image.png
                                d3937017-ac34-4991-b903-4e0e5665fe7e-image.png

                                haselchenH 1 Antwort Letzte Antwort
                                0
                                • L LassMichIchMachDasSelberKaputt

                                  @haselchen

                                  Ich hab grad keine GUI drauf. Aber per ls sehe ich ja dass die Datei dann ankommt. Und der Fan 2 sollte schon durch das script unterstützt sein oder?

                                  5b1cc0b8-af6d-49c0-b9d4-7daec83d1e3c-image.png

                                  Ich weiß aber dass das Gerätescript nicht in dem Ordner liegt bevor ich es hinein kopiere.

                                  Hier noch ein Paar Systemdetails:

                                  b98e2055-635b-413b-b349-8b11cefbae6f-image.png
                                  d3937017-ac34-4991-b903-4e0e5665fe7e-image.png

                                  haselchenH Offline
                                  haselchenH Offline
                                  haselchen
                                  Most Active
                                  schrieb am zuletzt editiert von
                                  #363

                                  @lassmichichmachdasselberkaputt

                                  Unterstützt ja, aber die Datei musste selber kopieren.
                                  Haste das Modul im Adapter eingetragen ?

                                  Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                                  L 1 Antwort Letzte Antwort
                                  0
                                  • haselchenH haselchen

                                    @lassmichichmachdasselberkaputt

                                    Unterstützt ja, aber die Datei musste selber kopieren.
                                    Haste das Modul im Adapter eingetragen ?

                                    L Offline
                                    L Offline
                                    LassMichIchMachDasSelberKaputt
                                    schrieb am zuletzt editiert von
                                    #364

                                    @haselchen

                                    Ja, das war der Zustand von meinem ersten Post. Sonst kommt das Script nicht so weit und meldet das fehlende Modul.

                                    haselchenH 1 Antwort Letzte Antwort
                                    0
                                    • L LassMichIchMachDasSelberKaputt

                                      @haselchen

                                      Ja, das war der Zustand von meinem ersten Post. Sonst kommt das Script nicht so weit und meldet das fehlende Modul.

                                      haselchenH Offline
                                      haselchenH Offline
                                      haselchen
                                      Most Active
                                      schrieb am zuletzt editiert von haselchen
                                      #365

                                      @lassmichichmachdasselberkaputt

                                      Hast du auch mal nen früheres Skript probiert?
                                      Hab dir mal meine 23er Version hier reingepackt.
                                      Haste axios auch als Modul in dem Adapter eingetragen?


                                      const SkriptVersion = "0.2.23"; //vom 30.06.2021 / Link zu Git: https://github.com/Pittini/iobroker-nodemihome / Forum: https://forum.iobroker.net/topic/39388/vorlage-xiaomi-airpurifier-3h-u-a-inkl-token-auslesen

                                      const mihome = require('node-mihome');

                                      // Logindaten für Xiaomi Cloud:
                                      const username = 'XXXXXXX';
                                      const password = 'XXXXXXX';
                                      const options = { country: 'de' }; // 'ru', 'us', 'tw', 'sg', 'cn', 'de' (Default: 'cn');
                                      const refresh = 13000; // Alle 13sek neue Daten

                                      const praefix0 = "javascript.0.MiHomeAll"; //Root für Skriptdatenpunkte

                                      const logging = false; //Logging aktivieren/deaktivieren

                                      //Ab hier nix mehr ändern!
                                      /*

                                      1. Xiaomi Cloudlogin
                                      2. Alle dort gelisteten Geräte und deren Basicdaten abrufen
                                      3. Für alle abgerufenen Geräte Basic Channel/Datenpunkte anlegen
                                      4. Prüfen welche supporteten Geräte in der Auflistung vorhanden sind und die entsprechenden spezifischen Datenpunkte erstellen
                                      5. Basic Channels mit Daten füllen / einlesen
                                      6. devicearray erstellen via node-mihome und die Gerätespezifischen Werte einlesen
                                        */
                                        // ######### TESTBEREICH ################
                                        //const axios = require('axios');

                                      //let miotDefinition= getMiotData('https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc1:1');

                                      async function getMiotData(url) {
                                      if (logging) log("Reaching MiotUrlConstructor");
                                      try {
                                      const response = await axios.get(url, { timeout: 10000 });
                                      log("resp:" + JSON.stringify(response.data));
                                      for (let z in Object.keys(response.data.services)) {
                                      log("Keys=" + Object.keys(response.data.services[z]))
                                      log(JSON.stringify(response.data.services[z]))
                                      }

                                          return response.data;
                                      } catch (error) {
                                          console.error(error);
                                      }
                                      

                                      }

                                      function MiotUrlConstructor(miotdevice) {
                                      if (logging) log("Reaching MiotUrlConstructor");
                                      let BaseUrl = "https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:" + miotdevice;
                                      return BaseUrl;
                                      }

                                      // ################ ENDE TESTBEREICH ####################

                                      const DeviceData = [];
                                      let AllDevicesRaw = [];

                                      let device = [];

                                      const States = [];
                                      let DpCount = 0;
                                      log("Starting AllMyMi V." + SkriptVersion);

                                      Init();

                                      const DefineDevice = [];

                                      // ***************************** Device Definitions *************************

                                      // ***************************** Airpurifiers ********************************

                                      DefineDevice[0] = { // Tested and working
                                      info: {},
                                      model: "zhimi.airpurifier.mb3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mb3:2
                                      description: "Purifier 3H",
                                      setter: {
                                      "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "motor-speed.favorite-fan-level": async function (obj, val) { await device[obj].setFavLevel(val) },
                                      "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                      { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                      { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                      { name: "alarm.alarm", type: "boolean", read: true, write: true },
                                      { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                      { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "motor-speed.motor-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                      { name: "motor-speed.motor-set-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                      { name: "motor-speed.favorite-fan-level", type: "number", read: true, write: true, min: 0, max: 14 },
                                      { name: "use-time.use-time", type: "number", read: true, write: false },
                                      { name: "environment.relative-humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[20] = { // tested and ok
                                      info: {},
                                      model: "zhimi.airpurifier.mc1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc1:1
                                      description: "Purifier 2S",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                      "led": async function (obj, val) { await device[obj].setLed(val) },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                      { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 1, max: 10 },
                                      { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                      { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[22] = {
                                      info: {},
                                      model: "zhimi.airpurifier.m1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-m1:1
                                      description: "Purifier 2",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                      "led": async function (obj, val) { await device[obj].setLed(val) },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                      { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 1, max: 10 },
                                      { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                      { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[8] = { // Tested and working
                                      info: {},
                                      model: "zhimi.airpurifier.mc2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc2:1
                                      description: "Purifier 2H",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                      "led": async function (obj, val) { await device[obj].setLed(val) },
                                      "led_b": async function (obj, val) { await device[obj].setLedB(val) },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                      { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 0, max: 16 },
                                      { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                      { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "average_aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "led_b", type: "number", role: "state", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                      { name: "motor1_speed", type: "number", role: "value", read: true, write: false, unit: "rpm" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[13] = { // Tested and working
                                      info: {},
                                      model: "zhimi.airpurifier.vb2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-vb2:1
                                      description: "Mi Air Purifier Pro H",
                                      setter: {
                                      "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "motor-speed.favorite-level": async function (obj, val) { await device[obj].setFavLevel(val) },
                                      "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "alarm.volume": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                      { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                      { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                      { name: "alarm.volume", type: "number", read: true, write: true, min: 0, max: 100 },
                                      { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                      { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "motor-speed.motor1-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                      { name: "motor-speed.motor1-set-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                      { name: "motor-speed.favorite-level", type: "number", read: true, write: true, min: 0, max: 14 },
                                      { name: "use-time.use-time", type: "number", read: true, write: false },
                                      { name: "environment.relative-humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[14] = { // Tested and working
                                      info: {},
                                      model: "zhimi.airpurifier.v7",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-v7:1
                                      description: "Mi Air Purifier",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "led": async function (obj, val) { await device[obj].setDisplay(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                      { name: "favorite_level", type: "number", read: true, write: false, min: 0, max: 16 },
                                      { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false },
                                      { name: "humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "aqi", type: "number", read: true, write: false, unit: "μg/m³" },
                                      { name: "average_aqi", type: "number", read: true, write: false, unit: "μg/m³" },
                                      { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "bright", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "volume", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "filter1_life", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "f1_hour", type: "number", read: true, write: false },
                                      { name: "f1_hour_used", type: "number", read: true, write: false },
                                      { name: "motor1_speed", type: "number", read: true, write: false, unit: "rpm" },
                                      { name: "motor2_speed", type: "number", read: true, write: false, unit: "rpm" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[15] = { // Tested and working
                                      info: {},
                                      model: "zhimi.airpurifier.mb4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mb4:2
                                      description: "Purifier 3C",
                                      setter: {
                                      "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "custom-service.favorite-speed": async function (obj, val) { await device[obj].setFavSpeed(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "screen.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "air-purifier.fault", type: "string", read: true, write: false },
                                      { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                      { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "screen.brightness", type: "number", read: true, write: true, min: 0, max: 8 },
                                      { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "custom-service.moto-speed-rpm", type: "number", read: true, write: false, min: 0, max: 65535, unit: "rpm" },
                                      { name: "custom-service.favorite-speed", type: "number", read: true, write: true, min: 300, max: 2300, unit: "rpm" },
                                      { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      DefineDevice[18] = { // Untested
                                      info: {},
                                      model: "zhimi.airpurifier.ma4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-ma4:1
                                      description: "Air Purifier",
                                      setter: {
                                      "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                      { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "none" } },
                                      { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                      { name: "alarm.alarm", type: "boolean", read: true, write: true, min: false, max: true },
                                      { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                      { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "environment.relative-humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                      { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };

                                      // ***************************** Fans *********************************
                                      // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:zhimi-sa1:1

                                      DefineDevice[1] = { // untested
                                      info: {},
                                      model: "leshow.fan.ss4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:leshow-ss4:1
                                      description: "Leshow Fan",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "blow": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "yaw": async function (obj, val) { await device[obj].setFanSwing(val ? 'on' : 'off') },
                                      "mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                      "sound": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                      "timer": async function (obj, val) { await device[obj].setTimer(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "blow", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "yaw", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "mode", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "sound", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "timer", type: "number", read: true, write: true, min: 0, max: 540, unit: "m" }]
                                      };

                                      DefineDevice[9] = { // Tested and working
                                      info: {},
                                      model: "zhimi.fan.za4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:zhimi-za4:1
                                      description: "Mi Fan 2S",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "angle": async function (obj, val) { await device[obj].setSwingAngle(val) },
                                      "angle_enable": async function (obj, val) { await device[obj].setSwing(val) },
                                      "natural_level": async function (obj, val) { await device[obj].setSleepMode((val == 1) ? true : false) },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val ? 'on' : 'off') },
                                      "led_b": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                      "speed_level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "poweroff_time": async function (obj, val) { await device[obj].setTimer(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "angle", type: "number", read: true, write: true, min: 1, max: 120 },
                                      { name: "angle_enable", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "natural_level", type: "number", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "led_b", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "speed_level", type: "number", read: true, write: true, min: 1, max: 100, unit: "%" },
                                      { name: "poweroff_time", type: "number", read: true, write: true, min: 0, max: 540, unit: "m" }]
                                      };

                                      DefineDevice[17] = { // Tested and working
                                      info: {},
                                      model: "dmaker.fan.p15",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p15:1
                                      description: "Mi Smart Standing Fan Pro",
                                      setter: {
                                      "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                      "fan.horizontal-angle": async function (obj, val) { await device[obj].setHorizontalAngle(val) },
                                      "indicator-light.on": async function (obj, val) { await device[obj].setIndicatorLight(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                      "motor-controller.motor-control": async function (obj, val) { await device[obj].setMotorController(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                      "off-delay-time.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                      },
                                      common:
                                      [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                      { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 4, states: { 1: "Slow", 2: "Middle", 3: "High", 4: "Turbo" } },
                                      { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.horizontal-angle", type: "number", role: "switch", read: true, write: true, min: 30, max: 140, unit: "°", states: { 30: "30°", 60: "60°", 90: "90°", 120: "120°", 140: "140°" } },
                                      { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                      { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "motor-controller.motor-control", type: "number", role: "switch", read: false, write: true, min: 0, max: 2, states: { 0: "None", 1: "Left", 2: "Right" } },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "off-delay-time.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                      ]
                                      };

                                      DefineDevice[19] = { // untested
                                      info: {},
                                      model: "dmaker.fan.1c",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-1c:1
                                      description: "Mi Smart Standing Fan 1C",
                                      setter: {
                                      "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                      "fan.brightness": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "fan.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                      "fan.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                      },
                                      common:
                                      [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Sleep" } },
                                      { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 3, states: { 1: "Slow", 2: "Middle", 3: "High" } },
                                      { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                      { name: "fan.brightness", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "fan.alarm", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "fan.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                      ]
                                      };

                                      DefineDevice[23] = { // Tested and working
                                      info: {},
                                      model: "dmaker.fan.p18",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p18:1
                                      description: "Mi Smart Standing Fan 2",
                                      setter: {
                                      "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                      "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                      "fan.horizontal-angle": async function (obj, val) { await device[obj].setHorizontalAngle(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                      "motor-controller.motor-control": async function (obj, val) { await device[obj].setMotorController(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                      "off-delay-time.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                      },
                                      common:
                                      [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                      { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 4, states: { 1: "Slow", 2: "Middle", 3: "High", 4: "Turbo" } },
                                      { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "fan.horizontal-angle", type: "number", role: "switch", read: true, write: true, min: 30, max: 140, unit: "°", states: { 30: "30°", 60: "60°", 90: "90°", 120: "120°", 140: "140°" } },
                                      { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                      { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "motor-controller.motor-control", type: "number", role: "switch", read: false, write: true, min: 0, max: 2, states: { 0: "None", 1: "Left", 2: "Right" } },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "off-delay-time.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                      ]
                                      };

                                      // ***************************** Lights *********************************

                                      DefineDevice[2] = { // Tested and ok except setting color
                                      info: {},
                                      model: "yeelink.light.strip2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-color2:1
                                      description: "Yeelight Lightstrip Plus",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "hue": async function (obj, val) { await device[obj].setColorHSV(val) },
                                      "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                      "ct": async function (obj, val) { await device[obj].setCt(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "hue", type: "number", read: true, write: true, min: 0, max: 16777215 },
                                      { name: "sat", type: "number", read: true, write: true, min: 0, max: 100 },
                                      { name: "color_mode", type: "number", read: true, write: true, min: 1, max: 2 },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };

                                      DefineDevice[5] = { // Tested and working
                                      info: {},
                                      model: "yeelink.light.ct2",// http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ct2:1
                                      description: "Yeelight LED Bulb (Tunable)",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "ct": async function (obj, val) { await device[obj].setColorTemperature(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };

                                      DefineDevice[6] = { // Tested and working except color setting
                                      info: {},
                                      model: "yeelink.light.color2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-color2:1
                                      description: "Yeelight LED Bulb (Color)",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "rgb": async function (obj, val) { await device[obj].setColorRgb(val) },
                                      "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                      "ct": async function (obj, val) { await device[obj].setCt(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "rgb", type: "number", read: true, write: true, min: 1, max: 16777215 },
                                      { name: "color_mode", type: "number", read: true, write: true, min: 1, max: 2 },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };
                                      DefineDevice[10] = { // untested
                                      info: {},
                                      model: "yeelink.light.ceiling3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling3:1
                                      description: "Yeelight LED Ceiling Light",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "nl_br": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "night_mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                      "ct": async function (obj, val) { await device[obj].setCt(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "nl_br", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "night_mode", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };
                                      DefineDevice[11] = { // untested
                                      info: {},
                                      model: "yeelink.light.ceiling1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling1:1
                                      description: "Yeelight Crystal Pedestal Light",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "nl_br": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "night_mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                      "ct": async function (obj, val) { await device[obj].setCt(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "nl_br", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "night_mode", type: "boolean", read: true, write: true },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };

                                      DefineDevice[21] = { // untested
                                      info: {},
                                      model: "yeelink.light.ceiling10",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling10:1
                                      description: "Yeelight Meteorite Pedestal Light",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "main_power": async function (obj, val) { await device[obj].setMainPower(val) },
                                      "bg_power": async function (obj, val) { await device[obj].setBgPower(val) },
                                      "bg_bright": async function (obj, val) { await device[obj].setBgBrightness(val) },
                                      "active_bright": async function (obj, val) { await device[obj].setActiveBrightness(val) },
                                      "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                      "ct": async function (obj, val) { await device[obj].setColorTemperature(val) },
                                      "bg_ct": async function (obj, val) { await device[obj].setBgColorTemperature(val) },
                                      "bg_rgb": async function (obj, val) { await device[obj].setBgColorRgb(val) },
                                      "bg_hue": async function (obj, val) { await device[obj].setBgColorHSV([val, 100]) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "main_power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bg_power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bg_bright", type: "number", role: 'level.dimmer', read: true, write: true, min: 1, max: 100, unit: "%" },
                                      { name: "active_bright", type: "number", role: 'level.dimmer', read: true, write: true, min: 1, max: 100, unit: "%" },
                                      { name: "color_mode", type: "boolean", role: 'switch.mode.color', read: true, write: true },
                                      { name: "moon_mode", type: "boolean", role: 'switch.mode.moon', read: true, write: true },
                                      { name: "bg_ct", type: "number", role: 'level.color.temperature', read: true, write: true, min: 2600, max: 6500, unit: "K" },
                                      { name: "ct", type: "number", role: 'level.color.temperature', read: true, write: true, min: 2600, max: 6500, unit: "K" },
                                      { name: "bg_rgb", type: "string", role: 'level.color.rgb', read: true, write: true },
                                      { name: "bg_hue", type: "number", role: 'level.color.hue', read: true, write: true, min: 0, max: 359 },
                                      { name: "bg_sat", type: "number", role: 'level.color.saturation', read: true, write: true, min: 1, max: 100 }]
                                      };

                                      DefineDevice[16] = { // untested
                                      info: {},
                                      model: "yeelink.light.lamp4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-lamp4:1
                                      description: "Mi LED Desk Lamp 1S",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "ct": async function (obj, val) { await device[obj].setColorTemperature(val) }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                      { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                      { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                      };

                                      // ***************************** Humidifier *********************************

                                      DefineDevice[3] = { // Tested and working
                                      info: {},
                                      model: "zhimi.humidifier.cb1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:zhimi-cb1:1
                                      description: "Smartmi Evaporative Humidifier",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                      "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "limit_hum": async function (obj, val) { await device[obj].setTargetHumidity(val) },
                                      "led": async function (obj, val) { await device[obj].setLedBrightness(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val) },
                                      "dry": async function (obj, val) { await device[obj].setMode(val ? 'dry' : 'humidify') }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "depth", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "limit_hum", type: "number", read: true, write: true, min: 0, max: 100, unit: "%", states: { 30: "30%", 40: "40%", 50: "50%", 60: "60%", 70: "70%", 80: "80%" } },
                                      { name: "led", type: "number", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "dry", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "medium": "medium", "high": "high" } }]
                                      };
                                      DefineDevice[4] = { // untested
                                      info: {},
                                      model: "deerma.humidifier.jsq",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:deerma-jsq:1
                                      description: "Mi Smart Antibacterial Humidifier",
                                      setter: {
                                      "humidifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                      "humidifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                      "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                      },
                                      common:
                                      [{ name: "humidifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "humidifier.fan-level", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "level1", 2: "level2", 3: "level3" } },
                                      { name: "humidifier.water-level", type: "number", read: true, write: false, min: 0, max: 127 },
                                      { name: "alarm.alarm", type: "boolean", read: true, write: true, min: false, max: true },
                                      { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "environment.relative-humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                      };
                                      DefineDevice[12] = { // untested
                                      info: {},
                                      model: "zhimi.humidifier.ca4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:zhimi-ca4:1
                                      description: "Smartmi Evaporative Humidifier",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                      "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                      "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                      "limit_hum": async function (obj, val) { await device[obj].setTargetHumidity(val) },
                                      "led": async function (obj, val) { await device[obj].setLedBrightness(val) },
                                      "child_lock": async function (obj, val) { await device[obj].setChildLock(val ? 'on' : 'off') },
                                      "dry": async function (obj, val) { await device[obj].setMode(val ? 'dry' : 'humidify') }
                                      },
                                      common:
                                      [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "depth", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "limit_hum", type: "number", read: true, write: true, min: 0, max: 100, unit: "%", states: { 30: "30%", 40: "40%", 50: "50%", 60: "60%", 70: "70%", 80: "80%" } },
                                      { name: "led", type: "number", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                      { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                      { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                      { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "dry", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                      { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "medium": "medium", "high": "high" } }]
                                      };

                                      // ***************************** Divers *********************************
                                      // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:coffee-machine:0000A049:scishare-s1102:1
                                      // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:camera:0000A01C:mijia-v1:1
                                      // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:plant-monitor:0000A030:hhcc-v1:1
                                      // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:heater:0000A01A:zhimi-mc2:1

                                      DefineDevice[7] = { // In arbeit - unvollständig
                                      info: {},
                                      model: "lumi.gateway.v3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:gateway:0000A019:lumi-v3:1
                                      description: "Xiaomi RGB Gateway",
                                      setter: {
                                      "power": async function (obj, val) { await device[obj].setPower(val) },
                                      "doorbell_push": async function (obj, val) { await device[obj].setBrightness(val) },
                                      "toggle_light": async function (obj, val) { await device[obj].setLightPower(val) },
                                      },
                                      common:
                                      [{ name: 'illumination', role: 'value.lux', write: false, read: true, type: 'number', unit: 'lux' },
                                      { name: 'rgb', role: 'level.color.rgb', write: true, read: true, type: 'string' },
                                      { name: 'nightlight_rgb', role: 'level.color.rgb', write: true, read: true, type: 'string' },
                                      { name: "mute", type: "boolean", read: true, write: true },
                                      { name: 'toggle_light', role: 'switch', write: true, read: true, type: 'boolean' },
                                      { name: 'light.dimmer', role: 'level.dimmer', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                      { name: 'gateway_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                      { name: 'doorbell_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                      { name: 'alarming_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                      { name: "doorbell_push", type: "boolean", read: true, write: true },
                                      { name: "arming", type: "boolean", read: true, write: true },
                                      { name: "arming_time", type: "number", read: true, write: true },
                                      { name: 'music_ID', role: 'state', write: true, read: false, type: 'number', desc: '10000 - stop, 10005 - custom ringtone' },
                                      { name: 'proto_version', role: 'info', write: false, read: true, type: 'string' },
                                      { name: 'join_permission', role: 'state', write: true, read: true, type: 'string' },
                                      { name: 'remove_device', role: 'state', write: true, read: true, type: 'string' }, // Removing a subdevice (device sid)
                                      { name: 'connected', role: 'indicator.reachable', write: false, read: true, type: 'boolean', desc: 'Will be set to false if no packets received in 20 seconds' }]
                                      };

                                      /*
                                      gateway: {type: 'gateway', fullName: 'Xiaomi RGB Gateway', ClassName: Gateway, states: {
                                      + illumination: {name: 'Illumination', role: 'value.lux', write: false, read: true, type: 'number', unit: 'lux'},
                                      + rgb: {name: 'RGB', role: 'level.color.rgb', write: true, read: true, type: 'string'},
                                      on: {name: 'Light', role: 'switch', write: true, read: true, type: 'boolean'},
                                      dimmer: {name: 'Light', role: 'level.dimmer', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100},
                                      volume: {name: 'Volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100},
                                      mid: {name: 'Music ID', role: 'state', write: true, read: false, type: 'number', desc: '10000 - stop, 10005 - custom ringtone'},
                                      proto_version: {name: 'Proto Version', role: 'info', write: false, read: true, type: 'string'},
                                      join_permission:{name: 'Add device', role: 'state', write: true, read: true, type: 'string'}, // Permission to add subdevices (yes / no)
                                      remove_device: {name: 'Remove device', role: 'state', write: true, read: true, type: 'string'}, // Removing a subdevice (device sid)
                                      connected: {name: 'Is gateway connected', role: 'indicator.reachable', write: false, read: true, type: 'boolean', desc: 'Will be set to false if no packets received in 20 seconds'}
                                      }
                                      */

                                      for (let x in DefineDevice) { //An alle Devicedefinitionen die generischen Datenpunkte anhängen
                                      DefineDevice[x].info = [
                                      { id: "localip", initial: "", forceCreation: false, common: { read: true, write: true, name: "Ip Adress", type: "string", role: "value", def: "" } },
                                      { id: "token", initial: "", forceCreation: false, common: { read: true, write: true, name: "Token", type: "string", role: "value", def: "" } },
                                      { id: "did", initial: "", forceCreation: false, common: { read: true, write: true, name: "Device Id", type: "string", role: "value", def: "" } },
                                      { id: "model", initial: "", forceCreation: false, common: { read: true, write: true, name: "Model", type: "string", role: "value", def: "" } },
                                      { id: "rssi", initial: 0, forceCreation: false, common: { read: true, write: false, name: "rssi", type: "number", role: "value.rssi", def: 0 } },
                                      { id: "name", initial: "", forceCreation: false, common: { read: true, write: true, name: "Name", type: "string", role: "value", def: "" } },
                                      { id: "isOnline", initial: false, forceCreation: false, common: { read: true, write: true, name: "Is online", type: "boolean", role: "value", def: false } }]

                                      }

                                      function PrepareDeviceDps(did, model) {
                                      if (logging) log("Reaching PrepareDeviceDps, did=" + did + " model=" + model);
                                      for (let x in DefineDevice) { //Alle definierten Model durchgehen
                                      if (DefineDevice[x].model == model) { //bei Model match
                                      for (let y in DefineDevice[x].common) { //Alle common propertys des models durchgehen und Var zusammensetzen
                                      States[DpCount] = { id: praefix0 + "." + did + "." + DefineDevice[x].common[y].name, common: DefineDevice[x].common[y] }; //
                                      DpCount++;
                                      };
                                      };
                                      };
                                      }

                                      function PrepareGenericDps(did) { //GenericDps
                                      // if (logging) log("Reaching PrepareGenericDps(did)");
                                      for (let y in DefineDevice[0].info) { //Alle info propertys des models durchgehen und Var zusammensetzen
                                      // log("DefineDevice[0].info[y]=" + JSON.stringify(DefineDevice[0].info[y].common.name))
                                      States[DpCount] = { id: praefix0 + "." + did + ".info." + DefineDevice[0].info[y].id, common: DefineDevice[0].info[y].common }; //
                                      DpCount++;
                                      };
                                      }

                                      function CreateStates() {
                                      if (logging) log("Reaching CreateStates()");

                                      //Alle States anlegen, Main aufrufen wenn fertig
                                      let numStates = States.length;
                                      States.forEach(function (state) {
                                          createState(state.id, state.initial, state.forceCreation, state.common, function () {
                                              numStates--;
                                              if (numStates === 0) {
                                                  if (logging) log(States.length + " States created, now setting up channels!");
                                                  setObject(praefix0, { type: 'channel', common: { name: "" }, native: {} }); //Root zum Channel machen
                                                  for (let x = 0; x < AllDevicesRaw.length; x++) {
                                                      setObject(praefix0 + "." + AllDevicesRaw[x].did, { type: 'device', common: { name: AllDevicesRaw[x].name }, native: {} }); //DeviceChannels machen
                                                      // if (logging) log("AllDevicesRaw[x]=" +JSON.stringify (AllDevicesRaw[x]))
                                                  };
                                                  main();
                                              };
                                          });
                                      });
                                      

                                      }

                                      async function main() {
                                      if (logging) log("Reaching main");
                                      await WriteGenericDpValues();
                                      await CreateDevices();
                                      CreateDpTrigger();
                                      }

                                      function WriteGenericDpValues() { //Alle vorhandenen generischen Werte einlesen und in Dps schreiben
                                      if (logging) log("Reaching WriteGenericDpValues()");
                                      for (let x in AllDevicesRaw) { //Alle vorhandenen Xiaomi Devices durchgehen
                                      for (let y in DefineDevice[0].info) { //Nimm ersten Eintrag aus DefineDevices da die Generics bei allen gleich sind
                                      setState(praefix0 + "." + AllDevicesRaw[x].did + ".info." + DefineDevice[0].info[y].id, AllDevicesRaw[x][DefineDevice[0].info[y].id], true);
                                      // log("DefineDevice[" + 0 + "].info[" + y + "]=" + JSON.stringify(DefineDevice[0].info[y]));
                                      // log("" + praefix0 + "." + AllDevicesRaw[x].did + ".Info." + DefineDevice[0].info[y].id)
                                      // log(AllDevicesRaw[x][DefineDevice[0].info[y].id])
                                      };
                                      };
                                      return true;
                                      }

                                      //################################################

                                      //Step 1 - Einloggen in die Cloud und abrufen aller Gerätedaten, dann vorbereiten der allgemeinen und devicespezifischen Datenpunkte mit anschließendem anlegen derselben
                                      async function Init() { //Cloudlogin und auslesen der gesamten Clouddaten
                                      if (logging) log("Reaching init");

                                      mihome.miioProtocol.init();// local miIO
                                      
                                      try {// cloud MIoT Login
                                          await mihome.miCloudProtocol.login(username, password); //Versuch einzuloggen
                                      }
                                      catch {
                                          log("You are already logged in, login canceled"); //Wenn schon eingeloggt
                                      };
                                      log("Retrieving your in " + options.country + " registered MiHome Devices");
                                      AllDevicesRaw = await mihome.miCloudProtocol.getDevices(null, options); //Gibt alle vorhandenen Devices zurück und weist die Werte einem lokalen Array zu
                                      log("Found " + AllDevicesRaw.length + " MiHome Devices, those are:");
                                      
                                      for (let x = 0; x < AllDevicesRaw.length; x++) { //Alle beim User vorhandenen Xiaomi Devices durchgehen
                                          log(AllDevicesRaw[x].name);
                                          await PrepareGenericDps(AllDevicesRaw[x].did); //und allgemeine generische Infos Dps vorbereiten
                                      };
                                      log("Now searching for supported Devices...");
                                      let NoDeviceMatch = true;
                                      for (let x = 0; x < AllDevicesRaw.length; x++) { //Jetzt erneut alle beim User vorhandenen Xiaomi Devices durchgehen
                                          for (let y = 0; y < DefineDevice.length; y++) { //und abgleichen mit von Skript und node-mihome unterstützten Geräten
                                              if (AllDevicesRaw[x].model == DefineDevice[y].model) { //Bei match Devicespezifische DPs vorbereiten
                                                  log("Device " + AllDevicesRaw[x].name + " is supported, creating DataPoints");
                                                  await PrepareDeviceDps(AllDevicesRaw[x].did, AllDevicesRaw[x].model);
                                                  NoDeviceMatch = false;
                                              };
                                          };
                                      };
                                      if (NoDeviceMatch) {
                                          log("No supported Devices found!", "warn");
                                      }
                                      CreateStates();
                                      

                                      }

                                      //################################################

                                      //Step 2 - Deviceobjekte anlegen, Trigger erzeugen und Datenpunktrefresh initieren
                                      async function CreateDevices() {
                                      if (logging) log("Reaching CreateDevices ");
                                      let z = 0;

                                      for (let x in AllDevicesRaw) { //Alle beim User gefundenen Geräte durchlaufen
                                          for (let y in DefineDevice) { //Alle vom Skript unterstützten und definierten Devices durchlaufen
                                              if (AllDevicesRaw[x].model == DefineDevice[y].model) { //Wenn die beiden matchen
                                                  log("Now creating device for " + AllDevicesRaw[x].model + " / " + AllDevicesRaw[x].did + " / " + AllDevicesRaw[x].localip + " / " + AllDevicesRaw[x].token + " / " + refresh);
                                      
                                                  device[z] = mihome.device({
                                                      id: AllDevicesRaw[x].did, // required, device id
                                                      model: AllDevicesRaw[x].model, // required, device model "zhimi.airpurifier.mb3"
                                                      address: AllDevicesRaw[x].localip, // miio-device option, local ip address
                                                      token: AllDevicesRaw[x].token, // miio-device option, device token 4ff8a96292d0451c5148142a0a851e4f
                                                      refresh: refresh // miio-device option, device properties refresh interval in ms
                                                  });
                                                  device[z].model = AllDevicesRaw[x].model;
                                                  device[z].setter = DefineDevice[y].setter;
                                                  device[z].definition = DefineDevice[y];
                                                  device[z].firstrun = true;
                                                  log("Created device " + JSON.stringify(device[z]) + " now fetching data");
                                                  await device[z].init(); // connect to device and poll for properties
                                                  log("Init Device# " + z + " - device=" + JSON.stringify(device[z].model));
                                                  z++;
                                              };
                                          };
                                      };
                                      
                                      for (let i in device) { //Datenabruf zyklisch entsprechend refresh
                                          log("Setting trigger #" + i + " for " + device[i].model);
                                          device[i].on('properties', (data) => {
                                              if (typeof data != "undefined" && data != {}) {
                                                  if (JSON.stringify(device[i].data) !== JSON.stringify(data)) {//Prüfen ob Datenänderung pro device
                                                      //if (logging) log(data)
                                                      if (typeof device[i].data == "undefined") {
                                                          device[i].data = data;
                                                      };
                                                      RefreshDps(i, data);
                                                  };
                                              } else {
                                                  log("Data was empty (undefined), aborting refresh", "warn");
                                              };
                                          });
                                      };
                                      
                                      onStop(function () { //Bei Scriptende alle Devices löschen
                                          for (let x in device) {
                                              device[x].destroy();
                                          };
                                          unsubscribe('properties');
                                      }, 10);
                                      

                                      }

                                      function RefreshDps(DeviceIndex, NewData) {
                                      // if (logging) log("Reaching RefreshDps at " + device[DeviceIndex].definition.description);

                                      for (let x in device[DeviceIndex].data) { //Alle properties des Devices durchgehen
                                          for (let i in NewData) {//Alle properties des Datenblocks durchgehen
                                              if ((NewData[i] !== device[DeviceIndex].data[x] || device[DeviceIndex].firstrun == true) && i === x) { //Überprüfen ob Datenänderung im property bei propertymatch, firstrun immer refreshen
                                      
                                                  if (logging) log("New Data at " + device[DeviceIndex].model + " " + i + " oldvalue=" + device[DeviceIndex].data[x] + " newvalue=" + NewData[i] + " path=" + praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x));
                                                  device[DeviceIndex].data[x] = NewData[i]; //Geänderten Wert ins Objekt schreiben
                                      
                                                  for (let y in device[DeviceIndex].definition.common) { //Alle Definitionsproperties durchgehen um ChannelId zu suchen
                                                      if (device[DeviceIndex].definition.common[y].name == CorrectChannelId(x) && device[DeviceIndex].definition.common[y].read == true) { //Wenn match und read=true
                                                          if (typeof device[DeviceIndex].data[x] == 'undefined' || typeof device[DeviceIndex].id == 'undefined') { //Wenn kein Pfad oder keine Daten
                                                              log("Empty packet for " + device[DeviceIndex].definition.common[y].name + ", skipping refresh", 'warn');
                                                              //return false;
                                                          } else {
                                                              //Prüfung auf bestimmte keys
                                                              switch (device[DeviceIndex].definition.common[y].name) {
                                                                  case 'temp_dec': //Umwandlung von 10tel Grad auf Grad beim Dp schreiben. data muß unverändert bleiben da im Trigger mit origdaten abgeglichen wird
                                                                      setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), parseInt(device[DeviceIndex].data[x]) / 10, true);
                                                                      break;
                                                                  default: //Wenn kein Treffer jetzt Prüfung auf bestimmte Daten
                                                                      switch (device[DeviceIndex].data[x]) {
                                                                          case 'on':
                                                                              setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), true, true);
                                                                              break;
                                                                          case 'off':
                                                                              setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), false, true);
                                                                              break;
                                                                          default:
                                                                              setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), device[DeviceIndex].data[x], true);
                                                                      };
                                                              };
                                                          };
                                                          if (logging) log("Refreshing " + praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x) + " / value=" + device[DeviceIndex].data[x] + " / read=" + device[DeviceIndex].definition.common[y].read + " write=" + device[DeviceIndex].definition.common[y].write);
                                                      };
                                                  };
                                              };
                                          };
                                      };
                                      device[DeviceIndex].firstrun = false;
                                      //if (logging) log(DeviceData[DeviceIndex])
                                      return true;
                                      

                                      }

                                      async function SetDevice(i, key, keyvalue) {
                                      if (logging) log("Reaching SetDevice - i=" + i + " key=" + key + " keyvalue=" + keyvalue);
                                      if (device[i].definition.common[key].name == 'rgb') { //Block wandelt Farb Hewwerte in Dezimalwert um
                                      keyvalue = ConvertHexToDezi(keyvalue);
                                      keyvalue = String(keyvalue);
                                      }
                                      if (device[i].definition.common[key].name == 'hue') { //Block kombiniert bei Änderungen von hue oder sat jweils die beiden Werte zu Array für übergabe an Funktion
                                      keyvalue = [keyvalue, device[i].data['sat']];
                                      } else if (device[i].definition.common[key].name == 'sat') {
                                      keyvalue = [device[i].data['hue'], keyvalue];
                                      for (let x in device[i].definition.common) {
                                      if (device[i].definition.common[x].name == 'hue') {
                                      key = x;
                                      };
                                      };
                                      };
                                      log("Keyvalue=" + keyvalue + " key=" + key)
                                      if (logging) log("Reaching SetDevice i=" + i);
                                      // log("Setting Device " + device[i].model + " to value " + keyvalue + " at " + device[i].definition.common[key].name)
                                      // log("Setting Device " + device[i].model + " to value " + keyvalue + " at " + device[i].setter[device[i].definition.common[key].name])
                                      log(device[i].setter[device[i].definition.common[key].name](i, keyvalue)); //Diese Zeile nicht entfernen, Funktionsaufruf!!!
                                      }

                                      function CreateDpTrigger() {
                                      if (logging) log("Reaching CreateDpTrigger");

                                      for (let i in device) {
                                          if (logging) log("Setting DataPointTrigger #" + i + " for " + device[i].model);
                                      
                                          for (let x in device[i].definition.common) { //Alle properties der Devicedefinition durchgehen
                                              if (device[i].definition.common[x].write) {
                                                  //  log(praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name)
                                                  on({ id: praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name, change: "ne", ack: false }, function (dp) { //Bei Statusänderung
                                                      if (logging) log("Triggered i=" + i + " x=" + x + " ack=" + dp.state.ack);
                                                      SetDevice(i, x, CheckDataTypeAndConvert(dp.state.val, device[i].definition.common[x].type));
                                                  });
                                                  if (logging) log("Setting Datapoint Trigger for " + praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name + " / read=" + device[i].definition.common[x].read + " write=" + device[i].definition.common[x].write);
                                              } else {
                                                  if (logging) log("No Datapoint Trigger set for " + device[i].definition.common[x].name + " because its readonly.");
                                              };
                                          };
                                      };
                                      

                                      }

                                      //***************** U T I L S *********************** */
                                      function CheckDataTypeAndConvert(value, HasToBe) {
                                      if (typeof value == "string" && HasToBe == "number") {
                                      if (parseFloat(value) == parseInt(value)) { //Umgewandelter String ist Int
                                      return parseInt(value);
                                      } else {
                                      return parseFloat(value);
                                      };
                                      } else {
                                      return value;
                                      };
                                      }

                                      function CorrectChannelId(cid) { //Erzeugt eine iobroker taugliche channelid. Punkte werden zu unterstrichen gewandelt, Doppelpunkte zu Punkten
                                      let temp = cid;
                                      if (typeof temp == "object") {
                                      temp = JSON.stringify(cid);
                                      };
                                      cid = temp.replace(".", '_'); //Erstmal alle potentiellen Punkte im channelnamen entfernen
                                      temp = cid.replace(/:/g, "."); //Jetzt alle Doppelpunkte durch Punkte ersetzen
                                      return temp;
                                      }

                                      function ConvertDeziToHex(DeziValue) {
                                      DeziValue = toInt(DeziValue)
                                      //log(typeof DeziValue)
                                      if (typeof DeziValue == "number") {
                                      return "#" + DeziValue.toString(16)
                                      };
                                      }

                                      function ConvertHexToDezi(HexValue) {

                                      if (typeof HexValue == "string") {
                                          if (HexValue.indexOf('#') != -1) {
                                              HexValue = HexValue.substr(1).toLowerCase()
                                          } else {
                                              HexValue = HexValue.toLowerCase()
                                          };
                                      };
                                      
                                      var i, j, digits = [0], carry;
                                      for (i = 0; i < HexValue.length; i += 1) {
                                          carry = parseInt(HexValue.charAt(i), 16);
                                          for (j = 0; j < digits.length; j += 1) {
                                              digits[j] = digits[j] * 16 + carry;
                                              carry = digits[j] / 10 | 0;
                                              digits[j] %= 10;
                                          }
                                          while (carry > 0) {
                                              digits.push(carry % 10);
                                              carry = carry / 10 | 0;
                                          }
                                      }
                                      return digits.reverse().join('');
                                      

                                      }

                                      /*

                                      • Converts an RGB color value to HSL. Conversion formula

                                      • adapted from http://en.wikipedia.org/wiki/HSL_color_space.

                                      • Assumes r, g, and b are contained in the set [0, 255] and

                                      • returns h, s, and l in the set [0, 1].

                                      • @param {number} r The red color value

                                      • @param {number} g The green color value

                                      • @param {number} b The blue color value

                                      • @return {Array} The HSL representation
                                        */
                                        function rgbToHsl(hex) {
                                        var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);

                                        var r = parseInt(result[1], 16);
                                        var g = parseInt(result[2], 16);
                                        var b = parseInt(result[3], 16);

                                        r /= 255, g /= 255, b /= 255;
                                        var max = Math.max(r, g, b),
                                        min = Math.min(r, g, b);
                                        var h, s, l = (max + min) / 2;

                                        if (max == min) {
                                        h = s = 0; // achromatic
                                        } else {
                                        var d = max - min;
                                        s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
                                        switch (max) {
                                        case r:
                                        h = (g - b) / d + (g < b ? 6 : 0);
                                        break;
                                        case g:
                                        h = (b - r) / d + 2;
                                        break;
                                        case b:
                                        h = (r - g) / d + 4;
                                        break;
                                        }
                                        h /= 6;
                                        }

                                        return [Math.round(h * 360), Math.round(s * 100)];
                                        }

                                      Synology DS218+ & 2 x Fujitsu Esprimo (VM/Container) + FritzBox7590 + 2 AVM 3000 Repeater & Homematic & HUE & Osram & Xiaomi, NPM 10.9.4, Nodejs 22.21.0 ,JS Controller 7.0.7 ,Admin 7.7.19

                                      L 1 Antwort Letzte Antwort
                                      0
                                      • haselchenH haselchen

                                        @lassmichichmachdasselberkaputt

                                        Hast du auch mal nen früheres Skript probiert?
                                        Hab dir mal meine 23er Version hier reingepackt.
                                        Haste axios auch als Modul in dem Adapter eingetragen?


                                        const SkriptVersion = "0.2.23"; //vom 30.06.2021 / Link zu Git: https://github.com/Pittini/iobroker-nodemihome / Forum: https://forum.iobroker.net/topic/39388/vorlage-xiaomi-airpurifier-3h-u-a-inkl-token-auslesen

                                        const mihome = require('node-mihome');

                                        // Logindaten für Xiaomi Cloud:
                                        const username = 'XXXXXXX';
                                        const password = 'XXXXXXX';
                                        const options = { country: 'de' }; // 'ru', 'us', 'tw', 'sg', 'cn', 'de' (Default: 'cn');
                                        const refresh = 13000; // Alle 13sek neue Daten

                                        const praefix0 = "javascript.0.MiHomeAll"; //Root für Skriptdatenpunkte

                                        const logging = false; //Logging aktivieren/deaktivieren

                                        //Ab hier nix mehr ändern!
                                        /*

                                        1. Xiaomi Cloudlogin
                                        2. Alle dort gelisteten Geräte und deren Basicdaten abrufen
                                        3. Für alle abgerufenen Geräte Basic Channel/Datenpunkte anlegen
                                        4. Prüfen welche supporteten Geräte in der Auflistung vorhanden sind und die entsprechenden spezifischen Datenpunkte erstellen
                                        5. Basic Channels mit Daten füllen / einlesen
                                        6. devicearray erstellen via node-mihome und die Gerätespezifischen Werte einlesen
                                          */
                                          // ######### TESTBEREICH ################
                                          //const axios = require('axios');

                                        //let miotDefinition= getMiotData('https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc1:1');

                                        async function getMiotData(url) {
                                        if (logging) log("Reaching MiotUrlConstructor");
                                        try {
                                        const response = await axios.get(url, { timeout: 10000 });
                                        log("resp:" + JSON.stringify(response.data));
                                        for (let z in Object.keys(response.data.services)) {
                                        log("Keys=" + Object.keys(response.data.services[z]))
                                        log(JSON.stringify(response.data.services[z]))
                                        }

                                            return response.data;
                                        } catch (error) {
                                            console.error(error);
                                        }
                                        

                                        }

                                        function MiotUrlConstructor(miotdevice) {
                                        if (logging) log("Reaching MiotUrlConstructor");
                                        let BaseUrl = "https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:" + miotdevice;
                                        return BaseUrl;
                                        }

                                        // ################ ENDE TESTBEREICH ####################

                                        const DeviceData = [];
                                        let AllDevicesRaw = [];

                                        let device = [];

                                        const States = [];
                                        let DpCount = 0;
                                        log("Starting AllMyMi V." + SkriptVersion);

                                        Init();

                                        const DefineDevice = [];

                                        // ***************************** Device Definitions *************************

                                        // ***************************** Airpurifiers ********************************

                                        DefineDevice[0] = { // Tested and working
                                        info: {},
                                        model: "zhimi.airpurifier.mb3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mb3:2
                                        description: "Purifier 3H",
                                        setter: {
                                        "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "motor-speed.favorite-fan-level": async function (obj, val) { await device[obj].setFavLevel(val) },
                                        "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                        { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                        { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                        { name: "alarm.alarm", type: "boolean", read: true, write: true },
                                        { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                        { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "motor-speed.motor-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                        { name: "motor-speed.motor-set-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                        { name: "motor-speed.favorite-fan-level", type: "number", read: true, write: true, min: 0, max: 14 },
                                        { name: "use-time.use-time", type: "number", read: true, write: false },
                                        { name: "environment.relative-humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[20] = { // tested and ok
                                        info: {},
                                        model: "zhimi.airpurifier.mc1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc1:1
                                        description: "Purifier 2S",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                        "led": async function (obj, val) { await device[obj].setLed(val) },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                        { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 1, max: 10 },
                                        { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                        { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[22] = {
                                        info: {},
                                        model: "zhimi.airpurifier.m1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-m1:1
                                        description: "Purifier 2",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                        "led": async function (obj, val) { await device[obj].setLed(val) },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                        { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 1, max: 10 },
                                        { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                        { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[8] = { // Tested and working
                                        info: {},
                                        model: "zhimi.airpurifier.mc2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mc2:1
                                        description: "Purifier 2H",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "favorite_level": async function (obj, val) { await device[obj].setFavoriteLevel(val) },
                                        "led": async function (obj, val) { await device[obj].setLed(val) },
                                        "led_b": async function (obj, val) { await device[obj].setLedB(val) },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", role: "state", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                        { name: "favorite_level", type: "number", role: "state", read: true, write: true, min: 0, max: 16 },
                                        { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false, min: -40.0, max: 525.0, unit: "°C" },
                                        { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "average_aqi", type: "number", role: "value", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "led_b", type: "number", role: "state", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "filter1_life", type: "number", role: "value", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "f1_hour", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "f1_hour_used", type: "number", role: "value", read: true, write: false, unit: "h" },
                                        { name: "motor1_speed", type: "number", role: "value", read: true, write: false, unit: "rpm" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[13] = { // Tested and working
                                        info: {},
                                        model: "zhimi.airpurifier.vb2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-vb2:1
                                        description: "Mi Air Purifier Pro H",
                                        setter: {
                                        "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "motor-speed.favorite-level": async function (obj, val) { await device[obj].setFavLevel(val) },
                                        "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "alarm.volume": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                        { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                        { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                        { name: "alarm.volume", type: "number", read: true, write: true, min: 0, max: 100 },
                                        { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                        { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "motor-speed.motor1-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                        { name: "motor-speed.motor1-set-speed", type: "number", read: true, write: false, min: 0, max: 3000, unit: "rpm" },
                                        { name: "motor-speed.favorite-level", type: "number", read: true, write: true, min: 0, max: 14 },
                                        { name: "use-time.use-time", type: "number", read: true, write: false },
                                        { name: "environment.relative-humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[14] = { // Tested and working
                                        info: {},
                                        model: "zhimi.airpurifier.v7",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-v7:1
                                        description: "Mi Air Purifier",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "led": async function (obj, val) { await device[obj].setDisplay(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "favorite": "favorite" } },
                                        { name: "favorite_level", type: "number", read: true, write: false, min: 0, max: 16 },
                                        { name: "temp_dec", type: "number", role: "value.temperature", read: true, write: false },
                                        { name: "humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "aqi", type: "number", read: true, write: false, unit: "μg/m³" },
                                        { name: "average_aqi", type: "number", read: true, write: false, unit: "μg/m³" },
                                        { name: "led", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "bright", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "volume", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "filter1_life", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "f1_hour", type: "number", read: true, write: false },
                                        { name: "f1_hour_used", type: "number", read: true, write: false },
                                        { name: "motor1_speed", type: "number", read: true, write: false, unit: "rpm" },
                                        { name: "motor2_speed", type: "number", read: true, write: false, unit: "rpm" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[15] = { // Tested and working
                                        info: {},
                                        model: "zhimi.airpurifier.mb4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-mb4:2
                                        description: "Purifier 3C",
                                        setter: {
                                        "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "custom-service.favorite-speed": async function (obj, val) { await device[obj].setFavSpeed(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "screen.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "air-purifier.fault", type: "string", read: true, write: false },
                                        { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "fanset" } },
                                        { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "screen.brightness", type: "number", read: true, write: true, min: 0, max: 8 },
                                        { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "custom-service.moto-speed-rpm", type: "number", read: true, write: false, min: 0, max: 65535, unit: "rpm" },
                                        { name: "custom-service.favorite-speed", type: "number", read: true, write: true, min: 300, max: 2300, unit: "rpm" },
                                        { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        DefineDevice[18] = { // Untested
                                        info: {},
                                        model: "zhimi.airpurifier.ma4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:air-purifier:0000A007:zhimi-ma4:1
                                        description: "Air Purifier",
                                        setter: {
                                        "air-purifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "air-purifier.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "air-purifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "indicator-light.brightness": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "air-purifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "air-purifier.fault", type: "number", read: true, write: false, min: 0, max: 5, states: { 0: "No faults", 1: "m1_run", 2: "m1_stuck", 3: "no_sensor", 4: "error_hum", 5: "error_temp", 6: "timer_error1", 7: "timer_error2" } },
                                        { name: "air-purifier.mode", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "sleep", 2: "favorite", 3: "none" } },
                                        { name: "air-purifier.fan-level", type: "number", read: true, write: true, min: 1, max: 3 },
                                        { name: "alarm.alarm", type: "boolean", read: true, write: true, min: false, max: true },
                                        { name: "indicator-light.brightness", type: "number", read: true, write: true, min: 0, max: 2 },
                                        { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "environment.relative-humidity", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "environment.pm2_5-density", type: "number", read: true, write: false, min: 0, max: 600, unit: "μg/m³" },
                                        { name: "filter.filter-life-level", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "filter.filter-used-time", type: "number", read: true, write: false, unit: "h" },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };

                                        // ***************************** Fans *********************************
                                        // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:zhimi-sa1:1

                                        DefineDevice[1] = { // untested
                                        info: {},
                                        model: "leshow.fan.ss4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:leshow-ss4:1
                                        description: "Leshow Fan",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "blow": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "yaw": async function (obj, val) { await device[obj].setFanSwing(val ? 'on' : 'off') },
                                        "mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                        "sound": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                        "timer": async function (obj, val) { await device[obj].setTimer(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "blow", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "yaw", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "mode", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "sound", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "timer", type: "number", read: true, write: true, min: 0, max: 540, unit: "m" }]
                                        };

                                        DefineDevice[9] = { // Tested and working
                                        info: {},
                                        model: "zhimi.fan.za4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:zhimi-za4:1
                                        description: "Mi Fan 2S",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "angle": async function (obj, val) { await device[obj].setSwingAngle(val) },
                                        "angle_enable": async function (obj, val) { await device[obj].setSwing(val) },
                                        "natural_level": async function (obj, val) { await device[obj].setSleepMode((val == 1) ? true : false) },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val ? 'on' : 'off') },
                                        "led_b": async function (obj, val) { await device[obj].setLcdBrightness(val) },
                                        "speed_level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "poweroff_time": async function (obj, val) { await device[obj].setTimer(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "angle", type: "number", read: true, write: true, min: 1, max: 120 },
                                        { name: "angle_enable", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "natural_level", type: "number", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "led_b", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "speed_level", type: "number", read: true, write: true, min: 1, max: 100, unit: "%" },
                                        { name: "poweroff_time", type: "number", read: true, write: true, min: 0, max: 540, unit: "m" }]
                                        };

                                        DefineDevice[17] = { // Tested and working
                                        info: {},
                                        model: "dmaker.fan.p15",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p15:1
                                        description: "Mi Smart Standing Fan Pro",
                                        setter: {
                                        "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                        "fan.horizontal-angle": async function (obj, val) { await device[obj].setHorizontalAngle(val) },
                                        "indicator-light.on": async function (obj, val) { await device[obj].setIndicatorLight(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                        "motor-controller.motor-control": async function (obj, val) { await device[obj].setMotorController(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                        "off-delay-time.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                        },
                                        common:
                                        [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                        { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 4, states: { 1: "Slow", 2: "Middle", 3: "High", 4: "Turbo" } },
                                        { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.horizontal-angle", type: "number", role: "switch", read: true, write: true, min: 30, max: 140, unit: "°", states: { 30: "30°", 60: "60°", 90: "90°", 120: "120°", 140: "140°" } },
                                        { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                        { name: "indicator-light.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "motor-controller.motor-control", type: "number", role: "switch", read: false, write: true, min: 0, max: 2, states: { 0: "None", 1: "Left", 2: "Right" } },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "off-delay-time.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                        ]
                                        };

                                        DefineDevice[19] = { // untested
                                        info: {},
                                        model: "dmaker.fan.1c",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-1c:1
                                        description: "Mi Smart Standing Fan 1C",
                                        setter: {
                                        "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                        "fan.brightness": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "fan.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                        "fan.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                        },
                                        common:
                                        [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Sleep" } },
                                        { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 3, states: { 1: "Slow", 2: "Middle", 3: "High" } },
                                        { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                        { name: "fan.brightness", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "fan.alarm", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "fan.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                        ]
                                        };

                                        DefineDevice[23] = { // Tested and working
                                        info: {},
                                        model: "dmaker.fan.p18",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:fan:0000A005:dmaker-p18:1
                                        description: "Mi Smart Standing Fan 2",
                                        setter: {
                                        "fan.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "fan.mode": async function (obj, val) { await device[obj].setMode(val) },
                                        "fan.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "fan.horizontal-swing": async function (obj, val) { await device[obj].setHorizontalSwing(val) },
                                        "fan.horizontal-angle": async function (obj, val) { await device[obj].setHorizontalAngle(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setAlarm(val) },
                                        "motor-controller.motor-control": async function (obj, val) { await device[obj].setMotorController(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) },
                                        "off-delay-time.off-delay-time": async function (obj, val) { await device[obj].setOffDelayTime(val) }
                                        },
                                        common:
                                        [{ name: "fan.on", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.mode", type: "number", role: "switch", read: true, write: true, min: 0, max: 1, states: { 0: "Straight Wind", 1: "Natural Wind" } },
                                        { name: "fan.fan-level", type: "number", role: "switch", read: true, write: true, min: 1, max: 4, states: { 1: "Slow", 2: "Middle", 3: "High", 4: "Turbo" } },
                                        { name: "fan.horizontal-swing", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "fan.horizontal-angle", type: "number", role: "switch", read: true, write: true, min: 30, max: 140, unit: "°", states: { 30: "30°", 60: "60°", 90: "90°", 120: "120°", 140: "140°" } },
                                        { name: "fan.status", type: "number", role: "indicator", read: true, write: false, min: 1, max: 100 },
                                        { name: "alarm.alarm", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "motor-controller.motor-control", type: "number", role: "switch", read: false, write: true, min: 0, max: 2, states: { 0: "None", 1: "Left", 2: "Right" } },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "off-delay-time.off-delay-time", type: "number", role: "switch", read: true, write: true, min: 0, max: 480, unit: "m" }
                                        ]
                                        };

                                        // ***************************** Lights *********************************

                                        DefineDevice[2] = { // Tested and ok except setting color
                                        info: {},
                                        model: "yeelink.light.strip2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-color2:1
                                        description: "Yeelight Lightstrip Plus",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "hue": async function (obj, val) { await device[obj].setColorHSV(val) },
                                        "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                        "ct": async function (obj, val) { await device[obj].setCt(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "hue", type: "number", read: true, write: true, min: 0, max: 16777215 },
                                        { name: "sat", type: "number", read: true, write: true, min: 0, max: 100 },
                                        { name: "color_mode", type: "number", read: true, write: true, min: 1, max: 2 },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };

                                        DefineDevice[5] = { // Tested and working
                                        info: {},
                                        model: "yeelink.light.ct2",// http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ct2:1
                                        description: "Yeelight LED Bulb (Tunable)",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "ct": async function (obj, val) { await device[obj].setColorTemperature(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };

                                        DefineDevice[6] = { // Tested and working except color setting
                                        info: {},
                                        model: "yeelink.light.color2",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-color2:1
                                        description: "Yeelight LED Bulb (Color)",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "rgb": async function (obj, val) { await device[obj].setColorRgb(val) },
                                        "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                        "ct": async function (obj, val) { await device[obj].setCt(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "rgb", type: "number", read: true, write: true, min: 1, max: 16777215 },
                                        { name: "color_mode", type: "number", read: true, write: true, min: 1, max: 2 },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };
                                        DefineDevice[10] = { // untested
                                        info: {},
                                        model: "yeelink.light.ceiling3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling3:1
                                        description: "Yeelight LED Ceiling Light",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "nl_br": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "night_mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                        "ct": async function (obj, val) { await device[obj].setCt(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "nl_br", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "night_mode", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };
                                        DefineDevice[11] = { // untested
                                        info: {},
                                        model: "yeelink.light.ceiling1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling1:1
                                        description: "Yeelight Crystal Pedestal Light",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "nl_br": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "night_mode": async function (obj, val) { await device[obj].setSleepMode(val) },
                                        "ct": async function (obj, val) { await device[obj].setCt(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "nl_br", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "night_mode", type: "boolean", read: true, write: true },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };

                                        DefineDevice[21] = { // untested
                                        info: {},
                                        model: "yeelink.light.ceiling10",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-ceiling10:1
                                        description: "Yeelight Meteorite Pedestal Light",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "main_power": async function (obj, val) { await device[obj].setMainPower(val) },
                                        "bg_power": async function (obj, val) { await device[obj].setBgPower(val) },
                                        "bg_bright": async function (obj, val) { await device[obj].setBgBrightness(val) },
                                        "active_bright": async function (obj, val) { await device[obj].setActiveBrightness(val) },
                                        "color_mode": async function (obj, val) { await device[obj].setColorMode(val) },
                                        "ct": async function (obj, val) { await device[obj].setColorTemperature(val) },
                                        "bg_ct": async function (obj, val) { await device[obj].setBgColorTemperature(val) },
                                        "bg_rgb": async function (obj, val) { await device[obj].setBgColorRgb(val) },
                                        "bg_hue": async function (obj, val) { await device[obj].setBgColorHSV([val, 100]) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "main_power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bg_power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bg_bright", type: "number", role: 'level.dimmer', read: true, write: true, min: 1, max: 100, unit: "%" },
                                        { name: "active_bright", type: "number", role: 'level.dimmer', read: true, write: true, min: 1, max: 100, unit: "%" },
                                        { name: "color_mode", type: "boolean", role: 'switch.mode.color', read: true, write: true },
                                        { name: "moon_mode", type: "boolean", role: 'switch.mode.moon', read: true, write: true },
                                        { name: "bg_ct", type: "number", role: 'level.color.temperature', read: true, write: true, min: 2600, max: 6500, unit: "K" },
                                        { name: "ct", type: "number", role: 'level.color.temperature', read: true, write: true, min: 2600, max: 6500, unit: "K" },
                                        { name: "bg_rgb", type: "string", role: 'level.color.rgb', read: true, write: true },
                                        { name: "bg_hue", type: "number", role: 'level.color.hue', read: true, write: true, min: 0, max: 359 },
                                        { name: "bg_sat", type: "number", role: 'level.color.saturation', read: true, write: true, min: 1, max: 100 }]
                                        };

                                        DefineDevice[16] = { // untested
                                        info: {},
                                        model: "yeelink.light.lamp4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:yeelink-lamp4:1
                                        description: "Mi LED Desk Lamp 1S",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "bright": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "ct": async function (obj, val) { await device[obj].setColorTemperature(val) }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true },
                                        { name: "bright", type: "number", read: true, write: true, min: 1, max: 100 },
                                        { name: "ct", type: "number", read: true, write: true, min: 1700, max: 6500 }]
                                        };

                                        // ***************************** Humidifier *********************************

                                        DefineDevice[3] = { // Tested and working
                                        info: {},
                                        model: "zhimi.humidifier.cb1",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:zhimi-cb1:1
                                        description: "Smartmi Evaporative Humidifier",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                        "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "limit_hum": async function (obj, val) { await device[obj].setTargetHumidity(val) },
                                        "led": async function (obj, val) { await device[obj].setLedBrightness(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val) },
                                        "dry": async function (obj, val) { await device[obj].setMode(val ? 'dry' : 'humidify') }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "depth", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "limit_hum", type: "number", read: true, write: true, min: 0, max: 100, unit: "%", states: { 30: "30%", 40: "40%", 50: "50%", 60: "60%", 70: "70%", 80: "80%" } },
                                        { name: "led", type: "number", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "dry", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "medium": "medium", "high": "high" } }]
                                        };
                                        DefineDevice[4] = { // untested
                                        info: {},
                                        model: "deerma.humidifier.jsq",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:deerma-jsq:1
                                        description: "Mi Smart Antibacterial Humidifier",
                                        setter: {
                                        "humidifier.on": async function (obj, val) { await device[obj].setPower(val) },
                                        "humidifier.fan-level": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "alarm.alarm": async function (obj, val) { await device[obj].setBuzzer(val) },
                                        "physical-controls-locked.physical-controls-locked": async function (obj, val) { await device[obj].setChildLock(val) }
                                        },
                                        common:
                                        [{ name: "humidifier.on", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "humidifier.fan-level", type: "number", read: true, write: true, min: 0, max: 3, states: { 0: "auto", 1: "level1", 2: "level2", 3: "level3" } },
                                        { name: "humidifier.water-level", type: "number", read: true, write: false, min: 0, max: 127 },
                                        { name: "alarm.alarm", type: "boolean", read: true, write: true, min: false, max: true },
                                        { name: "environment.temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "environment.relative-humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "physical-controls-locked.physical-controls-locked", type: "boolean", role: "switch", read: true, write: true, min: false, max: true }]
                                        };
                                        DefineDevice[12] = { // untested
                                        info: {},
                                        model: "zhimi.humidifier.ca4",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:zhimi-ca4:1
                                        description: "Smartmi Evaporative Humidifier",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val ? 'on' : 'off') },
                                        "buzzer": async function (obj, val) { await device[obj].setBuzzer(val ? 'on' : 'off') },
                                        "mode": async function (obj, val) { await device[obj].setFanLevel(val) },
                                        "limit_hum": async function (obj, val) { await device[obj].setTargetHumidity(val) },
                                        "led": async function (obj, val) { await device[obj].setLedBrightness(val) },
                                        "child_lock": async function (obj, val) { await device[obj].setChildLock(val ? 'on' : 'off') },
                                        "dry": async function (obj, val) { await device[obj].setMode(val ? 'dry' : 'humidify') }
                                        },
                                        common:
                                        [{ name: "power", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "depth", type: "number", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "limit_hum", type: "number", read: true, write: true, min: 0, max: 100, unit: "%", states: { 30: "30%", 40: "40%", 50: "50%", 60: "60%", 70: "70%", 80: "80%" } },
                                        { name: "led", type: "number", read: true, write: true, min: 0, max: 2, states: { 0: "bright", 1: "dim", 2: "off" } },
                                        { name: "buzzer", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "temperature", type: "number", role: "value.temperature", read: true, write: false, min: -40, max: 125, unit: "°C" },
                                        { name: "humidity", type: "number", role: "value.humidity", read: true, write: false, min: 0, max: 100, unit: "%" },
                                        { name: "child_lock", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "dry", type: "boolean", role: "switch", read: true, write: true, min: false, max: true },
                                        { name: "mode", type: "string", read: true, write: true, states: { "auto": "auto", "silent": "silent", "medium": "medium", "high": "high" } }]
                                        };

                                        // ***************************** Divers *********************************
                                        // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:coffee-machine:0000A049:scishare-s1102:1
                                        // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:camera:0000A01C:mijia-v1:1
                                        // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:plant-monitor:0000A030:hhcc-v1:1
                                        // TODO https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:heater:0000A01A:zhimi-mc2:1

                                        DefineDevice[7] = { // In arbeit - unvollständig
                                        info: {},
                                        model: "lumi.gateway.v3",// https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:gateway:0000A019:lumi-v3:1
                                        description: "Xiaomi RGB Gateway",
                                        setter: {
                                        "power": async function (obj, val) { await device[obj].setPower(val) },
                                        "doorbell_push": async function (obj, val) { await device[obj].setBrightness(val) },
                                        "toggle_light": async function (obj, val) { await device[obj].setLightPower(val) },
                                        },
                                        common:
                                        [{ name: 'illumination', role: 'value.lux', write: false, read: true, type: 'number', unit: 'lux' },
                                        { name: 'rgb', role: 'level.color.rgb', write: true, read: true, type: 'string' },
                                        { name: 'nightlight_rgb', role: 'level.color.rgb', write: true, read: true, type: 'string' },
                                        { name: "mute", type: "boolean", read: true, write: true },
                                        { name: 'toggle_light', role: 'switch', write: true, read: true, type: 'boolean' },
                                        { name: 'light.dimmer', role: 'level.dimmer', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                        { name: 'gateway_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                        { name: 'doorbell_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                        { name: 'alarming_volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100 },
                                        { name: "doorbell_push", type: "boolean", read: true, write: true },
                                        { name: "arming", type: "boolean", read: true, write: true },
                                        { name: "arming_time", type: "number", read: true, write: true },
                                        { name: 'music_ID', role: 'state', write: true, read: false, type: 'number', desc: '10000 - stop, 10005 - custom ringtone' },
                                        { name: 'proto_version', role: 'info', write: false, read: true, type: 'string' },
                                        { name: 'join_permission', role: 'state', write: true, read: true, type: 'string' },
                                        { name: 'remove_device', role: 'state', write: true, read: true, type: 'string' }, // Removing a subdevice (device sid)
                                        { name: 'connected', role: 'indicator.reachable', write: false, read: true, type: 'boolean', desc: 'Will be set to false if no packets received in 20 seconds' }]
                                        };

                                        /*
                                        gateway: {type: 'gateway', fullName: 'Xiaomi RGB Gateway', ClassName: Gateway, states: {
                                        + illumination: {name: 'Illumination', role: 'value.lux', write: false, read: true, type: 'number', unit: 'lux'},
                                        + rgb: {name: 'RGB', role: 'level.color.rgb', write: true, read: true, type: 'string'},
                                        on: {name: 'Light', role: 'switch', write: true, read: true, type: 'boolean'},
                                        dimmer: {name: 'Light', role: 'level.dimmer', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100},
                                        volume: {name: 'Volume', role: 'level.volume', write: true, read: true, type: 'number', unit: '%', min: 0, max: 100},
                                        mid: {name: 'Music ID', role: 'state', write: true, read: false, type: 'number', desc: '10000 - stop, 10005 - custom ringtone'},
                                        proto_version: {name: 'Proto Version', role: 'info', write: false, read: true, type: 'string'},
                                        join_permission:{name: 'Add device', role: 'state', write: true, read: true, type: 'string'}, // Permission to add subdevices (yes / no)
                                        remove_device: {name: 'Remove device', role: 'state', write: true, read: true, type: 'string'}, // Removing a subdevice (device sid)
                                        connected: {name: 'Is gateway connected', role: 'indicator.reachable', write: false, read: true, type: 'boolean', desc: 'Will be set to false if no packets received in 20 seconds'}
                                        }
                                        */

                                        for (let x in DefineDevice) { //An alle Devicedefinitionen die generischen Datenpunkte anhängen
                                        DefineDevice[x].info = [
                                        { id: "localip", initial: "", forceCreation: false, common: { read: true, write: true, name: "Ip Adress", type: "string", role: "value", def: "" } },
                                        { id: "token", initial: "", forceCreation: false, common: { read: true, write: true, name: "Token", type: "string", role: "value", def: "" } },
                                        { id: "did", initial: "", forceCreation: false, common: { read: true, write: true, name: "Device Id", type: "string", role: "value", def: "" } },
                                        { id: "model", initial: "", forceCreation: false, common: { read: true, write: true, name: "Model", type: "string", role: "value", def: "" } },
                                        { id: "rssi", initial: 0, forceCreation: false, common: { read: true, write: false, name: "rssi", type: "number", role: "value.rssi", def: 0 } },
                                        { id: "name", initial: "", forceCreation: false, common: { read: true, write: true, name: "Name", type: "string", role: "value", def: "" } },
                                        { id: "isOnline", initial: false, forceCreation: false, common: { read: true, write: true, name: "Is online", type: "boolean", role: "value", def: false } }]

                                        }

                                        function PrepareDeviceDps(did, model) {
                                        if (logging) log("Reaching PrepareDeviceDps, did=" + did + " model=" + model);
                                        for (let x in DefineDevice) { //Alle definierten Model durchgehen
                                        if (DefineDevice[x].model == model) { //bei Model match
                                        for (let y in DefineDevice[x].common) { //Alle common propertys des models durchgehen und Var zusammensetzen
                                        States[DpCount] = { id: praefix0 + "." + did + "." + DefineDevice[x].common[y].name, common: DefineDevice[x].common[y] }; //
                                        DpCount++;
                                        };
                                        };
                                        };
                                        }

                                        function PrepareGenericDps(did) { //GenericDps
                                        // if (logging) log("Reaching PrepareGenericDps(did)");
                                        for (let y in DefineDevice[0].info) { //Alle info propertys des models durchgehen und Var zusammensetzen
                                        // log("DefineDevice[0].info[y]=" + JSON.stringify(DefineDevice[0].info[y].common.name))
                                        States[DpCount] = { id: praefix0 + "." + did + ".info." + DefineDevice[0].info[y].id, common: DefineDevice[0].info[y].common }; //
                                        DpCount++;
                                        };
                                        }

                                        function CreateStates() {
                                        if (logging) log("Reaching CreateStates()");

                                        //Alle States anlegen, Main aufrufen wenn fertig
                                        let numStates = States.length;
                                        States.forEach(function (state) {
                                            createState(state.id, state.initial, state.forceCreation, state.common, function () {
                                                numStates--;
                                                if (numStates === 0) {
                                                    if (logging) log(States.length + " States created, now setting up channels!");
                                                    setObject(praefix0, { type: 'channel', common: { name: "" }, native: {} }); //Root zum Channel machen
                                                    for (let x = 0; x < AllDevicesRaw.length; x++) {
                                                        setObject(praefix0 + "." + AllDevicesRaw[x].did, { type: 'device', common: { name: AllDevicesRaw[x].name }, native: {} }); //DeviceChannels machen
                                                        // if (logging) log("AllDevicesRaw[x]=" +JSON.stringify (AllDevicesRaw[x]))
                                                    };
                                                    main();
                                                };
                                            });
                                        });
                                        

                                        }

                                        async function main() {
                                        if (logging) log("Reaching main");
                                        await WriteGenericDpValues();
                                        await CreateDevices();
                                        CreateDpTrigger();
                                        }

                                        function WriteGenericDpValues() { //Alle vorhandenen generischen Werte einlesen und in Dps schreiben
                                        if (logging) log("Reaching WriteGenericDpValues()");
                                        for (let x in AllDevicesRaw) { //Alle vorhandenen Xiaomi Devices durchgehen
                                        for (let y in DefineDevice[0].info) { //Nimm ersten Eintrag aus DefineDevices da die Generics bei allen gleich sind
                                        setState(praefix0 + "." + AllDevicesRaw[x].did + ".info." + DefineDevice[0].info[y].id, AllDevicesRaw[x][DefineDevice[0].info[y].id], true);
                                        // log("DefineDevice[" + 0 + "].info[" + y + "]=" + JSON.stringify(DefineDevice[0].info[y]));
                                        // log("" + praefix0 + "." + AllDevicesRaw[x].did + ".Info." + DefineDevice[0].info[y].id)
                                        // log(AllDevicesRaw[x][DefineDevice[0].info[y].id])
                                        };
                                        };
                                        return true;
                                        }

                                        //################################################

                                        //Step 1 - Einloggen in die Cloud und abrufen aller Gerätedaten, dann vorbereiten der allgemeinen und devicespezifischen Datenpunkte mit anschließendem anlegen derselben
                                        async function Init() { //Cloudlogin und auslesen der gesamten Clouddaten
                                        if (logging) log("Reaching init");

                                        mihome.miioProtocol.init();// local miIO
                                        
                                        try {// cloud MIoT Login
                                            await mihome.miCloudProtocol.login(username, password); //Versuch einzuloggen
                                        }
                                        catch {
                                            log("You are already logged in, login canceled"); //Wenn schon eingeloggt
                                        };
                                        log("Retrieving your in " + options.country + " registered MiHome Devices");
                                        AllDevicesRaw = await mihome.miCloudProtocol.getDevices(null, options); //Gibt alle vorhandenen Devices zurück und weist die Werte einem lokalen Array zu
                                        log("Found " + AllDevicesRaw.length + " MiHome Devices, those are:");
                                        
                                        for (let x = 0; x < AllDevicesRaw.length; x++) { //Alle beim User vorhandenen Xiaomi Devices durchgehen
                                            log(AllDevicesRaw[x].name);
                                            await PrepareGenericDps(AllDevicesRaw[x].did); //und allgemeine generische Infos Dps vorbereiten
                                        };
                                        log("Now searching for supported Devices...");
                                        let NoDeviceMatch = true;
                                        for (let x = 0; x < AllDevicesRaw.length; x++) { //Jetzt erneut alle beim User vorhandenen Xiaomi Devices durchgehen
                                            for (let y = 0; y < DefineDevice.length; y++) { //und abgleichen mit von Skript und node-mihome unterstützten Geräten
                                                if (AllDevicesRaw[x].model == DefineDevice[y].model) { //Bei match Devicespezifische DPs vorbereiten
                                                    log("Device " + AllDevicesRaw[x].name + " is supported, creating DataPoints");
                                                    await PrepareDeviceDps(AllDevicesRaw[x].did, AllDevicesRaw[x].model);
                                                    NoDeviceMatch = false;
                                                };
                                            };
                                        };
                                        if (NoDeviceMatch) {
                                            log("No supported Devices found!", "warn");
                                        }
                                        CreateStates();
                                        

                                        }

                                        //################################################

                                        //Step 2 - Deviceobjekte anlegen, Trigger erzeugen und Datenpunktrefresh initieren
                                        async function CreateDevices() {
                                        if (logging) log("Reaching CreateDevices ");
                                        let z = 0;

                                        for (let x in AllDevicesRaw) { //Alle beim User gefundenen Geräte durchlaufen
                                            for (let y in DefineDevice) { //Alle vom Skript unterstützten und definierten Devices durchlaufen
                                                if (AllDevicesRaw[x].model == DefineDevice[y].model) { //Wenn die beiden matchen
                                                    log("Now creating device for " + AllDevicesRaw[x].model + " / " + AllDevicesRaw[x].did + " / " + AllDevicesRaw[x].localip + " / " + AllDevicesRaw[x].token + " / " + refresh);
                                        
                                                    device[z] = mihome.device({
                                                        id: AllDevicesRaw[x].did, // required, device id
                                                        model: AllDevicesRaw[x].model, // required, device model "zhimi.airpurifier.mb3"
                                                        address: AllDevicesRaw[x].localip, // miio-device option, local ip address
                                                        token: AllDevicesRaw[x].token, // miio-device option, device token 4ff8a96292d0451c5148142a0a851e4f
                                                        refresh: refresh // miio-device option, device properties refresh interval in ms
                                                    });
                                                    device[z].model = AllDevicesRaw[x].model;
                                                    device[z].setter = DefineDevice[y].setter;
                                                    device[z].definition = DefineDevice[y];
                                                    device[z].firstrun = true;
                                                    log("Created device " + JSON.stringify(device[z]) + " now fetching data");
                                                    await device[z].init(); // connect to device and poll for properties
                                                    log("Init Device# " + z + " - device=" + JSON.stringify(device[z].model));
                                                    z++;
                                                };
                                            };
                                        };
                                        
                                        for (let i in device) { //Datenabruf zyklisch entsprechend refresh
                                            log("Setting trigger #" + i + " for " + device[i].model);
                                            device[i].on('properties', (data) => {
                                                if (typeof data != "undefined" && data != {}) {
                                                    if (JSON.stringify(device[i].data) !== JSON.stringify(data)) {//Prüfen ob Datenänderung pro device
                                                        //if (logging) log(data)
                                                        if (typeof device[i].data == "undefined") {
                                                            device[i].data = data;
                                                        };
                                                        RefreshDps(i, data);
                                                    };
                                                } else {
                                                    log("Data was empty (undefined), aborting refresh", "warn");
                                                };
                                            });
                                        };
                                        
                                        onStop(function () { //Bei Scriptende alle Devices löschen
                                            for (let x in device) {
                                                device[x].destroy();
                                            };
                                            unsubscribe('properties');
                                        }, 10);
                                        

                                        }

                                        function RefreshDps(DeviceIndex, NewData) {
                                        // if (logging) log("Reaching RefreshDps at " + device[DeviceIndex].definition.description);

                                        for (let x in device[DeviceIndex].data) { //Alle properties des Devices durchgehen
                                            for (let i in NewData) {//Alle properties des Datenblocks durchgehen
                                                if ((NewData[i] !== device[DeviceIndex].data[x] || device[DeviceIndex].firstrun == true) && i === x) { //Überprüfen ob Datenänderung im property bei propertymatch, firstrun immer refreshen
                                        
                                                    if (logging) log("New Data at " + device[DeviceIndex].model + " " + i + " oldvalue=" + device[DeviceIndex].data[x] + " newvalue=" + NewData[i] + " path=" + praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x));
                                                    device[DeviceIndex].data[x] = NewData[i]; //Geänderten Wert ins Objekt schreiben
                                        
                                                    for (let y in device[DeviceIndex].definition.common) { //Alle Definitionsproperties durchgehen um ChannelId zu suchen
                                                        if (device[DeviceIndex].definition.common[y].name == CorrectChannelId(x) && device[DeviceIndex].definition.common[y].read == true) { //Wenn match und read=true
                                                            if (typeof device[DeviceIndex].data[x] == 'undefined' || typeof device[DeviceIndex].id == 'undefined') { //Wenn kein Pfad oder keine Daten
                                                                log("Empty packet for " + device[DeviceIndex].definition.common[y].name + ", skipping refresh", 'warn');
                                                                //return false;
                                                            } else {
                                                                //Prüfung auf bestimmte keys
                                                                switch (device[DeviceIndex].definition.common[y].name) {
                                                                    case 'temp_dec': //Umwandlung von 10tel Grad auf Grad beim Dp schreiben. data muß unverändert bleiben da im Trigger mit origdaten abgeglichen wird
                                                                        setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), parseInt(device[DeviceIndex].data[x]) / 10, true);
                                                                        break;
                                                                    default: //Wenn kein Treffer jetzt Prüfung auf bestimmte Daten
                                                                        switch (device[DeviceIndex].data[x]) {
                                                                            case 'on':
                                                                                setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), true, true);
                                                                                break;
                                                                            case 'off':
                                                                                setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), false, true);
                                                                                break;
                                                                            default:
                                                                                setState(praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x), device[DeviceIndex].data[x], true);
                                                                        };
                                                                };
                                                            };
                                                            if (logging) log("Refreshing " + praefix0 + "." + device[DeviceIndex].id + "." + CorrectChannelId(x) + " / value=" + device[DeviceIndex].data[x] + " / read=" + device[DeviceIndex].definition.common[y].read + " write=" + device[DeviceIndex].definition.common[y].write);
                                                        };
                                                    };
                                                };
                                            };
                                        };
                                        device[DeviceIndex].firstrun = false;
                                        //if (logging) log(DeviceData[DeviceIndex])
                                        return true;
                                        

                                        }

                                        async function SetDevice(i, key, keyvalue) {
                                        if (logging) log("Reaching SetDevice - i=" + i + " key=" + key + " keyvalue=" + keyvalue);
                                        if (device[i].definition.common[key].name == 'rgb') { //Block wandelt Farb Hewwerte in Dezimalwert um
                                        keyvalue = ConvertHexToDezi(keyvalue);
                                        keyvalue = String(keyvalue);
                                        }
                                        if (device[i].definition.common[key].name == 'hue') { //Block kombiniert bei Änderungen von hue oder sat jweils die beiden Werte zu Array für übergabe an Funktion
                                        keyvalue = [keyvalue, device[i].data['sat']];
                                        } else if (device[i].definition.common[key].name == 'sat') {
                                        keyvalue = [device[i].data['hue'], keyvalue];
                                        for (let x in device[i].definition.common) {
                                        if (device[i].definition.common[x].name == 'hue') {
                                        key = x;
                                        };
                                        };
                                        };
                                        log("Keyvalue=" + keyvalue + " key=" + key)
                                        if (logging) log("Reaching SetDevice i=" + i);
                                        // log("Setting Device " + device[i].model + " to value " + keyvalue + " at " + device[i].definition.common[key].name)
                                        // log("Setting Device " + device[i].model + " to value " + keyvalue + " at " + device[i].setter[device[i].definition.common[key].name])
                                        log(device[i].setter[device[i].definition.common[key].name](i, keyvalue)); //Diese Zeile nicht entfernen, Funktionsaufruf!!!
                                        }

                                        function CreateDpTrigger() {
                                        if (logging) log("Reaching CreateDpTrigger");

                                        for (let i in device) {
                                            if (logging) log("Setting DataPointTrigger #" + i + " for " + device[i].model);
                                        
                                            for (let x in device[i].definition.common) { //Alle properties der Devicedefinition durchgehen
                                                if (device[i].definition.common[x].write) {
                                                    //  log(praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name)
                                                    on({ id: praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name, change: "ne", ack: false }, function (dp) { //Bei Statusänderung
                                                        if (logging) log("Triggered i=" + i + " x=" + x + " ack=" + dp.state.ack);
                                                        SetDevice(i, x, CheckDataTypeAndConvert(dp.state.val, device[i].definition.common[x].type));
                                                    });
                                                    if (logging) log("Setting Datapoint Trigger for " + praefix0 + "." + device[i].id + "." + device[i].definition.common[x].name + " / read=" + device[i].definition.common[x].read + " write=" + device[i].definition.common[x].write);
                                                } else {
                                                    if (logging) log("No Datapoint Trigger set for " + device[i].definition.common[x].name + " because its readonly.");
                                                };
                                            };
                                        };
                                        

                                        }

                                        //***************** U T I L S *********************** */
                                        function CheckDataTypeAndConvert(value, HasToBe) {
                                        if (typeof value == "string" && HasToBe == "number") {
                                        if (parseFloat(value) == parseInt(value)) { //Umgewandelter String ist Int
                                        return parseInt(value);
                                        } else {
                                        return parseFloat(value);
                                        };
                                        } else {
                                        return value;
                                        };
                                        }

                                        function CorrectChannelId(cid) { //Erzeugt eine iobroker taugliche channelid. Punkte werden zu unterstrichen gewandelt, Doppelpunkte zu Punkten
                                        let temp = cid;
                                        if (typeof temp == "object") {
                                        temp = JSON.stringify(cid);
                                        };
                                        cid = temp.replace(".", '_'); //Erstmal alle potentiellen Punkte im channelnamen entfernen
                                        temp = cid.replace(/:/g, "."); //Jetzt alle Doppelpunkte durch Punkte ersetzen
                                        return temp;
                                        }

                                        function ConvertDeziToHex(DeziValue) {
                                        DeziValue = toInt(DeziValue)
                                        //log(typeof DeziValue)
                                        if (typeof DeziValue == "number") {
                                        return "#" + DeziValue.toString(16)
                                        };
                                        }

                                        function ConvertHexToDezi(HexValue) {

                                        if (typeof HexValue == "string") {
                                            if (HexValue.indexOf('#') != -1) {
                                                HexValue = HexValue.substr(1).toLowerCase()
                                            } else {
                                                HexValue = HexValue.toLowerCase()
                                            };
                                        };
                                        
                                        var i, j, digits = [0], carry;
                                        for (i = 0; i < HexValue.length; i += 1) {
                                            carry = parseInt(HexValue.charAt(i), 16);
                                            for (j = 0; j < digits.length; j += 1) {
                                                digits[j] = digits[j] * 16 + carry;
                                                carry = digits[j] / 10 | 0;
                                                digits[j] %= 10;
                                            }
                                            while (carry > 0) {
                                                digits.push(carry % 10);
                                                carry = carry / 10 | 0;
                                            }
                                        }
                                        return digits.reverse().join('');
                                        

                                        }

                                        /*

                                        • Converts an RGB color value to HSL. Conversion formula

                                        • adapted from http://en.wikipedia.org/wiki/HSL_color_space.

                                        • Assumes r, g, and b are contained in the set [0, 255] and

                                        • returns h, s, and l in the set [0, 1].

                                        • @param {number} r The red color value

                                        • @param {number} g The green color value

                                        • @param {number} b The blue color value

                                        • @return {Array} The HSL representation
                                          */
                                          function rgbToHsl(hex) {
                                          var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);

                                          var r = parseInt(result[1], 16);
                                          var g = parseInt(result[2], 16);
                                          var b = parseInt(result[3], 16);

                                          r /= 255, g /= 255, b /= 255;
                                          var max = Math.max(r, g, b),
                                          min = Math.min(r, g, b);
                                          var h, s, l = (max + min) / 2;

                                          if (max == min) {
                                          h = s = 0; // achromatic
                                          } else {
                                          var d = max - min;
                                          s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
                                          switch (max) {
                                          case r:
                                          h = (g - b) / d + (g < b ? 6 : 0);
                                          break;
                                          case g:
                                          h = (b - r) / d + 2;
                                          break;
                                          case b:
                                          h = (r - g) / d + 4;
                                          break;
                                          }
                                          h /= 6;
                                          }

                                          return [Math.round(h * 360), Math.round(s * 100)];
                                          }

                                        L Offline
                                        L Offline
                                        LassMichIchMachDasSelberKaputt
                                        schrieb am zuletzt editiert von
                                        #366

                                        @haselchen

                                        Ich werde es die Tage versuchen, danke dir für deine Hilfe!

                                        L 1 Antwort Letzte Antwort
                                        0
                                        • L LassMichIchMachDasSelberKaputt

                                          @haselchen

                                          Ich werde es die Tage versuchen, danke dir für deine Hilfe!

                                          L Offline
                                          L Offline
                                          LassMichIchMachDasSelberKaputt
                                          schrieb am zuletzt editiert von
                                          #367

                                          Ich habe jetzt mal VNC installiert und gesehen dass die Datei welche ich per wget kopiert habe nicht den richtigen Inhalt hatte. Das habe ich geändert. Nun läuft das Script weiter aber produziert einen neuen Fehler:

                                          11.4.2022, 17:41:30.005	[info ]: javascript.0 (385244) Start javascript script.js.Xiaomi.MiHomeScript
                                          11.4.2022, 17:41:30.010	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Starting AllMyMi V.0.2.29
                                          11.4.2022, 17:41:30.011	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: registered 0 subscriptions and 0 schedules
                                          11.4.2022, 17:41:30.012	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: You are already logged in, login canceled
                                          11.4.2022, 17:41:30.012	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Retrieving your in de registered MiHome Devices
                                          11.4.2022, 17:41:30.152	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Found 5 MiHome Devices, those are:
                                          11.4.2022, 17:41:30.152	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Videokamera
                                          11.4.2022, 17:41:30.153	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Mi Air Purifier 3/3H
                                          11.4.2022, 17:41:30.153	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Eve
                                          11.4.2022, 17:41:30.153	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Ventilator Schlafzimmer
                                          11.4.2022, 17:41:30.153	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Mi Electric Scooter Pro 2 series
                                          11.4.2022, 17:41:30.154	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Now searching for supported Devices...
                                          11.4.2022, 17:41:30.154	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Device Mi Air Purifier 3/3H is supported, creating DataPoints if necessary
                                          11.4.2022, 17:41:30.154	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Device Ventilator Schlafzimmer is supported, creating DataPoints if necessary
                                          11.4.2022, 17:41:30.169	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Now creating device for zhimi.airpurifier.mb3 / 359561707 / 192.168.3.56 / f0c2418e128fce7315ed211901198273 / 10000
                                          11.4.2022, 17:41:30.170	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Created device {"_events": GEKÜRZT
                                          11.4.2022, 17:41:30.757	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Init Device# 0 - device="zhimi.airpurifier.mb3"
                                          11.4.2022, 17:41:30.757	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Now creating device for dmaker.fan.p18 / GEKÜRZT
                                          11.4.2022, 17:41:30.758	[info ]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: Created device {"_events": GEKÜRZT
                                          11.4.2022, 17:41:30.832	[error]: javascript.0 (385244) script.js.Xiaomi.MiHomeScript: TypeError: Cannot read property 'forEach' of undefined
                                          11.4.2022, 17:41:30.833	[error]: javascript.0 (385244)     at CreateDevices (script.js.Xiaomi.MiHomeScript:924:17)
                                          11.4.2022, 17:41:30.834	[error]: javascript.0 (385244)     at main (script.js.Xiaomi.MiHomeScript:842:5)
                                          
                                          P 1 Antwort Letzte Antwort
                                          0
                                          Antworten
                                          • In einem neuen Thema antworten
                                          Anmelden zum Antworten
                                          • Älteste zuerst
                                          • Neuste zuerst
                                          • Meiste Stimmen


                                          Support us

                                          ioBroker
                                          Community Adapters
                                          Donate

                                          531

                                          Online

                                          32.5k

                                          Benutzer

                                          81.7k

                                          Themen

                                          1.3m

                                          Beiträge
                                          Community
                                          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
                                          ioBroker Community 2014-2025
                                          logo
                                          • Anmelden

                                          • Du hast noch kein Konto? Registrieren

                                          • Anmelden oder registrieren, um zu suchen
                                          • Erster Beitrag
                                            Letzter Beitrag
                                          0
                                          • Home
                                          • Aktuell
                                          • Tags
                                          • Ungelesen 0
                                          • Kategorien
                                          • Unreplied
                                          • Beliebt
                                          • GitHub
                                          • Docu
                                          • Hilfe