I know I've asked before but this is the next step/stage.
I use TASKER for my phone and apps.
I've installed the MQTT part for it and things seem to work. But not always.
The story:
At certain battery levels the phone sends messages to Node-Red.
(Note WiFi is not always on, but I'll get to that soon.)
There are x conditions:
Battery low - stage 1
Battery low - stage 2
Power connected
Batter full
Power removed.
Pretty easy.
Of course power can be connected/removed when ever.
And usually only stage 1 is invoked.
So:
The battery gets low.
"Battery low - stage 1" sent.
An LED is turned on. (Orange)
Power connected.
LED turns YELLOW.
Bater full
LED turns GREEN.
Power removed.
LED goes out.
Normally this works - in testing.
But not always.
What got me initially is that I have to turn on the phone's WiFi and wait 8 seconds before sending the message.
Fair enough I guess. Allows negotiation to happen, etc.
But the other night I connected power, the LED went YELLOW.
An hour or so later it went GREEN - Battery full.
A few minutes later I got up and removed the power.
LED didn't go out.
I invoked the phone to run the code again and it didn't go out.
This morning I did it and it did go out.
So there seems to be something else happening in MQTT, or the negotiation/comms side.
Sorry, I guess this is not the right place. It is probably more on the phone side of things.
But I have to start somewhere.
You haven't answered that. Just because the wifi is connected does not necessarily mean it has connected to MQTT. You could flash the LED when the LWT message indicates not connected (or something else, whatever you fancy).
What makes you think that will happen? I doubt that very much, more likely it will just get lost.
Also what QoS are you using when you publish the state. If you want to guarantee it gets through (when connected) then use QoS 1 or more.
I suggest that the best thing would be to repeatedly publish the state (in the phone) maybe every minute, then you know that it will always catch up when the connection recovers (if that is the problem).
You have used LWT messages previously haven't you? Just make sure the client on the phone has them enabled and subscribe to them in node-red. Then you get a message each time it disconnects and another when it connects.
True, though the node-red client allows configuration of a message on clean disconnect too. So it depends on the client. I agree, LWT may not be the full solution here.
@Trying_to_learn the simplest solution is to publish the message routinely every 30 seconds or 1 minute, or whatever you think appropriate.
Sorry. I have to ask to work out where the demarcation lines are.
I'll have to look at it form Tasker's perspective.
Colin,
I don't want to publish it every 30 seconds. That means the WiFi is on longer than needed.
It is off unless needed.
Having a low battery isn't conducive to have/leaving on the WiFi.
The code (tasker):
turn on the WiFi - to my home network,
waits 8 seconds.
Publishes message and beeps. (makes the phone make a noise just to help me know things are happening.)
waits 8 seconds.
turns off wifi.
Most of the time it works.
Just now and then it doesn't.
I may change it to 10 seconds.... or 16.... Something bigger.
But not wanting to go crazy and leave it on for a long time.
In order to check whether that is the problem change it to 60 seconds before and after. Once you determine if that is the problem then you can turn it down again. If that doesn't fix it then you know that is not the problem and can look for another explanation.
Edit: Does the phone beep in the situations where it fails?