Previous state, current state of 48 digital inputs made whit pcf8574


After a lot of reading coding trying I did get some where
But I still need your expertise.
Whit the above I have 48 digital inputs
made of 6,PCF8574 whit interrupt, so NR gets a signal on pin 11 and knows now to read the PCF's
works like a charm

(me very happy now)

Now i'm struggling whit the code,

My goal is a object,array or a string, that holds the state of each input.

I think I found it, will work on some new code brb

The working code

The setup, hardware, software etc. And the why
  • Rpi 3
  • openhabian
  • pcf8574 breakout board
  • Currently I have in my home a system of ISYGLT (domotica) it's a wired system, old bad software, hard to get new modules. So my plan has always been to replace it with generic hardware that you can get almost anywhere. After a lot of reading I found that the community is more about wireless than wired. The inputs are done, I guess that the outputs (on/off) will pose me not the challenges that i got from the inputs, still there needs a lot to be done hardware wise dimming rgb but that's for another time I'll keep you updated.

Thanks to the forums and google I got it working

I am not sure what is happening, but all I see towards the bottom are two blurry images.

The "works like a charm" and "Now I'm struggling with the code".

Doesn't help if they can't be seen.

Ah click mouse on it, new code will be there soon i hope

I see 2 images as well oops maybe I am not allowed to post pics in my first post.


Ah. Weird. Never seen that happen before.

Oh, Arduino code.

Ok. I'm not good with Node-Red. Not much better with Arduino.


Sorry, it’s late and I’m on my phone but take a closer look at your conditional construction. Your if/else isn’t structured correctly resulting in the code that is indented to run always as it is outside of the “then” part of the construction. If I’ll remember I’ll post a more elaborate answer in the morning. I can do Dutch explanations too if preferred.

Furthermore if your context getting if fails, it will set a value instead, and with the code always executing it then saves it. Try to work it out on paper like “the old days” and see what values your code holds where. Debugging by hand is always my preferred method here :slight_smile:
Sorry, another edit: what I mean is that the conditions in your inner if/else attempt are always true so just fixing the construction won’t solve your issue.

You are missing an { after else

var Pin=context.get('Pin');
if (Pin===undefined){   // this check cos var Pin=context.get('Pin') || 0; fails for me
    Pin = context.get('Pin');
    var _obj = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
    context.set("Pin", _obj);
var obj = context.get("Pin");
var arr= obj.split(",");//Array.from(obj);
       for (var i = 0; i < arr.length; i++) {
          arr[i] = parseInt(arr[i]);

var _byte = msg.payload ^ (1 << 8) - 1; //inputs are High, so flip bit
var pcf8574 = (msg.address-32); //The addresses of the PCF in dec, so we have 0 for the first 1 for...etc.
var state, bit, pinNr;

for (i =0;i<8;i++) { 
    bit = ((_byte & (1 << i)) !== 0) ? 1 : 0; // set bit to 1, if bit !== 0 
    pinNr = (pcf8574*8)+i; //pinNr 0 to 47
        if (arr[pinNr] === bit )
        } else 
            msg.payload = pinNr + 1 + "," + bit; //pinNr 1 to 48, High or Low
            arr[pinNr] = bit;

context.set("Pin", (arr.toString()));

return ;

is the bom, solved all my issues.
In context,flow or global you can set a array, and it will show correctly in the debug window,
But a call back will fail, at least i could't I got Undefined[0]

I needed a system to store the state of 48 inputPins
var Pin=context.get('Pin') || 0;
was failing for me as well, the if (Pin===undefined){ check solved that.

I am sure there are cleaner and better solutions, but for me a Frankenstein coder this is well done I am happy
But still here, need to learn so please don't hold back and shoot at it.

Did you see my comment ?

Yeah I did, but that code was not working as i would like to the output wasn't correct (clean enough)

For who is interested.

The Flow

[{"id":"6ed11da6.caf884","type":"debug","z":"87abf50b.a415a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":710,"y":460,"wires":[]},{"id":"7131b02f.f4b62","type":"rpi-gpio in","z":"87abf50b.a415a8","name":"","pin":"11","intype":"up","debounce":"1","read":true,"x":140,"y":400,"wires":[["19231a0e.6e4db6"]]},{"id":"19231a0e.6e4db6","type":"switch","z":"87abf50b.a415a8","name":"Send onliy low","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"}],"checkall":"false","repair":false,"outputs":1,"x":300,"y":400,"wires":[["a29d7d95.7d29f"]]},{"id":"a29d7d95.7d29f","type":"i2c scan","z":"87abf50b.a415a8","name":"","x":120,"y":460,"wires":[[],["593b15c5.b1243c"]]},{"id":"593b15c5.b1243c","type":"i2c in","z":"87abf50b.a415a8","name":"","address":"","command":"255","count":"1","x":270,"y":460,"wires":[["74ae86fd.7508c8"]]},{"id":"f7b67c59.4433b","type":"rbe","z":"87abf50b.a415a8","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":415,"y":460,"wires":[["a06687a0.254868"]],"l":false},{"id":"74ae86fd.7508c8","type":"change","z":"87abf50b.a415a8","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"address","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":355,"y":460,"wires":[["f7b67c59.4433b"]],"l":false},{"id":"a06687a0.254868","type":"change","z":"87abf50b.a415a8","name":"","rules":[{"t":"delete","p":"topic","pt":"msg"},{"t":"delete","p":"command","pt":"msg"},{"t":"delete","p":"size","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":475,"y":460,"wires":[["9398d728.f73838"]],"l":false},{"id":"9398d728.f73838","type":"function","z":"87abf50b.a415a8","name":"","func":"var Pin=context.get('Pin');\nif (Pin===undefined){ // this check cos var Pin=context.get('Pin') || 0; fails for me\n Pin = context.get('Pin');\n var _obj = \"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\";\n context.set(\"Pin\", _obj);\n}\nvar obj = context.get(\"Pin\");\nvar arr= obj.split(\",\");//Array.from(obj);\n for (var i = 0; i < arr.length; i++) {\n arr[i] = parseInt(arr[i]);\n }\n\nvar _byte = msg.payload ^ (1 << 8) - 1; //inputs are High, so flip bit\nvar pcf8574 = (msg.address-32); //The addresses of the PCF in dec, so we have 0 for the first 1 for...etc.\nvar state, bit, pinNr;\n\nfor (i =0;i<8;i++) { \n bit = ((_byte & (1 << i)) !== 0) ? 1 : 0; // set bit to 1, if bit !== 0 \n pinNr = (pcf8574*8)+i; //pinNr 0 to 47\n if (arr[pinNr] === bit )\n {\n continue;\n } else \n msg.payload = pinNr + 1 + \",\" + bit; //pinNr 1 to 48, High or Low\n arr[pinNr] = bit;\n node.send(msg);\n } \n\ncontext.set(\"Pin\", (arr.toString()));\n\nreturn ;\n","outputs":1,"noerr":0,"x":535,"y":460,"wires":[["c5d878c3.d20d88"]],"l":false},{"id":"c5d878c3.d20d88","type":"change","z":"87abf50b.a415a8","name":"","rules":[{"t":"delete","p":"address","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":595,"y":460,"wires":[["6ed11da6.caf884"]],"l":false}]