Announcement

Collapse
No announcement yet.

Learning Node-Red - What do you do with it?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Eman
    replied
    Originally posted by John245 View Post

    Your Dutch is not that bad
    ---
    John
    I cheated!!

    I keep telling people about Windows 10 Translator but no one takes seriously!



    Eman

    Leave a comment:


  • John245
    replied
    Originally posted by Eman View Post

    I think he meant

    Code:
     context.data = context.data || {
    "alarm": null,
    "dark": null,
    "sleep": null,
    "holiday": null,
    "evening lighting": null,
    }
    
    var sleepNetActive = false;
    
    switch (msg.topic)
    {
    case "alarm":
    context.data.alarm = msg.payload;
    break;
    case "dark":
    context.data.dark = msg.payload;
    break;
    case "sleep":
    if (context.data.sleep=== false && msg.payload)
    sleepingNetActive = true;
    context.data.sleep = msg.payload;
    break;
    case "holiday":
    context.data.holiday = msg.payload;
    break;
    case "evening lighting" means:
    context.data.evening lighting = msg.payload;
    break;
    }
    
    if (context.data.alarm !== null &&
    context.data.dark !== null &&
    context.data.sleep !== null &&
    context.data.holiday !== null)
    {
    var status = "";
    
    if (sleepingNetActive)
    {
    status = "Going to bed";
    }
    else if (context.data.holiday && context.data.dark && context.data.alarm && context.data.evening lighting)
    {
    status = "Holiday - lights on";
    }
    else if (context.data.holiday && context.data.dark && context.data.alarm && !context.data.evening lighting)
    {
    status = "Holiday - lights out";
    }
    else if (!context.data.dark && !context.data.alarm)
    {
    status = "Present - lights off (daytime)";
    }
    else if (!context.data.dark && context.data.alarm)
    {
    status = "Absent lights off (daytime)";
    }
    else if (context.data.dark && context.data.alarm && context.data.sleep)
    {
    status = "Sleep - lights out";
    }
    else if (!context.data.evening lighting && context.data.dark && context.data.alarm)
    {
    status = "Night - lights out";
    }
    else if (context.data.dark && !context.data.alarm && !context.data.sleep)
    {
    status = "Present - lights on";
    }
    else if (context.data.dark && context.data.alarm && context.data.evening lighting && !context.data.)
    {
    status = "Absent - lights on";
    }
    node.status({fill:"green",shape:"dot",text: status});
    
    return { "payload": status };
    }
    else
    {
    node.status({fill:"red",shape:"ring",text: "Undefined"});
    }
    return null;



    How's is that in English?


    Eman
    Your Dutch is not that bad
    ---
    John

    Leave a comment:


  • Eman
    replied
    Originally posted by John245 View Post

    My Dutch is very good 😄

    —-
    John
    I think he meant

    Code:
     context.data = context.data || {
    "alarm": null,
    "dark": null,
    "sleep": null,
    "holiday": null,
    "evening lighting": null,
    }
    
    var sleepNetActive = false;
    
    switch (msg.topic)
    {
    case "alarm":
    context.data.alarm = msg.payload;
    break;
    case "dark":
    context.data.dark = msg.payload;
    break;
    case "sleep":
    if (context.data.sleep=== false && msg.payload)
    sleepingNetActive = true;
    context.data.sleep = msg.payload;
    break;
    case "holiday":
    context.data.holiday = msg.payload;
    break;
    case "evening lighting" means:
    context.data.evening lighting = msg.payload;
    break;
    }
    
    if (context.data.alarm !== null &&
    context.data.dark !== null &&
    context.data.sleep !== null &&
    context.data.holiday !== null)
    {
    var status = "";
    
    if (sleepingNetActive)
    {
    status = "Going to bed";
    }
    else if (context.data.holiday && context.data.dark && context.data.alarm && context.data.evening lighting)
    {
    status = "Holiday - lights on";
    }
    else if (context.data.holiday && context.data.dark && context.data.alarm && !context.data.evening lighting)
    {
    status = "Holiday - lights out";
    }
    else if (!context.data.dark && !context.data.alarm)
    {
    status = "Present - lights off (daytime)";
    }
    else if (!context.data.dark && context.data.alarm)
    {
    status = "Absent lights off (daytime)";
    }
    else if (context.data.dark && context.data.alarm && context.data.sleep)
    {
    status = "Sleep - lights out";
    }
    else if (!context.data.evening lighting && context.data.dark && context.data.alarm)
    {
    status = "Night - lights out";
    }
    else if (context.data.dark && !context.data.alarm && !context.data.sleep)
    {
    status = "Present - lights on";
    }
    else if (context.data.dark && context.data.alarm && context.data.evening lighting && !context.data.)
    {
    status = "Absent - lights on";
    }
    node.status({fill:"green",shape:"dot",text: status});
    
    return { "payload": status };
    }
    else
    {
    node.status({fill:"red",shape:"ring",text: "Undefined"});
    }
    return null;



    How's is that in English?


    Eman

    Leave a comment:


  • kriz83
    replied
    Originally posted by kideon View Post
    Very elegant! I did the same thing in HomeSeer using virtual state devices and then events with group conditions. It trimmed down the events by a lot!
    Thanks ! I always struggled with groups. In NodeRed I just put them on seperate flows. I also find it more easily to follow what happens:
    • I can see the current value of input
    • I add state information to my custom function nodes
    • By adding debug nodes
    Most of my flows run on HA, but it is necessary to log these things so you know what is happening on your system ...

    Leave a comment:


  • kriz83
    replied
    Originally posted by wpiman View Post

    The script could just take arguments to the state of the inputs and then it could be called from a separate test script; but to your point sometimes debugging HS scripts is a bi***h. The script has to be architected to support it.

    Are you able to share/publish Node Red functionality like this?
    I typically do it like this:

    Click image for larger version  Name:	image_92618.png Views:	4 Size:	16.0 KB ID:	1412777
    And then I have a python script that sends MQTT messages and captures the output on a given topic.
    You can create multiple testcases, or just put everything in a big list like so:

    Code:
    table = [
    // Input 1       Input 2         Input 3       result
    [ "On",           "Off",            5,               "All lights on"],
    ["Off",            "On",            2,               "Vacation mode"],
    ...
    ]

    Leave a comment:


  • kideon
    replied
    Originally posted by kriz83 View Post

    Click image for larger version Name:	 Views:	0 Size:	202.3 KB ID:	1412682

    I attached the statemachine for my lights.
    It has 5 inputs. When triggered, the outcome of the statemachine depends on:
    - The state of the other inputs
    - The current state

    In HS Events, this would be multiple events (or 1 big events with OR's and AND's):
    - 5 triggers
    - at least 4 conditions per trigger

    So it is quite easy to make a mistake if you need to add an extra condition/trigger. Using NR makes this very easy, and I am assured that only 1 lighting profile can be active at any time. Furthermore, I have my statemachine under test.
    Very elegant! I did the same thing in HomeSeer using virtual state devices and then events with group conditions. It trimmed down the events by a lot!

    Leave a comment:


  • wpiman
    replied
    Originally posted by kriz83 View Post
    I only have to hookup inject nodes (or websocket, or mqtt, depending on how I feel) and I inject values into the statemachine while capturing the output.
    I'd like to see the unittest of your script
    The script could just take arguments to the state of the inputs and then it could be called from a separate test script; but to your point sometimes debugging HS scripts is a bi***h. The script has to be architected to support it.

    Are you able to share/publish Node Red functionality like this?

    Leave a comment:


  • kriz83
    replied
    Originally posted by John245 View Post
    Can this not be done by a switch Node?
    Sure, but your flow will explode because of the dependencies between states :-)

    Leave a comment:


  • kriz83
    replied
    Originally posted by wpiman View Post
    In Homeseer, I could have an event that would fire if any of the 5 things change, then call a script there with essentially the same code passing the reference to the 5 devices. I could unit test that.
    I only have to hookup inject nodes (or websocket, or mqtt, depending on how I feel) and I inject values into the statemachine while capturing the output.
    I'd like to see the unittest of your script

    Leave a comment:


  • John245
    replied
    Originally posted by kriz83 View Post

    It's in dutch, but you should be able to get the idea
    Code:
    context.data = context.data || {
    "alarm": null,
    "donker": null,
    "slapen": null,
    "vakantie": null,
    "avondverlichting": null,
    }
    
    var slapenNetActief = false;
    
    switch (msg.topic)
    {
    case "alarm":
    context.data.alarm = msg.payload;
    break;
    case "donker":
    context.data.donker = msg.payload;
    break;
    case "slapen":
    if (context.data.slapen === false && msg.payload)
    slapenNetActief = true;
    context.data.slapen = msg.payload;
    break;
    case "vakantie":
    context.data.vakantie = msg.payload;
    break;
    case "avondverlichting":
    context.data.avondverlichting = msg.payload;
    break;
    }
    
    if (context.data.alarm !== null &&
    context.data.donker !== null &&
    context.data.slapen !== null &&
    context.data.vakantie !== null)
    {
    var status = "";
    
    if (slapenNetActief)
    {
    status = "Naar bed gaan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && context.data.avondverlichting)
    {
    status = "Vakantie - lichten aan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && !context.data.avondverlichting)
    {
    status = "Vakantie - lichten uit";
    }
    else if (!context.data.donker && !context.data.alarm)
    {
    status = "Aanwezig - lichten uit (overdag)";
    }
    else if (!context.data.donker && context.data.alarm)
    {
    status = "Afwezig - lichten uit (overdag)";
    }
    else if (context.data.donker && context.data.alarm && context.data.slapen)
    {
    status = "Slapen - lichten uit";
    }
    else if (!context.data.avondverlichting && context.data.donker && context.data.alarm)
    {
    status = "Nacht - lichten uit";
    }
    else if (context.data.donker && !context.data.alarm && !context.data.slapen)
    {
    status = "Aanwezig - lichten aan";
    }
    else if (context.data.donker && context.data.alarm && context.data.avondverlichting && !context.data.slapen)
    {
    status = "Afwezig - lichten aan";
    }
    node.status({fill:"green",shape:"dot",text: status});
    
    return { "payload": status };
    }
    else
    {
    node.status({fill:"red",shape:"ring",text: "Undefined"});
    }
    return null;
    Can this not be done by a switch Node?

    ---
    John

    Leave a comment:


  • wpiman
    replied
    Originally posted by kriz83 View Post
    That should be G14 classified info ! ;-)

    Off course, I moved the complexity into the javascript code now. But this code can be unit tested. That is not possible with HS events. I can make changes to the script while making sure I did not break anything.
    I am not a huge fan of the HS event creator. I've been an advocate for the State machine flow for a years.

    That said, I am not sure how this is any better. You are writing a script to do the state machine in Java script. In Homeseer, I could have an event that would fire if any of the 5 things change, then call a script there with essentially the same code passing the reference to the 5 devices. I could unit test that. The state would be a virtual device (I'd like to see something like a HS counter be able to be set so that could be used as a SM- or a global string. Global variables go away when HS rebooted).


    Leave a comment:


  • kriz83
    replied
    Originally posted by Tillsy View Post
    My daughters are good at double dutch...
    That should be G14 classified info ! ;-)

    Off course, I moved the complexity into the javascript code now. But this code can be unit tested. That is not possible with HS events. I can make changes to the script while making sure I did not break anything.



    Leave a comment:


  • Tillsy
    replied
    My daughters are good at double dutch...

    Leave a comment:


  • John245
    replied
    Originally posted by kriz83 View Post

    It's in dutch, but you should be able to get the idea
    Code:
    context.data = context.data || {
    "alarm": null,
    "donker": null,
    "slapen": null,
    "vakantie": null,
    "avondverlichting": null,
    }
    
    var slapenNetActief = false;
    
    switch (msg.topic)
    {
    case "alarm":
    context.data.alarm = msg.payload;
    break;
    case "donker":
    context.data.donker = msg.payload;
    break;
    case "slapen":
    if (context.data.slapen === false && msg.payload)
    slapenNetActief = true;
    context.data.slapen = msg.payload;
    break;
    case "vakantie":
    context.data.vakantie = msg.payload;
    break;
    case "avondverlichting":
    context.data.avondverlichting = msg.payload;
    break;
    }
    
    if (context.data.alarm !== null &&
    context.data.donker !== null &&
    context.data.slapen !== null &&
    context.data.vakantie !== null)
    {
    var status = "";
    
    if (slapenNetActief)
    {
    status = "Naar bed gaan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && context.data.avondverlichting)
    {
    status = "Vakantie - lichten aan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && !context.data.avondverlichting)
    {
    status = "Vakantie - lichten uit";
    }
    else if (!context.data.donker && !context.data.alarm)
    {
    status = "Aanwezig - lichten uit (overdag)";
    }
    else if (!context.data.donker && context.data.alarm)
    {
    status = "Afwezig - lichten uit (overdag)";
    }
    else if (context.data.donker && context.data.alarm && context.data.slapen)
    {
    status = "Slapen - lichten uit";
    }
    else if (!context.data.avondverlichting && context.data.donker && context.data.alarm)
    {
    status = "Nacht - lichten uit";
    }
    else if (context.data.donker && !context.data.alarm && !context.data.slapen)
    {
    status = "Aanwezig - lichten aan";
    }
    else if (context.data.donker && context.data.alarm && context.data.avondverlichting && !context.data.slapen)
    {
    status = "Afwezig - lichten aan";
    }
    node.status({fill:"green",shape:"dot",text: status});
    
    return { "payload": status };
    }
    else
    {
    node.status({fill:"red",shape:"ring",text: "Undefined"});
    }
    return null;
    My Dutch is very good 😄

    —-
    John

    Leave a comment:


  • kriz83
    replied
    Originally posted by koburg View Post
    I do daily Struct programming(plc controllers) but have not used node-red so much until now.
    can you send/show the code used in the statemachine function.
    It's in dutch, but you should be able to get the idea
    Code:
    context.data = context.data || {
    "alarm": null,
    "donker": null,
    "slapen": null,
    "vakantie": null,
    "avondverlichting": null,
    }
    
    var slapenNetActief = false;
    
    switch (msg.topic)
    {
    case "alarm":
    context.data.alarm = msg.payload;
    break;
    case "donker":
    context.data.donker = msg.payload;
    break;
    case "slapen":
    if (context.data.slapen === false && msg.payload)
    slapenNetActief = true;
    context.data.slapen = msg.payload;
    break;
    case "vakantie":
    context.data.vakantie = msg.payload;
    break;
    case "avondverlichting":
    context.data.avondverlichting = msg.payload;
    break;
    }
    
    if (context.data.alarm !== null &&
    context.data.donker !== null &&
    context.data.slapen !== null &&
    context.data.vakantie !== null)
    {
    var status = "";
    
    if (slapenNetActief)
    {
    status = "Naar bed gaan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && context.data.avondverlichting)
    {
    status = "Vakantie - lichten aan";
    }
    else if (context.data.vakantie && context.data.donker && context.data.alarm && !context.data.avondverlichting)
    {
    status = "Vakantie - lichten uit";
    }
    else if (!context.data.donker && !context.data.alarm)
    {
    status = "Aanwezig - lichten uit (overdag)";
    }
    else if (!context.data.donker && context.data.alarm)
    {
    status = "Afwezig - lichten uit (overdag)";
    }
    else if (context.data.donker && context.data.alarm && context.data.slapen)
    {
    status = "Slapen - lichten uit";
    }
    else if (!context.data.avondverlichting && context.data.donker && context.data.alarm)
    {
    status = "Nacht - lichten uit";
    }
    else if (context.data.donker && !context.data.alarm && !context.data.slapen)
    {
    status = "Aanwezig - lichten aan";
    }
    else if (context.data.donker && context.data.alarm && context.data.avondverlichting && !context.data.slapen)
    {
    status = "Afwezig - lichten aan";
    }
    node.status({fill:"green",shape:"dot",text: status});
    
    return { "payload": status };
    }
    else
    {
    node.status({fill:"red",shape:"ring",text: "Undefined"});
    }
    return null;

    Leave a comment:

Working...
X