# How to send an initial MQTT message to get the status of a tasmota switch?

**URL:** <https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721>\
**Category:** General\
**Created:** [4 December 2021 21:01 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721 "2021-12-04T21:01:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![belgah](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@belgah](https://discourse.nodered.org/u/belgah)\
**Post date:** [4 December 2021 21:01 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/1 "2021-12-04T21:01:40Z")

</div>

Hi,  
I'm new to node red, but could at least implement a switch to a tasmota switch via MQTT.  
It worked after I change the switch.  
But due to the fact, that I've initially missed the last state, I'm searching for a mechaism to send an initial command (which is known) to the switch to get the current state.  
How is it possible in node red?

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [4 December 2021 21:43 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/2 "2021-12-04T21:43:51Z")

</div>

You could try this, replacing the name SPOW-02 with the name of your tasmota device (topic mqtt) and also de broker MQTT  
Press once on the injection node and the answer arrives on the debug node  
Nodes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/72ba7a56c0d961ea71f3c497e01e4fa58adc9313.png)

Flow:

```auto
[{"id":"9c507fdbc5f56a9f","type":"tab","label":"Tasmota ask status","disabled":false,"info":"","env":[]},{"id":"90a068732a3eeb9f","type":"mqtt out","z":"9c507fdbc5f56a9f","name":"Ask status","topic":"cmnd/SPOW-02/STATUS","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"84fa01dd79cb7065","x":490,"y":120,"wires":[]},{"id":"bbbd7e9d0e1d06f4","type":"inject","z":"9c507fdbc5f56a9f","name":"Ask status 8 to Sonoff","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":"1","topic":"","payload":"8","payloadType":"num","x":270,"y":120,"wires":[["90a068732a3eeb9f"]]},{"id":"2ded94c6faf4b4cb","type":"debug","z":"9c507fdbc5f56a9f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":200,"wires":[]},{"id":"fb245c79da2948bc","type":"mqtt in","z":"9c507fdbc5f56a9f","name":"Télémétrie SPOW-02","topic":"stat/SPOW-02/STATUS8","qos":"2","datatype":"json","broker":"84fa01dd79cb7065","nl":false,"rap":false,"inputs":0,"x":240,"y":200,"wires":[["2ded94c6faf4b4cb"]]},{"id":"84fa01dd79cb7065","type":"mqtt-broker","name":"test","broker":"192.168.10.10","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

```

Debug exemple:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d47dbd079475479f64d51dc8e509adb88d465401.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2021 22:00 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/3 "2021-12-04T22:00:00Z")

</div>

Make the switch topic Retained (that is configurable in tasmotta I think). Then when node-red starts up the broker will automatically give it the current state.  
Read this excellent tutorial on MQTT and it will tell you lots of useful stuff. [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/)

---

<div class="post-metadata">

**Author:** ![belgah](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@belgah](https://discourse.nodered.org/u/belgah)\
**Post date:** [4 December 2021 22:12 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/4 "2021-12-04T22:12:59Z")

</div>

Thx and yes, I've already read, that I can enable the retain flag.  
I just hopped that there is another way, something like a single shot timer after the startup of node red.  
Or a timer, which will be automatically started and which I can stop after receiving the response.  
Something like that.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 00:53 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/5 "2021-12-05T00:53:38Z")

</div>

Isn't there a _status_ message that is sent every now and then that gives you the condition of the device?  
(Sent from said device)

The problem I can see is if NR is not active and the switch changed its state..... Even if the state is stored in MQTT broker: it will then be wrong.  
Won't it?

I have done stuff with Tasmota switches but I am like you and still only just keeping myself above the water line of knowledge.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2021 07:31 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/6 "2021-12-05T07:31:55Z")

</div>

> [@belgah](#):
>
> I just hopped that there is another way

Why? What would another way do that a Retained topic does not?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2021 07:36 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/7 "2021-12-05T07:36:16Z")

</div>

> [@Trying\_to\_learn](#):
>
> if NR is not active and the switch changed its state..... Even if the state is stored in MQTT broker: it will then be wrong.  
> Won't it?

No, it won't be wrong. The switch will send it's new state to the broker whether node red is running out not.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 07:38 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/8 "2021-12-05T07:38:39Z")

</div>

Yes, but I meant if the MQTT BROKER is down..... I don't know if the broker is on the same machine as that which is being turned on/off.

Sorry for the confusion.

I apologise for saying NR in my post. (Crossed wires) 😉

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2021 07:48 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/9 "2021-12-05T07:48:58Z")

</div>

> [@Trying\_to\_learn](#):
>
> I meant if the MQTT BROKER is down

Ok, understood. On my systems I consider node red to be the master so I make the device command topic that controls the switch Retained too. The result is that when the device connects to mqtt the command gets sent and the switch is forced to the required state.  
If you want the state of the device to be the master then I seem to remember a setting in tasmota to publish the state when it connects, but I am not certain about that.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 07:54 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/10 "2021-12-05T07:54:06Z")

</div>

Yes, and I am sorry for the confusion.

I thought the example was an _all in one_ at this point.

Anyway. All good. Glad that confusion is/was resolved....

Well.... Is it?

Ok, as I see things:

The `switch` (Tasmota)  
`MQTT broker`  
`RasPie`

When the RasPie gets turned on, it needs to get the state of the switch and _sync_ with it.  
Yes, if the `retain` flag is set on the state of the `switch` then the RasPie should get a message telling it the state and then process the message to set the indication on the dashboard accordingly.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2021 08:06 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/11 "2021-12-05T08:06:45Z")

</div>

Under what circumstances would the pi be off long enough for the switch to be manually changed? I assume that would only happen in some sort of fault condition.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 08:08 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/12 "2021-12-05T08:08:34Z")

</div>

If it is their _test machine_......

Sorry, I am not seeing it in the same context as you. No problems.

I'll leave well enough alone.

---

<div class="post-metadata">

**Author:** ![belgah](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@belgah](https://discourse.nodered.org/u/belgah)\
**Post date:** [5 December 2021 09:52 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/13 "2021-12-05T09:52:41Z")

</div>

Yes, I am sure that activating the retain function should work. I'm going to test this out today.  
My question is rather general.  
As a C ++ developer, I just expect there to be something like initialization so that I can do things like turning on the retain flag if I haven't already.  
By the way. I'm not sure how long this state will be stored, ideally in the eeprom, but this is not documented. And I'm not running on a PI, but on a Windows server. But even there, updates can lead to reboots or whatever. And after the reboot, I'm in an unknown and possibly wrong state.  
I know I might be able to fix this with the retain flag.  
But I don't understand this loophole or requirement that one should get along without initialization.  
I will now check the timer nodes. If I can force a timer to be called every second or so, I'll be close to this type of initialization. If I find a way to turn the timer off again...

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [5 December 2021 10:00 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/14 "2021-12-05T10:00:25Z")

</div>

> [@Colin](#):
>
> Under what circumstances would the pi be off long enough for the switch to be manually changed? I assume that would only happen in some sort of fault condition.

Hmm. If a fault condition can arise, it will arise. (sods law?)  
And if a fault condition cannot possibly arise, it eventually will arise ("nothing can possibly go wrong with our nuclear reactor")

I see I am going to have to look into mqtt retained messages.  
With my Tasmota switches I send cmnd/device/status (as @Jean-Luc suggests) at intervals. If I don't get a response in n seconds I know something is wrong and I can change my dashboard display from an assertive "The heater is off" to a neutral "No data"

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [5 December 2021 10:05 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/15 "2021-12-05T10:05:22Z")

</div>

> [@belgah](#):
>
> I'm not sure how long this state will be stored

For ever, provided the MQTT broker is correctly configured. Mosquitto, for example, saves them to a file.

> [@belgah](#):
>
> I'm not running on a PI, but on a Windows server.

It makes no difference

> [@belgah](#):
>
> And after the reboot, I'm in an unknown and possibly wrong state

No, the broker will restore the retained topics from the disc (or wherever it has stored them).

> [@belgah](#):
>
> But I don't understand this loophole or requirement that one should get along without initialization.

There is initialisation, when a client gets connected to the broker it gets given the initialisation data. Why would you need to ask for it again later?

> [@belgah](#):
>
> I will now check the timer nodes. If I can force a timer to be called every second or so, I'll be close to this type of initialization.

What exact scenario are you trying to recover from that needs this?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 10:12 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/16 "2021-12-05T10:12:43Z")

</div>

> [@Colin](#):
>
> What exact scenario are you trying to recover from that needs this?

I guess they are just covering all the bases.... Not fully understanding all the workings of MQTT the question begs to be asked.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 10:13 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/17 "2021-12-05T10:13:54Z")

</div>

Aren't there messages that are sent by/from the `TASMOTA` device every now and then?

A `satus` message or something?

---

<div class="post-metadata">

**Author:** ![belgah](https://avatars.discourse-cdn.com/v4/letter/b/b77776/32.png) [@belgah](https://discourse.nodered.org/u/belgah)\
**Post date:** [5 December 2021 10:15 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/18 "2021-12-05T10:15:18Z")

</div>

> [@Colin](#):
>
> For ever, provided the MQTT broker is correctly configured. Mosquitto, for example, saves them to a file.

No, I don't mean the broker. That's clear. I mean the tasmota device.

> [@Colin](#):
>
> There is initialisation, when a client gets connected to the broker it gets given the initialisation data. Why would you need to ask for it again later?

I would like to have a node, which will be called automatically during the startup like the injector node, without the need of pressing a button manually (which is nice for testing but not usable for a setup-and-forget smarthome server).

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 10:20 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/19 "2021-12-05T10:20:16Z")

</div>

You may want to look at the `poweronstate`

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [5 December 2021 10:24 UTC](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721/20 "2021-12-05T10:24:26Z")

</div>

I just checked and I am seeing a `...../tele/STATE` message now and then and it tells me all about the device. If it is turned on/off and all the other stuff.

Would that be helpful for you to keep things in sync if things go awry?

[Next page](https://discourse.nodered.org/t/how-to-send-an-initial-mqtt-message-to-get-the-status-of-a-tasmota-switch/54721.md?page=2)
