# Working with state changes

**URL:** <https://discourse.nodered.org/t/working-with-state-changes/4114>\
**Category:** Core Development\
**Created:** [19 October 2018 09:38 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114 "2018-10-19T09:38:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [19 October 2018 09:38 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/1 "2018-10-19T09:38:22Z")

</div>

Hey there I have MODBUS Input module

> **[Digital input module switch module Isolated 16 DI RS485 MODBUS WP8026ADAM |...](https://www.ebay.com/itm/Digital-input-module-switch-module-Isolated-16-DI-RS485-MODBUS-WP8026ADAM/272379730255?hash=item3f6b18d94f:g:BNAAAOSw6oBXE6n2:rk:3:pf:0)**
>
> Digital input module, Switch module 16CH isolated DI RS485 MODBUS communication Digital module(A4A2). 3, communication with input and status indicator. 1, Digital input channels 16 channels (active low). | eBay!

  
which is working in modbus.  
for some reason it is not working according to data sheet and instead of having one register per Input i get a decimal number two's complimentary i would like to do automation to detect the input status.

number is 16 bits  
i found something which should do the work  
node-red-contrib-binary  
not sure if it will support the signed number however ,  
is there any solution to manipulate the msg value ( i currently don't know how to get it in function node).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 October 2018 10:13 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/2 "2018-10-19T10:13:40Z")

</div>

Feed the message into a debug node and copy/paste here what you see, then explain again what you want to convert it to.

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [20 October 2018 13:22 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/3 "2018-10-20T13:22:24Z")

</div>

10/20/2018, 4:19:09 PM[node: 513283da.a8848c](http://192.168.10.201:1880/#)sensor.dio : msg.payload : string[1]

"9"

this is an example the number 9 indicate for two active inputs

the LSB and bit number 3  
so 2^0+2^3= 9...

for the automation i would like to get state for each input.  
basically i also could not find the reason why i cannot read the register directly and i only get one cumulative results.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9dd26b4e8a5100cc459e490db7c3496d308b49e1.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 October 2018 14:49 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/4 "2018-10-20T14:49:16Z")

</div>

> [@poratnir](#):
>
> 10/20/2018, 4:19:09 PM[node: 513283da.a8848c](http://192.168.10.201:1880/#)sensor.dio : msg.payload : string[1]
> 
> "9"

What node type is that string coming from?

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [20 October 2018 17:42 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/5 "2018-10-20T17:42:19Z")

</div>

Event status

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 October 2018 20:58 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/6 "2018-10-20T20:58:32Z")

</div>

> [@poratnir](#):
>
> Event status

I don't understand. I assumed it was coming from a comms node of some sort. How can a Status node read modbus? If that is what you meant by Event status.

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [21 October 2018 20:55 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/7 "2018-10-21T20:55:57Z")

</div>

Hey it is very simple ..  
i get a string which is represent by decimal number which if i convert it into 16 bit array  
each bit will indicate for the input status..

i would like to do an automation which will read each bit separately.... and will send MQTT message...

1. how can i convert the payload into number ?
2. how to put the bits data into an array? 16 bits...

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 October 2018 21:26 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/8 "2018-10-21T21:26:19Z")

</div>

OK, if you don't want to tell us which node you are using in the hope we might make your life easier then that is up to you.

To convert a string to a number you can do, in a function node

```auto
var number = Number(msg.payload);

```

Then to access, for example, bit 2 (0 being the least significant bit) you can use

```auto
var bit2 = (number >> 2)&1;

```

so you could load up an array using something like that in a loop.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 October 2018 04:33 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/9 "2018-10-22T04:33:22Z")

</div>

When I want to get an array of bits from a number, I build a string of 16 1's and 0's, split it into single characters, map those to numbers, and reverse the array, like so:

```auto
var bits = ('0000000000000000' + Number("9").toString(2)).slice(-16).split('').map(d => +d).reverse()

```

which returns this array:  
`[1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]`

Now you have a 16 element array, with the index # equal to each bit's position -- so to check bit 2, you would use the JS syntax `bits[2]`. There is probably a cleaner way than fiddling with all the 0 padding and slicing, but filling buffer arrays is not as intuitive for me...

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [22 October 2018 06:22 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/10 "2018-10-22T06:22:44Z")

</div>

Hey Thanks!

I am using function node

```auto
var bit2 = (number >> 2)&1; means rotate right ? 2 times and does And operand with 1?

```

second option  
var bits = ('0000000000000000' + Number("9").toString(2)).slice(-16).split('').map(d =\> +d).reverse()

means add array of 16 bits of zero ,

what does slice -16 will do ? i assume map and reverse will convert the array to be in the right direction ?

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [22 October 2018 06:26 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/11 "2018-10-22T06:26:09Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f6126149f0e7b167182e4da26cd471cd29c92bfa.png)

I am using simple flow..  
dio is the sensor ,  
i could not see any output to this....  
this is the new function i have created..  
var number = Number(msg.payload);  
var bit2 = (number \>\> 2)&1;  
msg=bit2;  
return msg;

when i tired to change return variable to number  
return number;

i get the following:  
Function tried to send a message of type number  
on debug window

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 October 2018 06:49 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/12 "2018-10-22T06:49:35Z")

</div>

> [@poratnir](#):
>
> Function tried to send a message of type number  
> on debug window

Which means your Function node is returning a number, not a message object.

Change your function to do:

```auto
msg.payload = bit2;

```

---

<div class="post-metadata">

**Author:** ![poratnir](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@poratnir](https://discourse.nodered.org/u/poratnir)\
**Post date:** [22 October 2018 07:54 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/13 "2018-10-22T07:54:26Z")

</div>

Thanks so can you elaborate the command  
var bit2 = (number \>\> 2)&1;

does it means rotate right ? 2 bits?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2018 09:42 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/14 "2018-10-22T09:42:46Z")

</div>

> [@poratnir](#):
>
> does it means rotate right ? 2 bits?

No, shift right not rotate right.  
The w3schools site is excellent for information on javascript (and lots of other stuff too). The operators can be seen at

> **[JavaScript Operators Reference](https://www.w3schools.com/jsref/jsref_operators.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [22 October 2018 18:51 UTC](https://discourse.nodered.org/t/working-with-state-changes/4114/15 "2018-10-22T18:51:42Z")

</div>

> [@poratnir](#):
>
> what does slice -16 will do ? i assume map and reverse will convert the array to be in the right direction ?

The `slice(-n)` function returns the last `n` characters of a string -- and `Array.reverse()`, well it reverses an array ;\*)

BUT, I have since found a more "natural" way to create an array of bit flags from any numeric value (either of datatype integer or string):

```auto
// create an empty array (of `N` bit flags)
var bits = new Array(16).fill(0);
// "AND" each element and "shift" to get its bit value
msg.payload = bits.map((d,i) => (msg.payload & 2**i) >> i);
// return the original msg with the new payload array
return msg;

```
