Trouble filtering key/value from MQTT message

I have a RF433 fan that I am trying to integrate with Home Assistant using a modified Sonoff RF-Bridge with Tasmota and Portisch firmware.

I want to be able to detect when a button on the remote is pressed to be able to modify the state of the device within Home Assistant to keep the fan and HA device state in sync.

Each button on the remote generates a different 24 character code for the payload.RfRaw.Data key value in the incomming MQTT message. This can be clearly seen when I set up a debug trace capturing the whole message:

I have written a simple function to filter the message based on the the Data value.

var rfdata = msg.payload.RfRaw.Data.toString().trim();
var rfdataType = typeof rfdata; // Get the type of rfdata

node.log("Received rfdata: " + rfdata + " (Type: " + rfdataType + ")"); // Log the received rfdata and its type

if (rfdata.toUpperCase() === "AAA4232E00F902CC63560155") {
    return msg; 
} else {
    node.status({
        fill: "red", 
        shape: "ring", 
        text: "No match: " + rfdata + " (Type: " + rfdataType + ")"
    });
    return null; // Stop flow if no match
}

The node displays "No match: AAA4232E00F902CC63560155"

I have tried a similar approach using a switch node in place of a function with the same outcome.

I have tried checking the data type of the value, it is a string value

Hi and welcome to the forum.

For the first couple of days you are limited to how many posts you can do.

Not that it matters, please edit the post (if you want) and where the code from the function node is, before you post it, click up the top on the </> button and paste the code where it says.

Now, to the question:
I'm not sure I understand the question and what you want to do.

Ok, you are (or seem to be) filtering the message/s and if the value matches, it is passed.

Yes, you get a visual indication if it doesn't match.
But honestly a switch node could do what you want.

Just edit the part of the message you want to check, and paste the original case of the message
(AAA4232E00F902CC63560155)
Then only if that part matches is the message passed on.

The string in your image does not match the string you are looking for.
Add a debug node prior to the function and confirm the actual string you want.

As @Trying_to_learn says a function node is overkill just to match 1 string

Hi, and welcome to forum! The answer is most likely posted by the other above.

Please compare the string in the msg with the string you filter on:

  • "AAA4232E00F902CC63560155"
  • "AAA4232D00FF02D063560155"

Other than that, there are so many things to say. First off, once you got the message into node red (and out of the mqtt node), this is a pure javascript issue. Which is good cause that is usually easier than mqtt problems :smiley: And whenever I see var I shiver, so all I can say is let/const is the preferred standard.

I wouldn't necessarily say to drop function node, but can certainly make the logic much simpler:

if(msg.payload?.RfRaw?.Data == "AAA4232D00FF02D063560155") {
  // match
  return msg;
}
// no match
return;

Thanks for pointing out my error. I did originally check that the values were the same, in fact comparison value in the script was cut and paste from the debug log. It appears that the real issue is that the value is changing. It appears that my assumption that each button on the remote generates a unique 24 character code was incorrect so I need to go back to the drawing board and work out a way to determine when each remote button is pressed.
It is probably beyond the scope of this discussion board but it appears that if the Tasmota/Portisch RF-Bridge is placed in capture mode RFRAW 177 it does generate a unique long data string (>24 char) for each button however as soon as I issue an MQTT command via HA to activate one of the available functions on the fan via the RF-Bridge the capture mode changes to RFRAW 1.

It is easier to capture the raw data as a button, in learning mode. Then it will store it under the key you specify - when the event arrives you only need to check for the key it detects as the raw data will never be the same.

ie. first send rfraw 1, followed by: rfkey1 2 - then press the button from your remote - you will hear a beep, then it learned the command and stored it under key 1. Then you can send rfkey1 6 which will send the learned command. (and this in turn can also be send over MQTT)

Where 2 is learning mode and 6 is sending the learned command. There are 16 keys available to learn.

Thanks bakman2, unfortunately this method wont work for me as I have 4 fans with 5 buttons each and I am planning to integrate other devices in future.

But you would have .... filter/SWITCH nodes for each of the different fans / buttons.

Then each one would have it's own output which you then parse.