Forward Messages From XMPP to MQTT

Hi all,

I am very new to Node_Red. I never worked with this but I think it should be possible to forward messages received by xmpp to forward them to mqtt.

My goal is to create a template sensor of messages sent from a client and to push them into a sensor readable by homeassistant.

Any idea how i can do that?

I played a bit but had to admit that i dunno at all what i am doing here :slight_smile:

So what is not working here?

When you are using an MQTT node to publish to a topic, it will only read the msg.payload and send that through the topic.

Your topic here is incorrect though. For starters, in MQTT it is bad form to use a / to start your topic, as the / is seen as another topic. Secondly, if you want to use it in Home assistant, you need to set your topic to one that can actually be read by home assistant. How do you have your MQTT sensor set up in HA? Publish to that topic.

OKā€¦ first step was succesfully
I receive messages now in MQTT topixā€¦ i Know need to know how to extract this payload into template sensorā€¦ any idea?

Help yourself :slight_smile: I now added a mqtt sensor:

## XMPP to MQTT Messages Sensor

  - platform: mqtt    
    name: "XMPP 2 MQTT Emilie"
    state_topic: "xmpp/"
    qos: 0

Now i like to fine tune the whole thing a bitā€¦ As i acccept all messages from everybody and everythiing i would like to add the contact name in the sensor like Holger: message
Any idea how to do so?

Then there is another thingā€¦ do you think it is possible to somehow keep messages ā€¦ lets say 10 messages which are added to this sensorā€¦ like if someone writes multiple messages that i can see them all (or at least up to 10 last messages)

You create an MQTT sensor, per the documentation.

Work so far but now I really need the senders name somehow included :stuck_out_tongue: If not I canā€™t distinguish the senders :stuck_out_tongue:

So send it with the payload?

I am totally newā€¦ do u have a hint how to make it ?

I have zero information on what data comes through in the msg object from xmpp.

Here is what I want you to do to learn how to access this information.

Output your XMPP to a debug node and on the debug node change a couple of options:
Name: XMPP
Change the option that says msg. to COMPLETE MSG OBJECT.

Then send a message over XMPP. Once you do that, you should see the debug message come through the debug window on Node Red.

Get that information and I want you to look at the data and how it is structured.

 - platform: mqtt
    name: mqtt payload test
    state_topic: "xmpp/messages"
    qos: 0
    value_template: "{{ msg.topic }}: {{msg.payload}}"

I will follow your guide and revert with detailsā€¦ this sensor does not work while this does:

  - platform: mqtt    
    name: "XMPP 2 MQTT"
    state_topic: "xmpp/messages"
    qos: 0
15.10.2019, 18:13:18node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "25c7788c.9651d8" }
15.10.2019, 18:13:18node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "59877cd3.a389d4" }
15.10.2019, 18:13:19node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "f684e283.808d4" }
15.10.2019, 18:13:20node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "58a7bc81.d3ab14" }
15.10.2019, 18:13:21node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "c70ee694.d6e978" }
15.10.2019, 18:13:22node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "7870ad91.c52c54" }
15.10.2019, 18:13:26node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "5c960aa5.6f9514" }
15.10.2019, 18:13:27node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "aa7de770.4ba5c8" }
15.10.2019, 18:13:39node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "59216687.022d78" }
15.10.2019, 18:13:39node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "3e4c11af.83edbe" }
15.10.2019, 18:13:41node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "71305cf4.335ed4" }
15.10.2019, 18:13:42node: XMPP
[email protected] : msg : Object
{ topic: "[email protected]", payload: object, _msgid: "a3af355c.405198" }

Seems that payload is in object? Sorry total noob here . I get the message well with the mqtt sensor.

msg.topic and msg.payload do not exist inside Home Assistant. You cannot use the same syntax between the 2. Once you know what data is coming FROM the XMPP node, you will know how to fill in the rest. But what you are looking for is value_json.something, so if your XMPP spits out a valid JSON object containing: {ā€œfromā€: ā€œusernameā€, ā€œmessageā€: ā€œsome message from XMPPā€} then you would access this information using the following:

{{ value_json.from }} : {{ value_json.message }}

That doesnā€™t look like the information that is coming from the debug node to me. Looks like you didnā€™t expand it out.

I recommend you delete that last post. There is a lot of email addresses in there you may not want public, and you still havenā€™t provided the information needed.

Why are you copying it out like this?

to give u what i have and to get it working

15.10.2019, 19:02:37node: XMPP
[email protected] : msg.payload : Object
object
presence: "online"
status: "Renovieren.... Renovieren..Mund nochmals Renovieren šŸ¾"
15.10.2019, 19:02:39node: XMPP
[email protected] : msg.payload : Object
{ presence: "away", status: "Renovieren.... Ren

I just donā€™t see where the message is gone

The description of this xmpp node is:

Connects to an XMPP server to receive messages.

The Buddy field is the jid of the buddy or room you want to receive messages from. Leave blank to receive from anyone.

Incoming messages will appear as msg.payload on the first output, while msg.topic will contain who it is from. If part of a chat room then msg.room may also be set.

The second output will show the presence and status of a user in msg.payload. Again msg.topic will hold the users jid.

Seems like I only see the SECOND output but not the first ā€¦ :confused:

and here another debug:

15.10.2019, 19:27:47node: XMPP
[email protected] : msg : Object
object
topic: "[email protected]"
payload: object
presence: "online"
status: "Renovieren.... Renovieren..Mund nochmals Renovieren šŸ¾"
_msgid: "6b4fa65a.3cfd48"

Thatā€™S really strangeā€¦ I just dunno where the messages are goneā€¦ or maybe it is this part: _msgid: "6b4fa65a.3cfd48" ?

OKā€¦ Now i got it! I added the Debug at th wrong place :stuck_out_tongue:

Based on your first screenshot, there are 2 outputs. To which output did you put the debug node?

sorry i didnā€™t save my last postā€¦ I got it when Iattached the debugger to the first output. I did not know that those two dots meant outputsā€¦ but we never stop learning :slight_smile:
Now it should be possible right? I still donā€™t know how but at least i provided yu what you wanted. Another question: the senders adressā€¦ itā€™s a bit ugly and longā€¦ Iā€™d like to adapt this like just by a name instead of JID

Yes. You need to change your payload before it gets sent to the MQTT topic.

You will need to convert it. Time to learn some javascript.

You can do both of these things in a function node before the MQTT topic.

Here is an example I worked up:

Inside the function node:

var sender = msg.topic.split("@", 1).toString();
var message = msg.payload;
msg.payload = {
    "sender": sender,
    "message": message
}
return msg;

The output:
image

The MQTT message should contain:
{"sender":"user", "message":"This is my message"}

access it in HA sensor by using:

value_template: "{{ value_json.sender }}: {{ value_json.message }}"

WOW!! Thatā€™s like magic for me !! YOu are my Heroā€¦ One last single stupid question: To get the sender in capital lettersā€¦ how would i put this in those function?

{ā€œsenderā€:ā€œUSERā€, ā€œmessageā€:ā€œThis is my messageā€}

EDIT:
Got it ! ā€¦ wow I just guessed :stuck_out_tongue:

var sender = msg.topic.split("@", 1).toString().toUpperCase();
var message = msg.payload;
msg.payload = {
    "sender": sender,
    "message": message
}
return msg;
1 Like