Announcement

Collapse
No announcement yet.

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

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

    #16
    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

    Comment


      #17
      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

      Comment


        #18
        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 :-)

        Comment


          #19
          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?
          HS3 Pro Edition 3.0.0.435 (Windows Server 8.1 on ESXi box)

          Plug-Ins Enabled:
          Z-Wave:,RaspberryIO:,AirplaySpeak:,Ecobee:,
          weatherXML:,JowiHue:,APCUPSD:,PHLocation:,Chromecast:,EasyTr igger:

          Comment


            #20
            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!

            Comment


              #21
              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"],
              ...
              ]

              Comment


                #22
                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 ...

                Comment


                  #23
                  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
                  TinkerLand : Life's Choices,"No One Size Fits All"

                  Comment


                    #24
                    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

                    Comment


                      #25
                      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
                      TinkerLand : Life's Choices,"No One Size Fits All"

                      Comment

                      Working...
                      X