Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Alias Umwandlung bei JSON mit "null" bereitet Probleme

    NEWS

    • [erledigt] 15. 05. Wartungsarbeiten am ioBroker Forum

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Alias Umwandlung bei JSON mit "null" bereitet Probleme

    This topic has been deleted. Only users with topic management privileges can see it.
    • FredF
      FredF Most Active Forum Testing @Heinz53 last edited by

      @heinz53
      Ich antworte mal:

      "read": "val == 'on' ? true : false"
      
      1 Reply Last reply Reply Quote 0
      • paul53
        paul53 @Heinz53 last edited by

        @heinz53 sagte: folgende Meldung

        Wenn auch geschrieben werden soll, muss man beim Schreiben konvertieren, denn ich nehme an, dass "Strom28.set" ebenfalls "on"/"off" erwartet?

              "read": "val == 'on'",
              "write": "val ? 'on' : 'off'" 
        
        1 Reply Last reply Reply Quote 1
        • H
          Heinz53 last edited by

          Danke für eure Tipps. Leider kann ich sie gerade nicht testen weil mein ioBroker in einer Endlosschleife läuft.

          Ich habe ihn gestoppt, neu gestartet und den Container neu erstellt., ioBroker fix ausgeführt, ... Das Ergebnis bleibt gleich:

          This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
          
          TypeError: Cannot read properties of undefined (reading 'toString')
          
              at applyAliasConvenienceConversion (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/aliasProcessing.ts:78:30)
          
              at Object.formatAliasValue (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/tools.ts:2753:17)
          
              at change (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/src/lib/adapter/adapter.ts:10961:37)
          
              at Immediate.<anonymous> (file:///opt/iobroker/node_modules/@iobroker/db-states-redis/src/lib/states/statesInRedisClient.ts:383:37)
          
              at processImmediate (node:internal/timers:483:21)
          
          This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
          
          TypeError: Cannot read properties of undefined (reading 'toString')
          
              at applyAliasConvenienceConversion (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/aliasProcessing.ts:78:30)
          
              at Object.formatAliasValue (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/tools.ts:2753:17)
          
              at change (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/src/lib/adapter/adapter.ts:10961:37)
          
              at Immediate.<anonymous> (file:///opt/iobroker/node_modules/@iobroker/db-states-redis/src/lib/states/statesInRedisClient.ts:383:37)
          
              at processImmediate (node:internal/timers:483:21)
          
          This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
          
          TypeError: Cannot read properties of undefined (reading 'toString')
          
              at applyAliasConvenienceConversion (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/aliasProcessing.ts:78:30)
          
              at Object.formatAliasValue (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/tools.ts:2753:17)
          
              at change (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/src/lib/adapter/adapter.ts:10961:37)
          
              at Immediate.<anonymous> (file:///opt/iobroker/node_modules/@iobroker/db-states-redis/src/lib/states/statesInRedisClient.ts:383:37)
          
              at processImmediate (node:internal/timers:483:21)
          
          This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
          
          TypeError: Cannot read properties of undefined (reading 'toString')
          
              at applyAliasConvenienceConversion (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/aliasProcessing.ts:78:30)
          
              at Object.formatAliasValue (/opt/iobroker/node_modules/@iobroker/js-controller-common-db/src/lib/common/tools.ts:2753:17)
          
              at change (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/src/lib/adapter/adapter.ts:10961:37)
          
              at Immediate.<anonymous> (file:///opt/iobroker/node_modules/@iobroker/db-states-redis/src/lib/states/statesInRedisClient.ts:383:37)
          
              at processImmediate (node:internal/timers:483:21)
          

          Ich kann nicht über den ioBroker auf meine Datenpunkte zugreifen.
          Vermutlich habe ich noch ungültige alias Definitionen.

          Wie kann ich den Zugriff wieder herstellen?

          1 Reply Last reply Reply Quote 0
          • H
            Heinz53 last edited by

            Mein ursprüngliches Problem ist mittlerweile behoben. Der Zugriff auf den ioBroker war nach Start der Admin Instanz über SSH wieder möglich.

            iobroker start admin
            

            Als Ursache für meine Probleme vermute ich den "Schlafmodus" meiner Zigbee Button beim Versuch des öffnens eines "schlafenden" alias Datenpunktes in den Objekten.
            Dies führte bei mir reproduzierbar zu Problemen der Admin Instanz.
            Nach dem Setzen des Retain bei den Problematischen Geräten traten keine Abbrüche mehr auf.

            "retain": true
            

            @FredF und @paul53 : Ich habe noch Probleme meine Zigbee Steckdose (mit JSON) zu schalten.
            Bei der Anbindung eines direkten Datenpunktes funktioniert es:

              "common": {
                "name": "Test_1",
                "desc": "Manuell erzeugt",
                "role": "state",
                "type": "boolean",
                "read": true,
                "write": true,
                "def": false,
                "alias": {
                  "id": "0_userdata.0.Test.TestOnOff",
                  "read": "val == 'on' ? true : false",
                  "write": "val ? 'on' : 'off'"
                }
              },
            

            Bei der Steckdose habe ich noch Probleme. Ich erhalte die Fehlermeldung:
            Invalid write function for "alias.0.Steckdose.Strom28.state": "val ? JSON.stringify{'state' : 'on'} : JSON.stringify{'state' : 'off'}" => Unexpected token '{'

            Ich habe schon viele Varianten erfolglos durchprobiert.

              "common": {
                "name": "state",
                "desc": "Manuell erzeugt",
                "role": "state",
                "type": "boolean",
                "read": true,
                "write": true,
                "def": "",
                "alias": {
                  "id": {
                    "read": "mqtt.0.zigbee2mqtt.Strom28",
                    "write": "mqtt.0.zigbee2mqtt.Strom28.set"
                  },
                  "read": "JSON.parse(val).state == 'on' ? true : false",
                  "write": "val ? JSON.stringify{'state' : 'on'} : JSON.stringify{'state' : 'off'}"
                }
              },
            

            Wie geht es richtig?

            paul53 1 Reply Last reply Reply Quote 0
            • paul53
              paul53 @Heinz53 last edited by paul53

              @heinz53 sagte: Wie geht es richtig?

              Es fehlenden die runden Klammern.

                    "read": "JSON.parse(val).state == 'on'",
                    "write": "JSON.stringify({state: val ? 'on' : 'off'})"
              

              Die Rolle sollte "switch" sein. Außerdem bietet sich an, einen eindeutigen, verständlichen Namen zu vergeben.

              1 Reply Last reply Reply Quote 0
              • H
                Heinz53 last edited by

                @paul53 said in Alias Umwandlung bei JSON mit "null" bereitet Probleme:

                JSON.stringify({state: val ? 'on' : 'off'})

                @paul53 Nun erhalte ich folgende Fehlermeldung:
                Object alias.0.Steckdose.Strom28.state is invalid: Default value has to be type "boolean" but received type "string" This will throw an error up from js-controller version 7.0.0!

                Wenn die Steckdose ausgeschaltet ist wird sie über den alias eingeschaltet.
                Das Ausschalten über den alias ist nicht möglich.

                  "common": {
                    "name": "Steckdose_Strom28",
                    "desc": "Manuell erzeugt",
                    "role": "switch",
                    "type": "boolean",
                    "read": true,
                    "write": true,
                    "def": "",
                    "alias": {
                      "id": {
                        "read": "mqtt.0.zigbee2mqtt.Strom28",
                        "write": "mqtt.0.zigbee2mqtt.Strom28.set"
                      },
                      "read": "JSON.parse(val).state == 'on' ? true : false ",
                      "write": "JSON.stringify({state: val ? 'on' : 'off'})"
                    }
                  },
                

                Was kann ich machen?

                paul53 1 Reply Last reply Reply Quote 0
                • paul53
                  paul53 @Heinz53 last edited by paul53

                  @heinz53 sagte: Was kann ich machen?

                  Ändere "def":

                      "def": false,
                  

                  @heinz53 sagte in Alias Umwandlung bei JSON mit "null" bereitet Probleme:

                  Das Ausschalten über den alias ist nicht möglich.

                  ?? Wie sieht der Wert von "mqtt.0.zigbee2mqtt.Strom28" bei ausgeschalteter Steckdose aus?

                  1 Reply Last reply Reply Quote 0
                  • H
                    Heinz53 last edited by

                    Die mqtt Daten sind:

                      "common": {
                        "name": "Steckdose_Strom28",
                        "desc": "Manuell erzeugt",
                        "role": "switch",
                        "type": "boolean",
                        "read": true,
                        "write": true,
                        "def": false,
                        "alias": {
                          "id": {
                            "read": "mqtt.0.zigbee2mqtt.Strom28",
                            "write": "mqtt.0.zigbee2mqtt.Strom28.set"
                          },
                          "read": "JSON.parse(val).state == 'on'",
                          "write": "JSON.stringify({state: val ? 'on' : 'off'})"
                        }
                      },
                    

                    Nach der Änderung von "def" erhalte ich keine Fehlermeldung mehr im log.
                    Das Ausschalten ist weiterhin nicht möglich.

                      "common": {
                        "name": "Steckdose_Strom28",
                        "desc": "Manuell erzeugt",
                        "role": "switch",
                        "type": "boolean",
                        "read": true,
                        "write": true,
                        "def": false,
                        "alias": {
                          "id": {
                            "read": "mqtt.0.zigbee2mqtt.Strom28",
                            "write": "mqtt.0.zigbee2mqtt.Strom28.set"
                          },
                          "read": "JSON.parse(val).state == 'on'",
                          "write": "JSON.stringify({state: val ? 'on' : 'off'})"
                        }
                      },
                    
                    1 Reply Last reply Reply Quote 0
                    • H
                      Heinz53 last edited by

                      die mqtt Daten:
                      {
                      "child_lock": "UNLOCK",
                      "countdown": 0,
                      "current": 0,
                      "energy": 0.03,
                      "indicator_mode": "off/on",
                      "last_seen": "2025-01-08T20:10:18+01:00",
                      "linkquality": 138,
                      "power": 0,
                      "power_outage_memory": "restore",
                      "state": "OFF",
                      "update": {
                      "installed_version": 80,
                      "latest_version": 80,
                      "state": "idle"
                      },
                      "update_available": false,
                      "voltage": 225
                      }

                      paul53 1 Reply Last reply Reply Quote 0
                      • paul53
                        paul53 @Heinz53 last edited by

                        @heinz53 sagte: "state": "OFF",

                        ... groß geschrieben! Auch "ON"?

                        1 Reply Last reply Reply Quote 0
                        • H
                          Heinz53 last edited by

                          Danke! Das war's. Nun funktioniert es.

                            "common": {
                              "name": "Steckdose_Strom28",
                              "desc": "Manuell erzeugt",
                              "role": "switch",
                              "type": "boolean",
                              "read": true,
                              "write": true,
                              "def": false,
                              "alias": {
                                "id": {
                                  "read": "mqtt.0.zigbee2mqtt.Strom28",
                                  "write": "mqtt.0.zigbee2mqtt.Strom28.set"
                                },
                                "read": "JSON.parse(val).state == 'ON'",
                                "write": "JSON.stringify({state: val ? 'ON' : 'OFF'})"
                              }
                            },
                          

                          Es funktionieren beim direkten Schalten beide Schreibweisen (Groß oder Kleinschrift)

                          ... und wieder was gelernt ...
                          Danke für die Unterstützung!

                          C 1 Reply Last reply Reply Quote 0
                          • C
                            c1olli @Heinz53 last edited by

                            Ich habe ein wenigstens der Überschrift nach ähnliches Problem.

                            Ich bekomme die Daten aus der Victron VRM als json ins MQTT geliefert.
                            Diese wandele ich mit:

                            Math.round(JSON.parse(val).value * 10) / 10
                            

                            im Alias zu Werten um und das funktioniert auch recht gut.

                            Aber wenn die PV nichts mehr einspeist meldet der Datenpunkt :

                            {"value":null}
                            

                            Damit kommt dann der alias nicht klar und meldet folgenden Fehler:

                            Invalid read function for "alias.0.Vic-Erz": "Math.round(JSON.parse(val).value * 10) / 10" => Cannot read properties of null (reading 'value')
                            

                            Weil Null kein Wert ist.

                            So sieht der Datenpunkt dann aus:

                            {
                              "common": {
                                "name": "PV-Pow-Ges",
                                "desc": "Manuell erzeugt",
                                "role": "value",
                                "type": "number",
                                "read": true,
                                "write": true,
                                "def": 0,
                                "alias": {
                                  "read": "Math.round(JSON.parse(val).value * 100) / 100",
                                  "id": "mqtt.0.N.48e7da85dd79.pvinverter.22.Ac.Power"
                                },
                                "unit": "W"
                              },
                              "type": "state",
                              "native": {},
                              "_id": "alias.0.PV-Pow-Ges",
                              "acl": {
                                "object": 1636,
                                "state": 1636,
                                "owner": "system.user.admin",
                                "ownerGroup": "system.group.administrator"
                              },
                              "from": "system.adapter.admin.0",
                              "user": "system.user.admin",
                              "ts": 1736334728344
                            }
                            

                            Wie kann ich nun die Fehlermeldung verhindern?

                            paul53 1 Reply Last reply Reply Quote 0
                            • mickym
                              mickym Most Active last edited by

                              @c1olli sagte in Alias Umwandlung bei JSON mit "null" bereitet Probleme:

                              Math.round(JSON.parse(val).value * 10) / 10

                              Probier halt das Gleiche was vorgeschlagen wurde:

                              val ? Math.round(JSON.parse(val).value * 10) / 10 : val
                              
                              1 Reply Last reply Reply Quote 0
                              • paul53
                                paul53 @c1olli last edited by paul53

                                @c1olli sagte: {"value":null}

                                "JSON.parse(val).value ? Math.round(JSON.parse(val).value * 100) / 100 : 0",
                                

                                Das JSON wird noch so erzeugt? Es ist nicht der gesamte Wert null?

                                C 2 Replies Last reply Reply Quote 0
                                • C
                                  c1olli @paul53 last edited by c1olli

                                  @paul53
                                  ich habe die Variante von @mickym und auch die von Dir probiert (2 verschiedene Werte) und beides funktioniert erst einmal.
                                  Ob das mit Null funktioniert weiß ich nicht, da die PV erst heute abend auf "null" stellt.

                                  Ich versuche mal beides zu simulieren.

                                  Edit:
                                  In der Simulation hat beides funktioniert. Ich warte also mal ab was passiert.

                                  1 Reply Last reply Reply Quote 0
                                  • C
                                    c1olli @paul53 last edited by

                                    @paul53
                                    @mickym

                                    Ich hab die Varianten jetzt laufen lassen und die Variante von @mickym funktioniert.
                                    Bei der anderen Variante kommt dann wieder "null" und die Fehlermeldung,

                                    Danke für die Hilfe.

                                    1 Reply Last reply Reply Quote 1
                                    • First post
                                      Last post

                                    Support us

                                    ioBroker
                                    Community Adapters
                                    Donate

                                    664
                                    Online

                                    31.6k
                                    Users

                                    79.5k
                                    Topics

                                    1.3m
                                    Posts

                                    blockly javascript
                                    5
                                    19
                                    656
                                    Loading More Posts
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    Community
                                    Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                    The ioBroker Community 2014-2023
                                    logo