# Switch debounce - switching animation

**URL:** <https://discourse.nodered.org/t/switch-debounce-switching-animation/52710>\
**Category:** General\
**Created:** [25 October 2021 12:52 UTC](https://discourse.nodered.org/t/switch-debounce-switching-animation/52710 "2021-10-25T12:52:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bubuq3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bubuq3/32/48671_2.png) [@bubuq3](https://discourse.nodered.org/u/bubuq3)\
**Post date:** [25 October 2021 12:52 UTC](https://discourse.nodered.org/t/switch-debounce-switching-animation/52710/1 "2021-10-25T12:52:20Z")

</div>

Hello I got the problem like on the video: switches animation ON-OFF-ON or OFF-ON-OFF in HA interface. is there i can do it about it? the video shows the problem. I used debounce node but didnt help ;/

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4da0e174429bb016fb713a45650e107c50eef6f5.png)

got this function: it receives feedback from a relay board e.g. 0000 turn off 0001 turn on relay and animates the entity node in HA interface

```auto
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 really is on then sets the relayall to on
// if not to off
relays[8] = { enable: relays.every((r) => r.enable) };
return relays;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/542ab32ced0334af04398e8d304aaabf1c8c5957.png)

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2a17848055133f4eb74d1e15dc8e1f7a868670b8.jpeg "ha24-10") ](https://www.youtube.com/watch?v=g8y8mq_TC0I)

my loop () function in esp8266:

```auto
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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![bubuq3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bubuq3/32/48671_2.png) [@bubuq3](https://discourse.nodered.org/u/bubuq3)\
**Post date:** [26 October 2021 15:41 UTC](https://discourse.nodered.org/t/switch-debounce-switching-animation/52710/2 "2021-10-26T15:41:00Z")

</div>

Tried like so, hasn't helped ;/

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 December 2021 15:41 UTC](https://discourse.nodered.org/t/switch-debounce-switching-animation/52710/3 "2021-12-25T15:41:41Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
