poll switch status on startup

235 views
Skip to first unread message

Dimitris

unread,
Mar 22, 2021, 5:39:32 AM3/22/21
to Routix
I am using tasmota to switch on / off  a relay.
I have created a switch in app, subscribed to topic .../stat/POWER and added .../cmnd/POWER (with payload ON/OFF) to turn on or off the relay.
It works well but...
If the application is closed and another mqtt message (from pc or other app) changes the state of the relay, when I open MQTT Dash, the status of the relay is still the last one (because tasmota does not mark the message as retained)

Is there a way to send .../cmnd/POWER (without payload) at statrup, so tasmota will return the current status of the relay so it can correctly show that at switch status ?

Routix.net

unread,
Mar 22, 2021, 5:45:26 AM3/22/21
to Routix
Retained flag was designed in MQTT protocol to solve such issues.
Devices should utilize it when required and admire it.
Actually, this is not app's issue that the device does not use retained flag.
You need to modify device's firmware.
You can't send anything on start up from the app.

Dimitris

unread,
Mar 22, 2021, 5:56:12 AM3/22/21
to Routix
I wanted to avoid retained messages, but it seems that I have to enable them.
Thank you!

ste...@c88.org

unread,
Mar 22, 2021, 7:49:27 AM3/22/21
to Routix
From the Tasmota documentation:
Power state message can be sent with the retain flag set using PowerRetain 1

THEKK S.

unread,
Oct 26, 2021, 2:15:09 PM10/26/21
to Routix
Hi folks, I stumbled over the same issue. Unfortunaly this problem is not a problem of retained messages setting.
In the tiles, I publish to "cmnd/../POWERX" and subscripe to "stat/../POWERX".
If the app is not viewed, the display is off or the mobile phone is not in the network, the status in the app will not be updated by subscription.
I guess this is formely ok or a wanted behavior.

The real problem is, that the subscription is not refreshed if the app becomes visible again.
I tried to handle that by using app.publish with a wrong payload, for example app.publish('cmnd/plug_A1B2C3/POWER2', 'x', false, '0') to get a message with the current state on the 'stat/' topic.
But the APP is spaming the publishing method if I put it in the "on display" handler, what effectifely DDoS the mqtt broker.

A solution for this can be the call of the subscription once again for each tile in the onResume() method of the android app.
Maybe there is a better life cycle method now, I hasn't develop on adroid since a few years.

THEKK S.

unread,
Oct 26, 2021, 2:20:56 PM10/26/21
to Routix
Solved by myself (maybe there are more beautiful solutions):
ON DISPLAY handler:
if (event.getSecondsSinceLastActivity() > 60) {
    app.publish('cmnd/plug_A1B2C3/POWER1', 'x', false, 0);
    var now = new Date().getTime();
    while(new Date().getTime() < now + 500){ /* Do nothing */ }
}

The default sleep code will not work ;-( :
await new Promise(resolve => setTimeout(resolve, 500));
Reply all
Reply to author
Forward
0 new messages