# Node-Red AIML Mosquitto

**URL:** <https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201>\
**Category:** General\
**Created:** [12 April 2022 21:20 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201 "2022-04-12T21:20:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego.82](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Diego.82](https://discourse.nodered.org/u/Diego.82)\
**Post date:** [12 April 2022 21:20 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/1 "2022-04-12T21:20:49Z")

</div>

Hi guys I have a problem between Node-Red and Python communicating through the broker Mqtt mosquitto. The problem is that I cannot convert the message.payload sent by the broker to python to pass it as simple user input.

Code Example:

#!/usr/bin/python3  
import os  
import sys  
import json  
import io  
import aiml

def on\_message(client, userdata, message):  
**print(str(message.payload.decode("utf-8")))**  
#mqtt  
from paho.mqtt.client import \*  
client = Client(client\_id = "IA")

client.connect("localhost")

client.loop\_start()  
client.subscribe("domanda")  
client.on\_message=on\_message

if os.path.exists(BRAIN\_FILE):  
print("Carico intelligenza artificiale: " + BRAIN\_FILE)  
k.loadBrain(BRAIN\_FILE)  
else:  
print("Carico file AIML")  
k.bootstrap(learnFiles="std-startup.aiml", commands="load aiml b")  
print("Salvo file intelligenza Artificiale: " + BRAIN\_FILE)  
k.saveBrain(BRAIN\_FILE)

# Inserimento domanda

while True:

```
input_text = input("Dimmi... ")

#Non mi passa richiesta Broker
**response = k.respond(input_text)**

#Editare per escludere risposta a terminale
print(response)

#Invia Risposta a Broker
client.publish(topic = "risposta", payload = (response))

```

If I print on the screen "print (str (message.payload.decode (" utf-8 ")))" the message is displayed correctly, if I try to send it as input "response = k.respond (str (message.payload.decode ("utf-8")) "I'm getting errors  
in summary I should send a string value with subscribe Mosquitto and send it to python as if it were user input. Thank you all for any help

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [12 April 2022 21:44 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/2 "2022-04-12T21:44:36Z")

</div>

I am **not** a programmer, but where did you import `paho.mqtt.client`?

In my code I have this at the start:

```auto
import time
import sys
import paho.mqtt.client as mqtt
import blinkt

```

Yours seems to be missing that line.

But I shall restate:  
**I am not a programmer**

---

<div class="post-metadata">

**Author:** ![Diego.82](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Diego.82](https://discourse.nodered.org/u/Diego.82)\
**Post date:** [13 April 2022 11:26 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/3 "2022-04-13T11:26:25Z")

</div>

from paho.mqtt.client import \*  
client = Client(client\_id = "IA")

---

<div class="post-metadata">

**Author:** ![Diego.82](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Diego.82](https://discourse.nodered.org/u/Diego.82)\
**Post date:** [13 April 2022 11:29 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/4 "2022-04-13T11:29:07Z")

</div>

if you look at the code it is imported after:  
#MQTT  
I do it to keep track. common libraries import them at the beginning

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 April 2022 11:30 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/5 "2022-04-13T11:30:13Z")

</div>

> [@Diego.82](#):
>
> I cannot convert the message.payload sent by the broker to python

In node-red, add a debug node - set to display the complete msg object - to the node feeding the mqtt-out node so we can see what the data looks like.

---

<div class="post-metadata">

**Author:** ![Diego.82](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Diego.82](https://discourse.nodered.org/u/Diego.82)\
**Post date:** [13 April 2022 11:35 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/6 "2022-04-13T11:35:27Z")

</div>

Actually I already know how the data looks in fact convert them to utf8 via:  
print (str (message.payload.decode ("utf-8")))

my problem is to convert print to input for python. As if given from terminal entries.

---

<div class="post-metadata">

**Author:** ![Diego.82](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@Diego.82](https://discourse.nodered.org/u/Diego.82)\
**Post date:** [13 April 2022 11:42 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/7 "2022-04-13T11:42:51Z")

</div>

I'll explain better if I define: input\_text = input ("Tell me ...")

and I set the answer:

response = k.respond (input\_text)

then by entering the text from the keyboard the system responds correctly while if instead of "input\_text" I insert:

response = k.respond (str (message.payload.decode ("utf-8")) the system only displays the payload but does not return python response to me as if the final send was missing or did not see the data

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 June 2022 11:42 UTC](https://discourse.nodered.org/t/node-red-aiml-mosquitto/61201/8 "2022-06-12T11:42:51Z")

</div>

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