Create a switch in Nodered reading status from URL

Every time this comes up I came never find the latest post I had written. You can use the HA endpoints created by the NR custom component.

Really rough steps.

  1. Store all the received data
  2. On connection to HA send all the discovery messages of the stored data.
  3. When new data arrives send updated values, if the id is not in the discovery cache send discovery payload first

https://community.home-assistant.io/t/custom-component-for-node-red-contrib-home-assistant-websocket/150736/7?u=kermit

Here’s a function node I use to create sensors for my monthly internet usage. Which is sent to an API node.

[5,4,3,2,1,0].forEach((month, index) => {
    const current = new Date();
    current.setMonth(current.getUTCMonth()-(month+1));
    const name = current.toLocaleString('default', { month: 'long' });
    
    msg.payload = { data: {
        type: "nodered/discovery",
        server_id: "noderedserver",
        node_id: `xfinity_month_usage_${month}`,
        component: "sensor",
        config: {
            name
        }
    }};
    
    node.send(msg);    
    
        msg.payload = { data: {
        type: "nodered/entity",
        server_id: "noderedserver",
        node_id: `xfinity_month_usage_${month}`,
        state: msg.usageMonths[index].totalUsage,
    }};

    node.send(msg);
});

node.done();