Create a switch in Nodered reading status from URL

Hi all

I am almost there but not fully; I try to make a switch in Node-Red to use in HA to control my house
s existing system.

My current system works like this:
Requesting device data is done by url http:// address/xyz/devicedata #which gives status info of all my controllable devices( in total 250 entities that I have to transfer separately to HA)
Request measurements and other info is done equally

Sending state change request is done with url
http://address/xyz/devices?opmode=setpin&uid=1771159508035&mask=128&values=128

and those values are different for each device in my system( the long string is the number of the control unit controlling relay outputs for 24 devices per module, the mask and value is different for all devices in a module).

Now, I am able to create a status element to HA from my server( measurement data entities were easy to create and device status equally) but I have to poll the info from the server with multiple url’s and that will overflow the server as it can handle only few requests simultaneously. Thus I collect a major list and extract the info to each element/ entity separately.

I am able to make a switch, which changes the state from on to off and vice versa. I am even able to have that switch in HA but that switch doesn poll my house system’s status. So, if I change the lights on, by said switch the house knows them to be on and HA knows them to be on and same with off via HA. But, if I turn the wall switch( a button), then HA doesn’t have the right status shown on the switch but I have to change the status twice to be certain the status shown by the switch is correct.

That said, how can I make a switch in Node-Red that it extracts the status data from my existing system every other second( timestamp is set to either second or two) but also acts as a switch?.

My current code looks like this:

It’s overcomplicated but essentially does what I want the switch to do:

  1. Device values are polled every second and formatted as XML and function 11 formulates mayloads of the read data in format msg.device_tvroom_rooflamp_state = msg.payload.response.unit[0]…$.value. Function 12 extracts only the value of tvroom_rooflamp
  2. switch creates an entuity called tvroom_rooflamp
  3. in case of value being 1, a http://address/xyz/device?opmode=setpin-for-on is sent
    and for value being 0 another url is sent.

All debugs give the right outcomes even if I switch the light on from my exisiting system or HA but the swithc status doesn’t update.

Should I do separate entities of the status and the switch or is there means to combine these?

On an another note, is there means to have enities created from the phase 1) polling directly with one node ( as I already have 250 data payloads made on function 11 -node) or is the only way to create them to create them separately( as I have done for 90 measurement data entities already)

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();

Thank you for your response, will take a look on provided material and try( this also has been a struggle as most examples seem to be a slight miss compared to what I think I’m looking for, emphasis on the think-part).

I know this must have been asked more than once before but just can’t find the right search( inside this community or externally) to be conducted.
Still newbie on Node-Red and HA with last touch to create sw 25 years ago so finding the right stuff is a bit of hit-n-miss. Also just found out NR has a row-limit per flow-tab, which required me to re-think how to produce the said 250 device-entities and their states with one polling( to which I hope to find some resolution)