Advice On JavaScript Again

I was able to find a huge solution to one of my problems yesterday using this forum, I'm hoping to get one last piece of advice to figure out the one remaining issue with this code.

if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = {"value":0};
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks = {"value":0};
if (typeof msg.payload.FsFlow === "undefined") msg.payload.FsFlow = {"value":0}; 
if (typeof msg.payload.MsFlow === "undefined") msg.payload.MsFlow = {"value":0};
if (typeof msg.payload.Ds1Flow === "undefined") msg.payload.Ds1Flow = {"value":0};
if (typeof msg.payload.SpruFlow === "undefined") msg.payload.SpruFlow = {"value":0};
if (typeof msg.payload.Jc1Flow === "undefined") msg.payload.Jc1Flow = {"value":0};
if (typeof msg.payload.Jc2Flow === "undefined") msg.payload.Jc2Flow = {"value":0};
if (typeof msg.payload.Jc3Flow === "undefined") msg.payload.Jc3Flow = {"value":0};
if (typeof msg.payload.Jc4Flow === "undefined") msg.payload.Jc4Flow = {"value":0};
if (typeof msg.payload.FacFlow === "undefined") msg.payload.FacFlow = {"value":0};
if (typeof msg.payload.JcFlow === "undefined") msg.payload.JcFlow = {"value":0};
if (typeof msg.payload.FacPress === "undefined") msg.payload.FacPress = {"value":0};
if (typeof msg.payload.JcPress === "undefined") msg.payload.JcPress = {"value":0};
if (typeof msg.payload.Ds2Flow === "undefined") msg.payload.Ds2Flow = {"value":0};

msg.topic = `INSERT INTO SSreadings (ProgramNumber, Date, Time, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, date('now'), time('now','localtime'), ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`
return msg;

My question is there a way to return the last value inputted in the table if undefined only for the ProgramNumber and NumOfLeaks part of the message.

if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = {"value":0};
if (typeof msg.payload.NumOfLeaks === "undefined") msg.payload.NumOfLeaks = {"value":0};

Thanks again for yesterdays help!

What does that mean? What table?

This Table

msg.topic = `INSERT INTO SSreadings (ProgramNumber, Date, Time, NumOfLeaks, FsFlow, MsFlow, Ds1Flow, SpruFlow, Jc1Flow, Jc2Flow, Jc3Flow, Jc4Flow, FacFlow, JcFlow, FacPress, JcPress, Ds2Flow) VALUES (${msg.payload.ProgramNumber.value}, date('now'), time('now','localtime'), ${msg.payload.NumOfLeaks.value}, ${msg.payload.FsFlow.value}, ${msg.payload.MsFlow.value}, ${msg.payload.Ds1Flow.value}, ${msg.payload.SpruFlow.value}, ${msg.payload.Jc1Flow.value}, ${msg.payload.Jc2Flow.value}, ${msg.payload.Jc3Flow.value}, ${msg.payload.Jc4Flow.value}, ${msg.payload.FacFlow.value}, ${msg.payload.JcFlow.value}, ${msg.payload.FacPress.value}, ${msg.payload.JcPress.value}, ${msg.payload.Ds2Flow.value});`

The data is being sent via 15 MQTT in nodes but only ON Value Change, so when one of the nodes value doesnt change the message reads undefined

The issue I am having is for only the ProgramNumber and NumOfLeaks I want to return the previous value if undefinesd. But the rest I want a "0".

Right now all the values are returning "0" if undefined. Which is what you helped me with yesterday.

That isn't a table, that is a statement assigning a String containing an SQL query to msg.topic.

So I think what you want is to retain the last good values of those two properties and if they are not valid in the payload coming in then to use the last good values instead. If that is correct then have a read of the node red docs guide on Writing Functions where it shows how to use the node context to save data. So every time you get a good value of those properties save it to the context and then when you get bad data pick it up from the context again.

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Filter">

<table style='width:100%' border="1" id="SSreadings">
  <tr>
    <th>ProgramNumber</th> 
    <th>Date</th>
    <th>Time</th>
    <th>NumOfLeaks</th>
    <th>FsFlow</th>
    <th>MsFlow</th> 
    <th>Ds1Flow</th>
    <th>SpruFlow</th>
    <th>Jc1Flow</th>
    <th>Jc2Flow</th>
    <th>Jc3Flow</th>
    <th>Jc4Flow</th>
    <th>FacFlow</th>
    <th>JcFlow</th>
    <th>FacPress</th>
    <th>JcPress</th>
    <th>Ds2Flow</th>
  </tr>
  <tr ng-repeat='x in msg.payload | limitTo:25'>
    <td>{{msg.payload[$index].ProgramNumber}}</td>
    <td>{{msg.payload[$index].Date}}</td>
    <td>{{msg.payload[$index].Time}}</td>
    <td>{{msg.payload[$index].NumOfLeaks}}</td>
    <td>{{msg.payload[$index].FsFlow}}</td> 
    <td>{{msg.payload[$index].MsFlow}}</td>
    <td>{{msg.payload[$index].Ds1Flow}}</td>
    <td>{{msg.payload[$index].SpruFlow}}</td>
    <td>{{msg.payload[$index].Jc1Flow}}</td>
    <td>{{msg.payload[$index].Jc2Flow}}</td>
    <td>{{msg.payload[$index].Jc3Flow}}</td>
    <td>{{msg.payload[$index].Jc4Flow}}</td>
    <td>{{msg.payload[$index].FacFlow}}</td>
    <td>{{msg.payload[$index].JcFlow}}</td>
    <td>{{msg.payload[$index].FacPress}}</td>
    <td>{{msg.payload[$index].JcPress}}</td>
    <td>{{msg.payload[$index].Ds2Flow}}</td>
  </tr>
</table>

<script>
function myFunction() {
  // Declare variables
  var input, filter, table, tr, td, i, txtValue;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("SSreadings");
  tr = table.getElementsByTagName("tr");

  // Loop through all table rows, and hide those who don't match the search query
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[0];
    if (td) {
      txtValue = td.textContent || td.innerText;
      if (txtValue.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

Sorry, The code above is my actual table, I am just trying to pass all the MQTT values through the function node to assign the values in the correct order.

But yes that is exactly what I want to do so I will read through the link and see what I come up with.

context.set("Pvalue", msg.payload.ProgramNumber);
var ProNum = context.get("Pvalue");

if (typeof msg.payload.ProgramNumber === "undefined") msg.payload.ProgramNumber = {"value":0};

This is what I have come up with but I just dont know what to set msg.payload.ProgramNumber = {"value":0}; to. Any Suggestions?

When I Try to set it equal to the ProNum Variable I get an undefined value error.

TypeError: Cannot read property 'value' of undefined

You must only do that if there is a good value, otherwise you are just saving undefined.

That's probably because you have saved undefined.

Thanks again for helping me, for the resolution of this thread I used the following code, and this worked through numerous test.

/* Defines ProgramNumber as undefined or defined, gives previous value if undefined*/
if (typeof msg.payload.ProgramNumber !== "undefined") {
    context.set("Pvalue", msg.payload.ProgramNumber)
} else {
    context.set("Pnull",msg.payload.ProgramNumber)
}
msg.payload.Pvalue = context.get("Pvalue");

What is the Pnull context var for?

What will happen if the very first time through there is no ProgramNumber?

I dont really need the else { context.set("Pnull",msg.payload.ProgramNumber) on second thought as long as im calling "Pvalue".

There will never be a first time through without the program number. The way I have the Ladder Logic set up. The machine will not run without a programed call. Im just pig tailing off that Program number signal.

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