zachowj / node-red-contrib-home-assistant-websocket

Node-RED integration with Home Assistant
https://zachowj.github.io/node-red-contrib-home-assistant-websocket/
MIT License
488 stars 88 forks source link

`wait-until` node crashes on certain inputs #1127

Closed zhimsel closed 10 months ago

zhimsel commented 10 months ago

Describe the bug

After upgrading to 0.57.4, I started encountering the following error:

24 Oct 11:31:37 - [error] TypeError: Cannot read properties of null (reading 'last_changed')
    at WaitUntil._WaitUntil_onEntityChange (/data/node_modules/node-red-contrib-home-assistant-websocket/dist/nodes/wait-until/WaitUntilController.js:151:47)
    at runNextTicks (node:internal/process/task_queues:61:5)
    at processImmediate (node:internal/timers:437:9)

To Reproduce

Unsure. I'm currently narrowing down what nodes specifically (if any) are causing the error. So far, if I disable all my wait-until nodes, NodeRED starts up fine.

I've narrowed it down to one particular node (or set of nodes). I have a state node for a device tracker that is set to output on connect which is wired to a wait-until node.

Whenever the WIFI_DEVICE_TRACKER entity's state becomes not_home (either on startup or due to an actual state change), the crash happens. If I manually update the entity's state to home (in the developer tools), and restart the flow, the crash does not happen. If I change the state from home -> not_home (with the flow running), the crash happens.

Please not that it appears to only be the wait-until node receiving the message which causes the crash. Having the wait-until node disabled during these state changes/startup does not cause the crash to happen.

Expected behavior

No response

Screenshots

No response

Example Flow

[
    {
        "id": "e263cf46e7beb180",
        "type": "ha-wait-until",
        "z": "c687a37cf5d53e50",
        "name": "App reports the same",
        "server": "2e7ea548.981d4a",
        "version": 2,
        "outputs": 2,
        "entityId": "device_tracker.HA_APP_TRACKER",
        "entityIdFilterType": "exact",
        "property": "state",
        "comparator": "is_not",
        "value": "home",
        "valueType": "str",
        "timeout": "55",
        "timeoutType": "num",
        "timeoutUnits": "seconds",
        "checkCurrentState": true,
        "blockInputOverrides": true,
        "outputProperties": [
            {
                "property": "notify",
                "propertyType": "msg",
                "value": "{\"message\": \"command_update_sensors\"}",
                "valueType": "jsonata"
            }
        ],
        "entityLocation": "data",
        "entityLocationType": "none",
        "x": 1360,
        "y": 1210,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "8bbb57a3c9812c58",
        "type": "function",
        "z": "c687a37cf5d53e50",
        "name": "reset",
        "func": "return {\"reset\": true};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1160,
        "y": 1250,
        "wires": [
            [
                "e263cf46e7beb180"
            ]
        ]
    },
    {
        "id": "408520826e1ad93b",
        "type": "server-state-changed",
        "z": "c687a37cf5d53e50",
        "name": "WIFI_DEVICE_TRACKER",
        "server": "2e7ea548.981d4a",
        "version": 5,
        "outputs": 2,
        "exposeAsEntityConfig": "",
        "entityId": "device_tracker.WIFI_DEVICE_TRACKER",
        "entityIdType": "exact",
        "outputInitially": true,
        "stateType": "str",
        "ifState": "home",
        "ifStateType": "str",
        "ifStateOperator": "is_not",
        "outputOnlyOnStateChange": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": true,
        "ignoreCurrentStateUnavailable": true,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            },
            {
                "property": "friendly_name",
                "propertyType": "msg",
                "value": "REDACTED",
                "valueType": "str"
            }
        ],
        "x": 980,
        "y": 1220,
        "wires": [
            [
                "e263cf46e7beb180"
            ],
            [
                "8bbb57a3c9812c58"
            ]
        ],
        "outputLabels": [
            "not home",
            "home"
        ]
    },
    {
        "id": "2e7ea548.981d4a",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open|playing|idle|paused",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": true,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "id",
        "entitySelector": "id",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "h23",
        "statusTimeFormat": "h:m:s",
        "enableGlobalContextStore": true
    }
]

Environment Information

Version: 0.57.4

Home Assistant version: 2023.10.5 Companion version: 3.0.1

Node-RED version: 3.1.0 Docker: yes Add-on: no

Node.js version: 16.20.2 OS: Linux 6.1.34-1-lts x64 LE

Additional context

These are the messages that are going into the wait-until node during each state change (as captured by a debug node):

not_home -> home

{"reset":true,"_msgid":"d29dd7a9e011548d"}

home -> not_home

{
  "payload": "not_home",
  "data": {
    "entity_id": "device_tracker.WIFI_DEVICE_TRACKER",
    "old_state": {
      "entity_id": "device_tracker.WIFI_DEVICE_TRACKER",
      "state": "home",
      "attributes": {
        "source_type": "router",
        "mac": "REDACTED",
        "host_name": "REDACTED",
        "oui": "REDACTED",
        "icon": "mdi:cellphone-wireless",
        "friendly_name": "REDACTED"
      },
      "last_changed": "2023-10-24T16:17:38.179850+00:00",
      "last_updated": "2023-10-24T16:17:38.179850+00:00",
      "context": {
        "id": "REDACTED",
        "parent_id": null,
        "user_id": "REDACTED"
      }
    },
    "new_state": {
      "entity_id": "device_tracker.WIFI_DEVICE_TRACKER",
      "state": "not_home",
      "attributes": {
        "source_type": "router",
        "mac": "REDACTED",
        "host_name": "REDACTED",
        "oui": "REDACTED",
        "icon": "mdi:cellphone-wireless",
        "friendly_name": "REDACTED"
      },
      "last_changed": "2023-10-24T16:17:41.423402+00:00",
      "last_updated": "2023-10-24T16:17:41.423402+00:00",
      "context": {
        "id": "REDACTED",
        "parent_id": null,
        "user_id": "REDACTED"
      },
      "timeSinceChangedMs": 11
    }
  },
  "topic": "device_tracker.WIFI_DEVICE_TRACKER",
  "friendly_name": "REDACTED",
  "_msgid": "b89752292e50678f"
}
zachowj commented 10 months ago

Can you check if it crashes if you uncheck "Check against current state" in the wait-until node?