Skip to content
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • 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

  • Default (No Skin)
  • No Skin
Collapse
Logo
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. Object

NEWS

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

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    1.8k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    15
    1
    2.0k

Object

Scheduled Pinned Locked Moved Skripten / Logik
8 Posts 3 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tefi
    wrote on last edited by
    #1

    Hallo zusammen

    Ich habe folgenden kleine Test geschrieben und ein Object definiert. Darin enthalten ist die Eigenschaft Name.

    In der Function im on Bereich möchte ich auf diese Eigenschaft zugreifen, erhalte aber ein undefined. Wie muss ich das schreiben, dass es klappt.

    function test (obj) {
        this.Name = obj.name;
        log(this.Name);
    
        // ----- motions --------------------------------
        $('state(this.Name+'.motions")[state.id=*.active]').on(function(obj) {
            log(this.Name);
            log(obj.Name);
        })
    }
    
    1 Reply Last reply
    0
    • AlCalzoneA Offline
      AlCalzoneA Offline
      AlCalzone
      Developer
      wrote on last edited by
      #2

      Variante 1 (modernes JavaScript) => Verwende arrow-functions als Callback statt dem klassischen function-Konstrukt:

      Vorher:

      $('state(this.Name+'.motions")[state.id=*.active]').on(function(obj) {
      	// Der Wert von this hängt davon ab, was der Aufrufer des Callbacks als Kontext wählt
              log(this.Name);
              log(obj.Name);
      })
      

      Nachher:

      $('state(this.Name+'.motions")[state.id=*.active]').on((obj) => {
      	// Der Wert von this entspricht dem in der umgebenden Funktion
              log(this.Name);
              log(obj.Name);
      })
      

      Variante 2 => merke dir den Wert von this in einer Hilfsvariablen:

      function test (obj) {
          const that = this; // that ist Hilfsvariable
          this.Name = obj.name; 
          log(this.Name);
      
          // ----- motions --------------------------------
          $('state(this.Name+'.motions")[state.id=*.active]').on(function(obj) {
              log(that.Name); // <= !!!
              log(obj.Name);
          })
      }
      

      Warum `sudo` böse ist: https://forum.iobroker.net/post/17109

      1 Reply Last reply
      0
      • AlCalzoneA Offline
        AlCalzoneA Offline
        AlCalzone
        Developer
        wrote on last edited by
        #3

        Für Details warum und wieso kannst du zB hier weiterlesen:

        https://developer.mozilla.org/de/docs/W … funktionen

        Warum `sudo` böse ist: https://forum.iobroker.net/post/17109

        1 Reply Last reply
        0
        • T Offline
          T Offline
          Tefi
          wrote on last edited by
          #4

          Besten Dank, das hat top geklappt.

          Ich hätte da noch eine Frage. In einem Object habe ich folgenden Eventhandler laufen, welcher aber interessanterweise durch alle Datenpunkt sämlicher Adapter durch geht. Was ist hier falsch?

           $({id: 'javascript.0.roomcontroller.küche.Scene', change:'any', fromNe:'system.adapter.javascript.0'}).on((obj) => {
              log(obj.state.from+'----->');
           })
          
          1 Reply Last reply
          0
          • AlCalzoneA Offline
            AlCalzoneA Offline
            AlCalzone
            Developer
            wrote on last edited by
            #5

            @Tefi:

             $({id: 'javascript.0.roomcontroller.küche.Scene', change:'any', fromNe:'system.adapter.javascript.0'}).on((obj) => {
                log(obj.state.from+'----->');
             })
            ```` `  
            

            Du vermixt die Syntax der $- und on-Funktion:

            on({id: 'javascript.0.roomcontroller.küche.Scene', change:'any', fromNe:'system.adapter.javascript.0'}, (obj) => {
                log(obj.state.from+'----->');
            })
            

            Warum `sudo` böse ist: https://forum.iobroker.net/post/17109

            1 Reply Last reply
            0
            • T Offline
              T Offline
              Tefi
              wrote on last edited by
              #6

              Besten Dank, klappt

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tefi
                wrote on last edited by
                #7

                Bei einem ENUM möchte ich nur die Objekte "obj.state.val=true".

                Lässt sich das nun schon im subscribe angeben?

                $('state(functions=Bewegung)[state.id=*.active]').on( (obj) => {
                } );
                
                1 Reply Last reply
                0
                • paul53P Offline
                  paul53P Offline
                  paul53
                  wrote on last edited by
                  #8

                  @Tefi:

                  Bei einem ENUM möchte ich nur die Objekte "obj.state.val=true".

                  Lässt sich das nun schon im subscribe angeben? `
                  Das dürfte bei einem https://github.com/ioBroker/ioBroker.javascript/blob/master/doc/en/javascript.md#–-selector nicht möglich sein. Da bleibt nur die Abfrage if(obj.state.val).

                  Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
                  Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  Support us

                  ioBroker
                  Community Adapters
                  Donate

                  603

                  Online

                  32.4k

                  Users

                  81.4k

                  Topics

                  1.3m

                  Posts
                  Community
                  Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                  ioBroker Community 2014-2025
                  logo
                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Recent
                  • Tags
                  • Unread 0
                  • Categories
                  • Unreplied
                  • Popular
                  • GitHub
                  • Docu
                  • Hilfe