Filtering Node doent work ?!?

Hi,

i want to Filter a analyzed Json Objekt from MQTT

I add a Screenshot

You need to show us where the debug nodes are and tell us what happens and what should happen.

I have a solution with function Node:

if (msg.payload.ENERGY.Current == '0' && msg.payload.ENERGY.Power == '0') {
    return [null, msg];
}else {
    msg.payload = {
        totalstarttime: msg.payload.ENERGY.TotalStartTime,
        totalkwh: msg.payload.ENERGY.Total,
        yesterdaykwh: msg.payload.ENERGY.Yesterday,
        todaykwh: msg.payload.ENERGY.Today,
        period: msg.payload.ENERGY.Period,
        watt: msg.payload.ENERGY.Power,
        factor: msg.payload.ENERGY.Factor,
        voltage: msg.payload.ENERGY.Voltage,
        ampere: msg.payload.ENERGY.Current

    }
}
return msg;

If Power and Current = 0 there are no messages to Influx and else send the msg.

But I would be interested to know if there was another way to do it

That should never do anything, as in the debug node you show that those are numbers, but you are testing for them to be string '0'

It will work because using == doesn't enforce strict type checking, so 0 == '0' is true, but 0 === '0' is false.

Oh yes, that is true.

@Starfoxfs for that line you should have
if (msg.payload.ENERGY.Current === 0 && msg.payload.ENERGY.Power === 0)

Since you want to rename a number properties the simplest way is probably as you have done it.
Why do you not want to write it to influx if the values are zero?

No i want to write it to Influx if Message isn´t 0

See my code above

 return [null, msg];

I also can do this with

if (msg.payload.ENERGY.Current != 0 && msg.payload.ENERGY.Power != 0)

and change the else value to return [null, msg];

Operator === don´t work

only ==

If i have decimal places like 0.017 in the value === will return false and == will return true

if (msg.payload.ENERGY.Current === 0 && msg.payload.ENERGY.Power === 0)
is not the opposite of
if (msg.payload.ENERGY.Current != 0 && msg.payload.ENERGY.Power != 0)

The first one requires both of them to be 0 to give a true result. In that condition the second one gives false, so they are the opposite.
However, if Current is 0 and Power is 1 then the first one gives false (as it requires them both 0), but the second one also gives false, as it requires them both to be non-zero. The opposite of
if (msg.payload.ENERGY.Current === 0 && msg.payload.ENERGY.Power === 0)
is
if (msg.payload.ENERGY.Current != 0 || msg.payload.ENERGY.Power != 0)

No it won't, I suspect it is the point I have just described that is causing the problem.

Exactly, my question was why you do not want to write it to influx if is is zero?

Ok,

I connected a solar station with a shelly flashed to Tasmota. This transmits the values ​​via mqtt every 60 seconds to Nodered. But at night I have 0 values ​​that I don't need in the database. Therefore Nodered should not write anything to the database if the power and ampere values ​​are 0.

The 0 values every 60Seconds at Night over 8 Hours are a many of Data what i don´t need

At the moment it works with == as Operator not with === the === Operator don´t catch the decimal place eg. 0.015

What do you mean by not catching 0.015? Do you mean it thinks that 0.015 is equal to 0?
Show us the failing message and tell us what happens.
Also show us the code that does not work.

This is exactly the Problem:

https://gauravkk22.medium.com/why-0-1-0-2-0-3-is-false-in-js-mystery-unsolved-with-solution-4f7db2755f18

0.015 gives true from the if statement with === operator but i need to give false.

I need that the If statement only give true at exactly 0

No it isn't, that is a different problem. Show us the message going in and your current code and describe what happens.

If you haven't got a failing message, is it the current or the power that is 0.015 and what is the value of the other one?

Ok here the code:

MSG From MQTT (analyzed Json Object):

{"Time":1667494233,"Switch1":"OFF","ENERGY":{"TotalStartTime":"2022-10-29T18:30:48","Total":4.064,"Yesterday":1.786,"Today":0.869,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0,"Voltage":233,"Current":0}}

At The Moment its Dark so i have no Power and no Current

This is my Function Node:

var ampere = msg.payload.ENERGY.Current;
var watt = msg.payload.ENERGY.Power;

if (ampere == '0' && watt == '0') {
    return [null, msg];


} else {
    msg.payload = {
        totalstarttime: msg.payload.ENERGY.TotalStartTime,
        totalkwh: msg.payload.ENERGY.Total,
        yesterdaykwh: msg.payload.ENERGY.Yesterday,
        todaykwh: msg.payload.ENERGY.Today,
        period: msg.payload.ENERGY.Period,
        watt: msg.payload.ENERGY.Power,
        voltage: msg.payload.ENERGY.Voltage,
        ampere: msg.payload.ENERGY.Current,
        factor: msg.payload.ENERGY.Factor,
        scheinleistung: msg.payload.ENERGY.ApparentPower,
        blindleistung: msg.payload.ENERGY.ReactivePower
    }
    return msg;
}

This works actually but it´s not working if i use ===
Eg.:

if (ampere === '0' && watt === '0') {

don´t work

And a Screenshot from all (ignore the 2 Nodes in the bottom):

No, of course not. You have said that ampere must be exactly the same as the string '0', but it is a number 0 so it does not match. As I said earlier it should be
if (ampere === 0 && watt === 0)

Ah sorry i missed the quotation marks in your earlier Post.

Without '' it works fine

Big thanks to you :slightly_smiling_face:

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