node-red-contrib-home-assistant-websocket

repository·main·Indexed 20 days ago

https://github.com/zachowj/node-red-contrib-home-assistant-websocket

Node-RED integration for Home Assistant using WebSockets and REST API. Provides a suite of nodes for automation, including support for the Home Assistant Community add-on, actionable notifications for Android, history retrieval, and event filtering by label or area. Includes cookbook examples for expiration date monitoring and WLED holiday light scheduling.

Tokens
54.1K
Snippets
137
Records
328
Agent score
69%

What's inside node-red-contrib-home-assistant-websocket

  1. Use the Get Entities node to retrieve Home Assistant entities

    main

    The Get Entities node retrieves entities from Home Assistant based on specific search criteria. It is used for filtering entities that match certain conditions, such as finding all lights that are currently 'on' or sensors within a specific range.

    All specified search criteria must be met for an entity to be considered valid. You can configure the node via its UI or override settings using the msg.payload object.

  2. Configure the Action node Data object

    main

    The Data object for a Home Assistant service call can be defined in three ways:

    1. Directly in the Action node UI: Using the JSONata (J:) option in the 'Data' field.
    2. In a preceding Change node: By passing a msg.payload object containing the service call settings (domain, service, target, and data).
    3. In a flow trigger node: Such as an Events: state node, where the msg.payload is generated as output.

    If settings are provided in msg.payload (via a Change node), they take precedence over the settings configured in the Action node's UI.

  3. Use the Poll State node to monitor entities

    main

    The Poll State node outputs the state of a Home Assistant entity at regular intervals. It is designed for scenarios where you need to periodically check an entity's status to ensure automations remain synchronized with the latest information.

    Key capabilities include:

    • Periodic polling based on a defined interval.
    • Triggering on startup (via Output on connect).
    • Triggering immediately when an entity changes (via Output on change).
    • Conditional routing based on the entity's state (via If State).
  4. Use Home Assistant State Boolean type

    main

    The Home Assistant State Boolean type uses the boolean mappings defined in your server's State Boolean configuration. This is useful for checking if an entity matches any of your configured "true" values (e.g., checking if a light is on or a door is open).

    Default True values: y, yes, on, true, home, open (stored as an array).

    Important Notes:

    • Comparisons are case-sensitive.
    • While configured state boolean values are normalized to lowercase when deployed, entity state comparisons remain case-sensitive.
    • For user-controlled entities with capitalized states (like input_text), use a static list or flow/global context instead.
  5. Use the Trigger: state node for conditional automation

    main

    The Trigger: state node triggers when a specific entity's state changes. It is similar to the Events: state node but allows you to build complex conditional tests directly within the node's UI fields. This reduces the need for external conditional logic nodes (like Switch or Filter) for many common automation patterns.

    Key Difference: While it has extensive UI-based conditional testing, the JSONata implementation within the condition UI fields does not have access to the $entity() or $entities() functions. However, JSONata is available in the output properties UI fields, where it does have access to $entities().

  6. Use JSONata in the Current State node

    main

    The Current State node supports JSONata to transform outputs, perform conditional tests, and generate values for UI settings.

    Key JSONata functions available within the node:

    • $entity(): Returns the subject entity of the node. Use $entity().state for the state and $entity().attributes for the attributes object.
    • $entities('entity_id'): Returns the state of a different, specified entity (e.g., $entities('sensor.time').state).

    Note on Data Access: While the node's default output sets msg.data to the entity data object, inside a JSONata expression within the node, you should use $entity() to access the state and attributes directly.

  7. Access Home Assistant data via Global Context

    main

    By enabling Enable Global Context Store in the Server Config node, Home Assistant connection data, states, and services are exposed in the Node-RED global context.

    The namespace used in the global context is the Server Config's Name converted to camelCase.

    Available Context Keys

    KeyDescriptionUpdate Behavior
    statesAll entity statesAlways kept up to date as state changes occur
    servicesAvailable Home Assistant servicesLoaded on initial deploy
    eventsAvailable Home Assistant eventsLoaded on initial deploy

    Usage Example

    If your Server Config node is named Home Assistant and you want to check the state of switch.my_switch in a Function node:

    const haCtx = global.get("homeassistant");
    const configCtx = haCtx.homeAssistant;
    const entityState = configCtx.states["switch.my_switch"];
    return entityState.state === "on";
  8. Listen for WebSocket client events

    main

    You can use the Events: all node to monitor the status of the connection between Node-RED and Home Assistant. To do this, set the Event Type in the node configuration to home_assistant_client.

    This allows you to trigger logic based on the lifecycle of the WebSocket connection. Available client events include:

    • connecting: When the client is attempting to connect to HA.
    • connected: After authorization has been accepted.
    • disconnected: When the socket stops attempting to connect or disconnects after being connected.
    • error: When a WebSocket disconnect occurs with an error.
    • states_loaded: The first time all states are loaded from HA.
    • services_loaded: The first time all services are loaded from HA.
    • running: When HA is in a running state and states have been loaded.
    // Example configuration for the Events: all node to catch client events
    {
      "event_type": "home_assistant_client"
    }
  9. Use relative time strings in Get History

    main

    When using the Use Relative Time option, you can provide a duration string in the In the Last field (or msg.payload.relativeTime). The parser supports the following units:

    • Milliseconds: ms, milli, millisecond, milliseconds
    • Seconds: s, sec, secs, second, seconds
    • Minutes: m, min, mins, minute, minutes
    • Hours: h, hr, hrs, hour, hours
    • Days: d, day, days
    • Weeks: w, week, weeks
    • Months: mon, mth, mths, month, months
    • Years: y, yr, yrs, year, years

    Example: 4h 30m parses to the last 4 hours and 30 minutes.