MQTT script delay

Hi,

I’m trying to have Homebridge & Nymea talk to each other via MQTT (see also other forum post Homekit & Siri voice control for Nymea (using Homebridge & MQTT) - #3 by loosrob).
I’m now trying to replace the magic in that post by a script.
That seems to work fine for most devices, but I have one issue that is recurrent:

import QtQuick 2.0
import nymea 1.0

Item {

    ThingAction {
        id: publishAction
        thingId: "{481dfe44-972e-4c1a-ba92-0785d2b8b77e}" // Internal MQTT client
        actionName: "trigger"
    }
    
    ThingEvent {
        id:publishReceived
        thingId: "{481dfe44-972e-4c1a-ba92-0785d2b8b77e}" // Internal MQTT client
        eventName: "triggered"
        onTriggered: {
            var paramsTxt = JSON.stringify(params);
            console.log("MQTT event received:", paramsTxt);
            var topic = params["topic"]
            if(topic == "nymea/setScene/aircoOnOff") {
                aircoPower.value=params["data"];
                console.log("airco power", aircoPower.value);
            }
        }
    }
    
    ThingState {
        id: aircoPower
        thingId: "{7909215c-6a89-444c-a476-f75bc52a5ab5}" // Airco Woonkamer
        stateName: "power";
        onValueChanged: {
            var aircoPowerVal = tvPower.value;
            var payload = "true";
            if(aircoPowerVal == 0) {
               payload = "false";
            }
            console.log("airco control, payload", payload)
            publishAction.execute({"topic": "homebridge/getStatus/aircoOn", "data":payload, "qos": 1 })
        }
    }    
}

I would expect this script to change the power status of the Nymea Airco thing (Daikin airco plugin) upon receiving the right MQTT message from the dummy Airco switch in Homebridge, and also send an MQTT message to Homebridge if the power status of the Nymea thing is changed. It does so for the other devices at least, and almost does it for the airconditioning.

However, I get the following return in the console when turning on the device via Homekit/Homebridge:

MQTT event received: {"27ec8baf-0c13-4d0a-aaee-313582592695":"nymea/setScene/aircoOnOff","8af98566-79d9-4e65-b1dc-9067e4f93af1":"true","data":"true","topic":"nymea/setScene/aircoOnOff"}
airco power false
airco control, payload false

So the Nymea script processes the MQTT message and gets to the part where it sets aircoPower.value=params[“data”]=true, but then the next line console.log(“airco power”, aircoPower.value) results in “airco power false”. Right after, onValueChanged is triggered, and it sets the Homebridge device’s power to false.

The airco does is however activated, which is also reflected by updating the power status to true for the Nymea thing, but that change then doesn’t seem to trigger a new MQTT message, so the power status in Homekit/Homebridge remains false.

I suspect the plugin has a small delay in setting the thing power status to true upon receiving the MQTT message, but I don’t understand how this could then cause the above loop.

Any help would be appreciated!

Meanwhile I’ll continue using magic to accomplish the desired effect, as that does seem to work fine, even though the logic of the rules is the same as the logic of the script.

Best,
Rob

As for the state not changing right away after an assignment, that’s somewhat expected. The state in nymea will only change once the device actually reported the state change (when the plugin does setStateValue(), which may be considerably later than the executeAction() call happening when you assign a value. So, while it may seem weird, that’s probably ok and the right behavior of nymea.

The thing I wonder tho is what the var aircoPowerVal = tvPower.value is… Is that a copy/paste error from another script that may break things?

Oowkey I feel silly now :slight_smile: That might just be the thing I overlooked… will have another look tomorrow! Thanks!

That solved the issue indeed… :slight_smile:

1 Like