MQTT Cover STOP action

Hello,

I have some DI-O/Cacon cover managed via MQTT Cover. I only have 2 buttons : UP/DOWN.
When I press UP, I can press UP again to STOP. Same thing for DOWN.

I want to add function this to STOP button. This idea is to get the STOP action in MQTT, get the last action/position of the cover and send to mqtt the action UP or DOWN and stop the cover.

I’m looking at appdameon but it seems i can’t use paho lib for mqtt.??
I can make an external python script if appdaemon can’t but I would prefer use appdaemon.

Do you have an other solution. to implement STOP action ?

Thanks

I don’t know of any reason why you shouldn’t be able to. Why do you say this?

I have checked this thread before writing here : https://community.home-assistant.io/t/appdaemon-mqtt/8118.
Maybe i misunderstood, but i see : [quote=“aimc, post:2, topic:8118”]
AppDaemon currently does not have the code to integrate directly with MQTT,
[/quote]

@aimc I think your comment is being misinterpreted here, but I think you should clarify the situation yourself.

Yes, correct. My comment above was saying that there is no explicit support for MQTT in AppDaemon, but it’s just Python code so you can use any libraries that you like in AppDaemon Apps.

Thanks for clarify this, i will convert my python script to AppDaemon :slight_smile:

1 Like

Hi @Minims, im looking for a way to get some mqtt events in appdaemon.
Is it possible to share your converted script?
I would realy appreciate it.

Hi, i m still using a small Python script without appdaemon. I didn’t get time to migrate to appdaemon but i can share it of course, maybe tomorrow.
I am using mqtt to get and set status of my cover so it just a mqtt call linked with last action. I run this script at startup with systemctl.

Edit : add script parts :

#!/usr/bin/python

import paho.mqtt.client as mqtt
import redis
import time
import re
import json

def init_db():
    db = redis.Redis(host='127.0.0.1', port=6379, db=0)
    return db

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    client.subscribe( [
                        ("RF/command",0),
    ] )

def on_message(client, userdata, msg):
    db = init_db()
    if str(msg.topic) == "RF/command":
        print "Topic: ", msg.topic+'\nMessage: '+str(msg.payload)
        if str(msg.payload) == "10;NewKaku;123456;1;ON":
            db.setex('window_currentPosition',"ON",2592000)
        elif str(msg.payload) == "10;NewKaku;123456;1;OFF":
            db.setex('window_currentPosition',"OFF",2592000)
        elif str(msg.payload) == "10;NewKaku;123456;1;STOP":
            cover('window',client)
        elif str(msg.payload) == "10;NewKaku;654321;2;ON":
            db.setex('door_currentPosition',"ON",2592000)
        elif str(msg.payload) == "10;NewKaku;654321;2;OFF":
            db.setex('door_currentPosition',"OFF",2592000)
        elif str(msg.payload) == "10;NewKaku;654321;2;STOP":
            cover('door',client)
   
def cover(name,client):
    db = init_db()
    if name == 'window':
        time_to_100 = 14 // later
        payload_on = "10;NewKaku;123456;1;ON"
        payload_off = "10;NewKaku;123456;1;OFF"
    elif name == 'door':
        time_to_100 = 22 // later 
        payload_on = "10;NewKaku;654321;2;ON"
        payload_off = "10;NewKaku;654321;2;OFF"

    if db.get(str(name)+'_currentPosition') == 'OFF':
        print("OFF")
        client.publish("RF/command",payload_off,qos=0,retain=False)
    elif db.get(str(name)+'_currentPosition') == 'ON':
        print("ON")
        client.publish("RF/command",payload_on,qos=0,retain=False)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.10", 1883, 60)
client.loop_forever()
1 Like

HI

try this code ( this code forward the message to other topic only as proof)

import appdaemon.appapi as appapi
import paho.mqtt.client as mqtt

class TestMQTTClient(appapi.AppDaemon):

  def initialize(self):
    self.sensor = self.args["sensor"]

    self.client = mqtt.Client("appdaemon")
    self.client.on_connect = self.mqtt_on_connect
    self.client.on_message = self.mqtt_on_message

    self.log("Iniciando conexión de cliente MQTT")
    self.run()

  def run(self):
    self.client.connect("127.0.0.1", 1883, 60)
    self.client.subscribe("test/pir", 0)
    self.client.loop_start()

  def terminate(self):
    self.log("Finalizando conexión de cliente MQTT")
    self.client.loop_stop()

  def mqtt_on_connect(self, client, userdata, rc,a):
    self.log("Conenctado con codigo: {}".format(str(rc)))

  def mqtt_on_message(self, client, userdata, msg):
    payload = msg.payload.decode("utf-8")
    self.log("Topico:{}\n mensaje:{}".format(msg.topic,payload))
    self.call_service("mqtt/publish", topic = "home/pirs/xxxxx", payload = payload, qos = "0", retain = "false" )
1 Like