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


  • 0

Sending MQTT message from LUA scene


Question

Posted

I want to send a MQTT message when a Fibaro motion detector detects motion.
Is this possible from a LUA scene?
If not, I want to use a QA. What would be the best way to hook up the QA to motion detected events from my motion detectors? 

4 answers to this question

Recommended Posts

  • 1
  • Inquirer
  • Posted

    Sorry for the late reply @frederiksborgvej388, I must have somehow missed your post.

    Here is the code for my QA: (left some non-essential and debug parts out)

     

    local QoS = {
        AT_MOST_ONCE = 0,
        AT_LEAST_ONCE = 1,
        EXACTLY_ONCE = 2,
    }
     
    -- IP address of your MQTT broker. I use a Raspberry Pi with Mosquitto broker
    local uri = "192.xxx.xxx.xxx"
     
    function QuickApp:onInit()
        self:debug("onInit")
        self:connect()
    end
     
    function QuickApp:connect()
        self.client = mqtt.Client.connect(uri)
    end
     
    -- Here I use the MQTT message "hc3/motion", but you send any message for the MQTT broker to re-transmit
    function QuickApp:motion(deviceId)
        self.client:publish("hc3/motion", tostring(deviceId), {qos = QoS.EXACTLY_ONCE})
    end
     
    And then here the code for the LUA scene action side where XXX is the ID of the QA created above:
     
    fibaro.call(XXX"motion", sourceTrigger.id)
     
     
    Hope this helps you on your way!
    • Like 1
    • 0
    Posted

    You can’t hook or subscribe on events in QA. I am mean what you can’t do in easily only just via looping and calling API for grabbing the current event and via filtering what you want 

    about scene - I do not know. IMHO it should works by default just search by forum samples for MQTT IMHO (again) code for QA also implemented for scene. If not?! Yes it’s Fibaro here all is not OK 

    so you should separate your task on two tasks 

    1st task will be hook event for motion . That’s should work without any issues (God thanks) then you should call method in your QA. Just declare method in your QA and call from scene 

    and in QA method send MQTT message to your broker 

    easy?! No. That’s fibaro. 

    • 0
  • Inquirer
  • Posted

    Thanks a lot @10der for your answer.
    I indeed already used the method you suggest, using two tasks. I have a scene that calls a function of a QA with a parameter (id of the device generating the motion event) and the QA function send the MQTT message...
    A pity that it can't be done in an easier/cleaner way, your right!

    • Thanks 1

    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.

    ×
    ×
    • Create New...