Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 2

HC3 and MQTT in QA


Guest kallecux
 Share

Question

Guest kallecux

Hello! - Happy Easter!

 

I try to play with the new MQTT-functionality in QA's, but im am not successful.

There i have a MQTT broker on Hassio and this code on HC3:

 

Please login or register to see this code.

When i save the QA i get this message on HC3:

[13.04.2020] [13:33:33] [DEBUG] [QUICKAPP353]: onConnected: {"sessionPresent":false,"returnCode":0}

 

The Broker says this:

1586777612: New connection from 192.168.3.116 on port 1883.
[INFO] found hc3 on Home Assistant
1586777613: New client connected from 192.168.3.116 as randomjdtfpexjvy (p2, c0, k0, u'hc3')

 

So i think everything is OK, but i get no:

- onSubscribeMessage when the QA started

- onPublished Message when i toggle the switch 

 

Is there anybody who can give me a hint?

Thanks in advance.

Karl Heinz 

 

Edited:

When i toggle the QA Switch, the message is transferred to the broker and the other clients (NodeRed-Cient), but the function onPublished is NOT called! 

I think the callback / envent is not executed, so i can not get the subscribed data. :-(

 

Edited by kallecux
Link to comment
Share on other sites

Recommended Posts

  • 0
On 5/14/2020 at 10:56 AM, jgab said:

Can you register different callbacks for each topic (humid, temp etc). Now you seem to have one big callback?

Alternatively, what happens if you do

Please login or register to see this code.

or, even more chopped up

Please login or register to see this code.

The idea is to give time for the MQTT client to process messages... if that is the problem. 

Thank you all for sharing, i reused a lot of you work here .

Perhaps someone can se what i'm doing wrong?

 

I subscribe to the "ventilation/moist/RH" with  self.client:subscribe("ventilation/moist/RH")

But the system just gets all events from the MQTT and I cant filter out the RH

 

Any idea why ?

 

Please login or register to see this code.

 

 

 

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0
1 hour ago, ChristianSogaard said:

Thank you all for sharing, i reused a lot of you work here .

Perhaps someone can se what i'm doing wrong?

 

I subscribe to the "ventilation/moist/RH" with  self.client:subscribe("ventilation/moist/RH")

But the system just gets all events from the MQTT and I cant filter out the RH

 

Any idea why ?

 

Please login or register to see this code.

 

 

 

Please login or register to see this code.

 

I found the same issue described here. :-) thank you for posting

Please login or register to see this link.

Link to comment
Share on other sites

  • 0

I'm new to coding in HC3 and was wondering if anyone could help me understand how to publish to a know broker. I am trying to publish data to a broker. I managed to do this in python with the following code : 

 

import sys
import time
import numbers
from datetime import datetime, timezone, timedelta
import json
import paho.mqtt.client as mqtt
import random

mqtt_server = "mqtt.energydata.dk"
mqtt_port = 1883
mqtt_user = "Username"
mqtt_pass ="Password"

def on_connect(client, userdata, flags, rc):
    if rc==0:
        client.connected_flag = True
        print("connected OK")
    else:
        print("Bad connection Returned code=",rc)


mqtt.Client.connected_flag = False

client = mqtt.Client("Flexhouse 2 client")
client.username_pw_set(mqtt_user, mqtt_pass)

client.on_connect=on_connect

client.loop_start()
print("Connecting to broker: ", mqtt_server)
client.connect(mqtt_server, mqtt_port, 60)

while not client.connected_flag:
    print("In wait loop")
    time.sleep(1)

print("in Main Loop")

while True:
  my_topic = "9F7J/M4/P2/DTU/PLDK/SYSLAB/studenproject_Q1_2021/SMART_HOME_CONSUM/gas_in_temperature"
  my_value = random.randint(0, 100)

  now = datetime.now(timezone.utc)
  epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) # use POSIX epoch
  my_timestamp = (now - epoch) // timedelta(microseconds=1) // 1000

  json_body = '{"value": ' + str(1.52) + ', "timestamp": ' + str(my_timestamp) + ' }'

  print(json_body)
  client.publish(my_topic, json_body)
  time.sleep(1)

client.loop_stop()  
client.disconnect()
#######################################################################################

Is there anyone here who could help me translate this to HC3?

Link to comment
Share on other sites

  • 0

Hi, I have been following this thread and I can get my Tasmota partially working over MQTT, but i am getting this error, I am actually usingt the emulator so maybe thats the issue. Anyone seen this before?

 

Thanks

Jamie

Please login or register to see this attachment.

Link to comment
Share on other sites

  • 0
7 hours ago, Jamie said:

Hi, I have been following this thread and I can get my Tasmota partially working over MQTT, but i am getting this error, I am actually usingt the emulator so maybe thats the issue. Anyone seen this before?

 

Thanks

Jamie

Please login or register to see this attachment.

Do you get the same error on the HC3?

The error happens when you call json.encode from line 46 in file clean.lua- what happens there?

Link to comment
Share on other sites

  • 0
8 hours ago, jgab said:

Do you get the same error on the HC3?

The error happens when you call json.encode from line 46 in file clean.lua- what happens there?

Humm good point;

Please login or register to see this image.

/monthly_2021_11/image.png.5748ff3988b4ae141331f93ad54b0174.png" /> 

Must be the json.decode

Link to comment
Share on other sites

  • 0
42 minutes ago, Jamie said:

Humm good point;

Please login or register to see this link.

 

Must be the json.decode

add

   self:debug(event.payload)

and see what you got

Just now, jgab said:

add

   self:debug(event.payload)

and see what you got

 or better - because you run in the emulator. Set a break-point at line 46 and inspect the payload variable.

Link to comment
Share on other sites

  • 0

If you set the emulator's mqtt debug flag you will get logs from the mqtt "engine" giving you a better idea what's going on.

Link to comment
Share on other sites

  • 0

I checked the mqtt code in TQAE (no one used it since I moved it over from the old emulator)

...and I had an error when it tried to include some libraries.

I fixed it in v0.40 that I just pushed to my GitHub.

Link to comment
Share on other sites

  • 0

If I run this in the TQAE emulator  (note that I added the mqtt debug flag in the header)

Please login or register to see this code.

 

I have set up a MQTT broker on a NodeRed server (node-red-contrib-aedes) that answers to the messages.

I get this output in the console (I restarted the broker at the end so the client got a close message)

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

Thanks for the response, this is the error. Need to take a better look tonight;

Please login or register to see this attachment.

Link to comment
Share on other sites

  • 0

I worked it out, it because the payload doesn't always present as json string and hence errors, I have put some logic in to detect for JSON, which has worked. Now I am getting a nil value error;

Please login or register to see this attachment.

Link to comment
Share on other sites

  • 0

You assign 'local payload', but it goes out of scope when you exit the if-then-else...

Link to comment
Share on other sites

  • 0

Hi, yes I worked that out. Also some of the MQTT subscribes were coming back not as JSON, its a Tasmota plug and its fairly standard for the stat/tasmota-plug1/POWER for example. Spent ages coding a test for JSON and realised I could probably just exclude them! Anyway I now have the issue that I need to detect between different MQTT messages as not all responses, I am guessing can do if payload.topic = xxx then xxx like this. It seems to work but feels messy. Also had anyone updated the watts on a QA to get the power working correctly? I assume its got to be a child device.

 

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

I try to control my lamps through MQTT in a QA, but get an unknown error: handleJsonRpc.

It's working fine when I send the commands by a ruby MQQT client:

Please login or register to see this code.

It's just a simple code, but for me now okay for learn more about this. But what's wrong with this?

 

 

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0

.publish != :publish

so

please replace 

 

Please login or register to see this code.

 

 

Please login or register to see this code.

Edited by 10der
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 0
18 minutes ago, 10der said:

.publish != :publish

so

please replace 

 

Please login or register to see this code.

 

 

Please login or register to see this code.

 

Thanks?, this works. And much better than control by UDP 

Link to comment
Share on other sites

  • 0

One more question, how can a send a dimmer value?

It works if I manually enter the value between 0 and 100, but when I use the value of a slider it doesn't work.

 

Please login or register to see this code.

 

Link to comment
Share on other sites

  • 0
12 hours ago, Firepeet said:

One more question, how can a send a dimmer value?

It works if I manually enter the value between 0 and 100, but when I use the value of a slider it doesn't work.

 

Please login or register to see this code.

 

 

  • Thanks 1
Link to comment
Share on other sites

  • 0
2 hours ago, jgab said:

Spend hours trying to fix this, and the solutions looks always so simple when I see it?

Thank so much again for helping??

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...