0

I created a Mqtt subscribe with python that has HA as broker … when I make the first connection I start a series of messages of all the mqtt buttons that I connected even if I am not activating them because my subscription is "house/click/ # ". Once the list is done … if you press a button I start getting the messages I need … is there a way to delete this first list that it gives me.

this is the code I use:

import paho.mqtt.client as paho
import pandas as pd
import pyautogui
import os
import platform


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))


def on_message(client, userdata, msg):
    pyautogui.FAILSAFE=False
    os_system=platform.system()


    # Selezione area dello screenshot

    if os_system == 'Darwin':
        region=(10,80, 850, 100) #MAC
    else:
        region=(60,130,900,250) #RASP

    tab_pos=pd.read_csv('table_position.csv')
    print(msg.topic+"  "+str(msg.payload)+" "+str(msg.qos))
    mqtt_name=str(msg.topic).split("click/")[0]+str(msg.topic).split("click/")[1]
    mqtt_payload=msg.payload.decode('utf-8')
    index_loc=tab_pos.index[tab_pos['mqtt_topic'] == mqtt_name].tolist()
    x_loc=tab_pos.iloc[index_loc[0]][0]
    y_loc=tab_pos.iloc[index_loc[0]][1]
    new_xloc=x_loc+region[0]
    new_yloc=y_loc+region[1]
    if tab_pos.iloc[index_loc[0]][5] == "cover":
        if str(mqtt_payload) == "OPEN":
            if str(mqtt_payload) == "STOP":
                pyautogui.click(new_xloc+8,new_yloc+2)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
            else:
                pyautogui.click(new_xloc+8,new_yloc+2)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
        if str(mqtt_payload) == "CLOSE":
            if str(mqtt_payload) == "STOP":
                pyautogui.click(new_xloc+8,new_yloc+25)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
            else:
                pyautogui.click(new_xloc+8,new_yloc+25)
                pyautogui.moveTo(region[0],region[1])
                print(str(new_xloc)+","+str(new_yloc))
    else:
        pyautogui.click(new_xloc+10,new_yloc+10)
        pyautogui.moveTo(region[0],region[1])
        print(str(new_xloc)+","+str(new_yloc))


client = paho.Client(client_id = "atena_mqtt_click")
client.on_subscribe = on_subscribe
client.on_message = on_message
client.username_pw_set("xxxx", "xxxxx")
client.connect("XXX.XXX.XXX.XXX", 1883)
client.subscribe("house/click/#", qos=1)
client.loop_forever()

this is the output

Subscribed: 1 (1,)
house/click/xxxxxx/light b’OFF’ 0
711,104
house/click/xxxxxx/alarm b’OFF’ 0
454,134
house/click/xxxxxx/light b’OFF’ 0
795,104
house/click/xxxxxxx/cover b’STOP’ 0

after these messages that all come together I can begin to have the messages due to my actions on the buttons

someone could tell me how I can do a clean session without having these first lines?

thanks

chpiero
  • 41
  • 5
  • Which broker are you using? – Hsn Aug 16 '20 at 20:37
  • Your question is not clearly explained. So you mean to say you automatically gets messages when you run your code for the fist time? And then you get normal messages? – Hsn Aug 16 '20 at 20:39
  • @Hsn the broker is Hassio (mosquitto broker). "So you mean to say you automatically gets messages when you run your code for the fist time? And then you get normal messages? " Yes, Yes – chpiero Aug 17 '20 at 08:02
  • If you can also access the publisher side then you can make the retain flag to false. if not then its really hard and you have to go for n workaround like having a flag for each topic etc.. – Hsn Aug 17 '20 at 09:14

1 Answers1

1

Check your MQTT Publisher flag. If your publisher is using the 'retain flag' you need to make it false.

and to delete a retained message from a topic, a client must either replace it with another message with retain flag set to True OR a client must publish a blank message with retain flag set to True on that topic. This is the only way to delete a retained message.

"A retained message is a normal MQTT message with the retained flag set to true. The broker stores the last retained message and the corresponding QoS for that topic. Each client that subscribes to a topic pattern that matches the topic of the retained message receives the retained message immediately after they subscribe. The broker stores only one retained message per topic". Source: https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

Hsn
  • 1,168
  • 2
  • 16
  • 39
  • 1
    `retained` flag is no broker configuration - this is set/unset for a topic by the publisher to tell the broker to persist that message so any new client subscribing this topic is able to get it. Since he just wants to skip the first published messages, this won't be any help I guess – Odysseus Aug 17 '20 at 08:57
  • I think he first needs to set `retain=False` when publishing the topics and on the other hand set `clean_session=True` on his subscribing client instance. Then he will only get the unwanted messages if the subscribing client connects before the publishing clients. – Odysseus Aug 17 '20 at 09:53
  • 1
    Yes you are right. But i think first, he needs to delete the retained message which is already being published and retained in the broker. – Hsn Aug 17 '20 at 09:58