Animating buttons in home assistant depend on json data

Hello, I’d like to animate switches because I need feedback cause it is MODBUS ;/
image

got function node:

let m = msg.payload

let relay1status = m.substr(0,4);
let relay2status = m.substr(4,4);
let relay3status = m.substr(8,4);
let relay4status = m.substr(12,4);
let relay5status = m.substr(16,4);
let relay6status = m.substr(20,4);
let relay7status = m.substr(24,4);
let relay8status = m.substr(28,4);

return {
    payload:
    {
        relay1:relay1status,
        relay2:relay2status,
        relay3:relay3status,
        relay4:relay4status,
        relay5:relay5status,
        relay6:relay6status,
        relay7:relay7status,
        relay8:relay8status,
        
    }
    
    }

it returns object:
object
relay1: “0001”
relay2: “0000”
relay3: “0001”
relay4: “0000”
relay5: “0001”
relay6: “0000”
relay7: “0000”
relay8: “0000”

// “0001” is on, 0000 is off

what I got to change in payload section in entity node (HA node):

edit: got to do in entity node → payload section:
if (relay1status == “0000”) {
// friendlyname == Relay1
// animate Relay1 to off position
} else {
// animate Relay1 to on position - 0001
}

edit2: entity node → payload:

{
"if": { "relay1status": "0000" },
    "then": { "Relay1": "off" },
    "else": {
        "if": { "relay1status": "0001" },
         "then": { "Relay1": "on" }
    }
}


??? it doesn’t work ;/

[{"id":"3910f388ec3bada2","type":"function","z":"ffbd7f06.4a014","name":"","func":"const relays = [];\n\nfor(let i = 0;i < 8;i++) {\n    relays[i] = {\n        enable: !!Number(msg.payload.substr(i * 4, 4))\n    }\n}\n\nreturn relays;","outputs":8,"noerr":0,"initialize":"","finalize":"","libs":[],"x":284,"y":1632,"wires":[["a43a62d7d39cc187"],["9b549ffffd735144"],["1611aedc737322fc"],["f4e27578d393a8db"],["a1ba711f9939e345"],["aee0f79ac62ed38f"],["814e4c774a9a8752"],["62cee85c217c2426"]]},{"id":"2eac18bae0479504","type":"inject","z":"ffbd7f06.4a014","name":"evens on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00000001000000010000000100000001","payloadType":"str","x":124,"y":1584,"wires":[["3910f388ec3bada2"]]},{"id":"a43a62d7d39cc187","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay1","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay1"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1584,"wires":[["1d71e3db592478cd"],["1d71e3db592478cd"]]},{"id":"9b549ffffd735144","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay2","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay2"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1632,"wires":[["a3a91e0cf64f5430"],["a3a91e0cf64f5430"]]},{"id":"1611aedc737322fc","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay3","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay3"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1680,"wires":[["8ae82d25e1ae28bf"],["8ae82d25e1ae28bf"]]},{"id":"f4e27578d393a8db","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay4","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay4"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1728,"wires":[["a5a7b6569c443b8c"],["a5a7b6569c443b8c"]]},{"id":"a1ba711f9939e345","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay5","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay5"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1776,"wires":[["6ff2b7c45b55a028"],["6ff2b7c45b55a028"]]},{"id":"aee0f79ac62ed38f","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay6","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay6"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1824,"wires":[["61accdf31f240ed4"],["61accdf31f240ed4"]]},{"id":"814e4c774a9a8752","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay7","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay7"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1872,"wires":[["c2958026b048c166"],["c2958026b048c166"]]},{"id":"62cee85c217c2426","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay8","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay8"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":450,"y":1920,"wires":[["dea5b7753392b861"],["dea5b7753392b861"]]},{"id":"1d71e3db592478cd","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":634,"y":1584,"wires":[]},{"id":"a3a91e0cf64f5430","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1632,"wires":[]},{"id":"8ae82d25e1ae28bf","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1680,"wires":[]},{"id":"a5a7b6569c443b8c","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1728,"wires":[]},{"id":"6ff2b7c45b55a028","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1776,"wires":[]},{"id":"61accdf31f240ed4","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1824,"wires":[]},{"id":"c2958026b048c166","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1872,"wires":[]},{"id":"dea5b7753392b861","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":636,"y":1920,"wires":[]},{"id":"ef6b16033ca4ef57","type":"inject","z":"ffbd7f06.4a014","name":"odds on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010000000100000001000000010000","payloadType":"str","x":114,"y":1632,"wires":[["3910f388ec3bada2"]]},{"id":"cb166f0d426f3fd5","type":"inject","z":"ffbd7f06.4a014","name":"first and last","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010000000000000000000000000001","payloadType":"str","x":134,"y":1680,"wires":[["3910f388ec3bada2"]]},{"id":"86a50e0742ef4ee9","type":"inject","z":"ffbd7f06.4a014","name":"all off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00000000000000000000000000000000","payloadType":"str","x":114,"y":1728,"wires":[["3910f388ec3bada2"]]},{"id":"015ab5d4be74214c","type":"inject","z":"ffbd7f06.4a014","name":"all on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010001000100010001000100010001","payloadType":"str","x":114,"y":1776,"wires":[["3910f388ec3bada2"]]}]
1 Like

Thank for your reply. I managed to do it with your help!!! <3
@Kermit what is good way to delay this animation in function node? Because If I turn on the switch animates to on and after 1s second to off and then to on. I tried to delay it with delay node but hasn;t helped. I guess it changes the position of the switch because it reads the previous message of the mqtt in node I think I got to do changes inside the function?


let m = msg.payload
const relays = [];

     for(let i = 0;i < 8;i++) {
    relays[i] = {
        enable: !!Number(m.substr(i * 4, 4))
                }
    }

return relays;


Demo from the first link
http://reactivex.io/documentation/operators/debounce.html

Think that’s what you are after.

1 Like

If I send the mqtt meassage in 4 seconds each, I got to do 5 seconds debounce?


Still when I mess with debounce nodes it doesn’t work I supposed to ;/
attach debounce to mqtt in node and the function and tried from function out to entity nodes (switches)

And what If I wanna turn on the all relays switch (yellow color if it is on)?: what a function I got to do?
image
image

 if (msg.payload == "00000000000000000000000000000000") {
     

   // enable all relays switch - OFF - BLUE COLOR
     
 } else {
         // enable all relays switch - ON - YELLOW COLOR
 }


I don’t really understand the previous function code:
especially this line:

 enable: !!Number(msg.payload.substr(i * 4, 4))

The debounce was intended for the on/off flicker. So 1 second should be enough. So it will pass the first on msg, and then block any other msg. So it won’t flick on off on. So only showing the initial on status.

got it like and 1000 ms is set and it still anmiates from on-off-on when I want to turn on

Hmm actually I think that debounce won’t help. It’s the switch in HA that needs the delay.

The relay isn’t actually switching on/off/on is it? It didn’t sound like it in the video.
Just the animation in HA. I’ve only done it with a Reed switch updating the state of a different switch. Not the same switch. I think yours the switch is not getting the status update quick enough.
So it sends the on, then returns to off, then gets the status update to on…

The delay needs to be in HA fronted.

They didn’t physically change but only the position in HA interface, when I turn it on it physically is ON, but the animation in ha interface is ON-OFF-ON, or the opposite of it, when I want to turn it off it goes OFF-ON-OFF.
sometimes it got lagged that it doesn;t change the position (if I set 2500ms debounce time or more), but mqtt messages come every 2200ms Or happened that I changeg the debounce to 3000 and a few relays after deploy in node-red were already ON, and after time pass it doesn’t change the position to off (physically relays were off)

as you said it got to be HA interface problem?
//////////
or if I got 1000 ms debounce time then again it is like turn on-off-on

even if I receive the message from esp to my node-red flow every 5 seconds and got 1000 debounce there are laggs: want to turn on switch (animation in HA: ON-OFF-ON)
so it is something with HA or the function ;/

const relays = [];

// loops through the 8 relays and creates a msg property 
// object to send to the entity node to turn it on/off
for(let i = 0;i < 8;i++) {
    relays[i] = {
        // msg.payload.substr(i * 4, 4) same as you had original to parse out the string
        // Number() converts the string '0001' or '0000' to a number 1 or 0
        // !! convert the number to a boolean - true or false        
        enable: !!Number(msg.payload.substr(i * 4, 4))
    }
}
// check if any relay is on then sets the relayall to on
// if not to off
relays[8] = { enable: relays.some((r) => r.enable) };
return relays;

[{"id":"3910f388ec3bada2","type":"function","z":"ffbd7f06.4a014","name":"","func":"const relays = [];\n\n// loops through the 8 relays and creates a msg property \n// object to send to the entity node to turn it on/off\nfor(let i = 0;i < 8;i++) {\n    relays[i] = {\n        // msg.payload.substr(i * 4, 4) same as you had original to parse out the string\n        // Number() converts the string '0001' or '0000' to a number 1 or 0\n        // !! convert the number to a boolean - true or false        \n        enable: !!Number(msg.payload.substr(i * 4, 4))\n    }\n}\n// check if any really is on then sets the relayall to on\n// if not to off\nrelays[8] = { enable: relays.some((r) => r.enable) };\nreturn relays;","outputs":9,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":1440,"wires":[["a43a62d7d39cc187"],["9b549ffffd735144"],["1611aedc737322fc"],["f4e27578d393a8db"],["a1ba711f9939e345"],["aee0f79ac62ed38f"],["814e4c774a9a8752"],["62cee85c217c2426"],["706350d61da23944"]]},{"id":"2eac18bae0479504","type":"inject","z":"ffbd7f06.4a014","name":"evens on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00000001000000010000000100000001","payloadType":"str","x":140,"y":1392,"wires":[["3910f388ec3bada2"]]},{"id":"a43a62d7d39cc187","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay1","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay1"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1392,"wires":[["1d71e3db592478cd"],["1d71e3db592478cd"]]},{"id":"9b549ffffd735144","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay2","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay2"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1440,"wires":[["a3a91e0cf64f5430"],["a3a91e0cf64f5430"]]},{"id":"1611aedc737322fc","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay3","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay3"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1488,"wires":[["8ae82d25e1ae28bf"],["8ae82d25e1ae28bf"]]},{"id":"f4e27578d393a8db","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay4","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay4"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1536,"wires":[["a5a7b6569c443b8c"],["a5a7b6569c443b8c"]]},{"id":"a1ba711f9939e345","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay5","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay5"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1584,"wires":[["6ff2b7c45b55a028"],["6ff2b7c45b55a028"]]},{"id":"aee0f79ac62ed38f","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay6","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay6"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1632,"wires":[["61accdf31f240ed4"],["61accdf31f240ed4"]]},{"id":"814e4c774a9a8752","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay7","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay7"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1680,"wires":[["c2958026b048c166"],["c2958026b048c166"]]},{"id":"62cee85c217c2426","type":"ha-entity","z":"ffbd7f06.4a014","name":"relay8","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relay8"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":466,"y":1728,"wires":[["dea5b7753392b861"],["dea5b7753392b861"]]},{"id":"1d71e3db592478cd","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":1392,"wires":[]},{"id":"a3a91e0cf64f5430","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1440,"wires":[]},{"id":"8ae82d25e1ae28bf","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1488,"wires":[]},{"id":"a5a7b6569c443b8c","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1536,"wires":[]},{"id":"6ff2b7c45b55a028","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1584,"wires":[]},{"id":"61accdf31f240ed4","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1632,"wires":[]},{"id":"c2958026b048c166","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1680,"wires":[]},{"id":"dea5b7753392b861","type":"debug","z":"ffbd7f06.4a014","name":"mqtt node","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":652,"y":1728,"wires":[]},{"id":"ef6b16033ca4ef57","type":"inject","z":"ffbd7f06.4a014","name":"odds on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010000000100000001000000010000","payloadType":"str","x":130,"y":1440,"wires":[["3910f388ec3bada2"]]},{"id":"cb166f0d426f3fd5","type":"inject","z":"ffbd7f06.4a014","name":"first and last","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010000000000000000000000000001","payloadType":"str","x":150,"y":1488,"wires":[["3910f388ec3bada2"]]},{"id":"86a50e0742ef4ee9","type":"inject","z":"ffbd7f06.4a014","name":"all off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00000000000000000000000000000000","payloadType":"str","x":130,"y":1536,"wires":[["3910f388ec3bada2"]]},{"id":"015ab5d4be74214c","type":"inject","z":"ffbd7f06.4a014","name":"all on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"00010001000100010001000100010001","payloadType":"str","x":130,"y":1584,"wires":[["3910f388ec3bada2"]]},{"id":"706350d61da23944","type":"ha-entity","z":"ffbd7f06.4a014","name":"relayall","server":"","version":1,"debugenabled":false,"outputs":2,"entityType":"switch","config":[{"property":"name","value":"relayall"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":true,"outputPayload":"$entity().state ? 1 : 0","outputPayloadType":"jsonata","x":476,"y":1776,"wires":[["1d71e3db592478cd","a3a91e0cf64f5430","8ae82d25e1ae28bf","a5a7b6569c443b8c","6ff2b7c45b55a028","61accdf31f240ed4","c2958026b048c166","dea5b7753392b861"],["1d71e3db592478cd","a3a91e0cf64f5430","8ae82d25e1ae28bf","a5a7b6569c443b8c","6ff2b7c45b55a028","61accdf31f240ed4","c2958026b048c166","dea5b7753392b861"]]}]
1 Like

Ok I change some() to every() to check all the elements in the array and it lights up the all relays icon as I wanted :),but still there is ON-OFF-ON or OFF-ON-OFF animation in HA interface. is there i can do it about it? the video shows the problem. Once again thank you :slight_smile:

my loop () function in esp8266:

void loop() {
  mb.task();
  yield();
  
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
// send msg with  relays' statuses
 long now = millis();
  if (now - lastMsg > 2600) {
    lastMsg = now;
  mb.readHreg(SLAVE_ID, 0x0001, status, 8, cbWrite);

char buf[256] = { 0 };
for (int i = 0; i < 8; i++) {
sprintf(buf + strlen(buf), "%04X", status[i]);
Serial.println(buf);
}  
     client.publish("device1/relaysChannelStatus", buf);
  }
}

Tried like so, hasn’t helped ;/

const relays = [];

// loops through the 8 relays and creates a msg property 
// object to send to the entity node to turn it on/off
setTimeout(getStatues, 5000);
function getStatues() {
for(let i = 0;i < 8;i++) {
    relays[i] = {
        // msg.payload.substr(i * 4, 4) same as you had original to parse out the string
        // Number() converts the string '0001' or '0000' to a number 1 or 0
        // !! convert the number to a boolean - true or false        
        enable: !!Number(msg.payload.substr(i * 4, 4))
        
    }
  }
}
getStatues();
// check if any really is on then sets the relayall to on
// if not to off
relays[8] = { enable: relays.every((r) => r.enable) };
return relays;