{"id":468,"date":"2018-09-12T07:00:52","date_gmt":"2018-09-12T11:00:52","guid":{"rendered":"http:\/\/www.thesmarthomehookup.com\/test_install\/?p=468"},"modified":"2023-02-08T17:02:31","modified_gmt":"2023-02-08T22:02:31","slug":"end-random-ghost-switching-for-good-by-fixing-your-retain-settings-in-tasmota-and-home-assistant","status":"publish","type":"post","link":"http:\/\/www.thesmarthomehookup.com\/test_install\/end-random-ghost-switching-for-good-by-fixing-your-retain-settings-in-tasmota-and-home-assistant\/","title":{"rendered":"End random &#8220;ghost switching&#8221; for good by fixing your retain settings in tasmota and home assistant."},"content":{"rendered":"<p><iframe width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/31IyfM1gygo\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen=\"\"><\/iframe><\/p>\n<p><strong>Tasmota Switches \u2013 Random On\/Off? Incorrect state reported?.<\/strong><\/p>\n<p>Today on the hookup we\u2019re going to take a detailed look at the MQTT retain settings in Tasmota and figure out why your tasmota device may not be acting the way you want it to.<\/p>\n<p>Whether you\u2019ve got sonoffs, or shelly\u2019s or some other type of tasmotized device, you\u2019ve probably had at least one experience where they didn\u2019t quite act the way you expected them to.&nbsp; I\u2019ve read dozens of posts on reddit and facebook groups about people experiencing \u201cghost switching\u201d where their tasmota device turns on or off unexpectedly.&nbsp; DrZzs made a video about hardware ghost switching, and while electrical interference is certainly a possibility, I\u2019m willing to bet that the vast majority of these random switching events are caused by a much more dark and sinister problem\u2026.<\/p>\n<p>User Configuration Error!<\/p>\n<p>Errors in MQTT configuration can cause very mysterious and annoying things to happen with your tasmota devices, but today we\u2019re going to pull back the curtains on those mysteries and get your tasmota devices set up correctly.<\/p>\n<p>The first thing we need to take a look are the three different MQTT topics used by tasmota.&nbsp; These topics are tele, which stands for telemetry data messages, which is where every 5 minutes by default, your sonoff will report all kinds of statistics statistics about uptime, vcc voltage, and relay state.&nbsp; Most people never pay attention to their tele messages since they are sent at an interval of 5 minutes instead of on demand.<\/p>\n<p>The state topic channel is stat, which is where your sonoff will publish any changes in attached devices.&nbsp; If the relay changes from off to on, it will immediately publish \u201cON\u201d to its state topic. &nbsp;The state topic is used by your home automation system to know the current state of your tasmota devices.<\/p>\n<p>The last topic is cmnd, which stands for command messages.&nbsp; This is the topic that your tasmota device actually responds to.&nbsp; If you want to turn your tasmota device on or off you\u2019ll need to send the correct payload to the cmnd topic.<\/p>\n<p>Each of these topics has the option to publish messages using a retain flag using commands in the tasmota console.<\/p>\n<p>If you\u2019re interested in learning about MQTT retain and QoS, I made a whole video talking about the basics of MQTT, but this video will cover the quick and dirty of retained messages as they pertain to tasmota.<\/p>\n<p>Your MQTT broker keeps a file called a \u201cpersistence file\u201d, in this simple file it keeps the last retained message for each MQTT topic that has ever been published to it.&nbsp; When a device like a node-mcu or a sonoff reboots and reconnects to the MQTT server all it needs to do is subscribe to the topics that are relevant and the MQTT server will send it the last retained message on each of those topics, updating the device on what\u2019s been happening while it was disconnected.<\/p>\n<p>Retain is really useful, but if not configured correctly it can be the root of the most common problem I\u2019ve seen on forums and facebook groups:&nbsp; \u201cMy tasmota device is randomly turning on in the middle of the night and scaring us to death\u201d.<\/p>\n<p>The underlying problem in these cases is most likely poor wifi connection, or a power surge, either of which can cause a tasmota device to reboot, and with rebooting comes resubscription to MQTT topics.&nbsp; If a retained message is not available on the cmnd topic, your sonoff will use an option called \u201cpowerOnState\u201d, which can be set to always turn the relay on after reboot, turn off after reboot, or use the last saved power state.&nbsp; That power state is stored in the non-volatile memory of the ESP8266 chip so it will persist through a reboot.<\/p>\n<p>However, if you\u2019ve ever configured any of your devices to publish a retained message to that cmnd topic, your experience will be different because your device is going to receive a new command the second it connects to your MQTT broker.&nbsp; Even if you removed that setting long ago, the retained messages are still on your broker, and will be resent any time a device connects.<\/p>\n<p>Lets look at a common way this issue plays out.&nbsp; I\u2019ve configured my home assistant MQTT switch to publish with a retain flag, but I haven\u2019t turned on any of the retain settings in tasmota. I\u2019ll turn my light on with home assistant, which populates the command topic with an ON retained message.&nbsp; Next I\u2019ll turn off my light using the push button, you can see in the tasmota console that this publishes a message of \u201cOFF\u201d to the command topic.&nbsp; But since it isn\u2019t a retained message it doesn\u2019t overwrite the previous \u201cON\u201d message that was sent by home assistant.<\/p>\n<p>If I unplug my sonoff and plug it back in, bam, it turns on, even though my powerOnState is set to \u201clast saved state\u201d, even though the last message published to that topic was \u201cOFF\u201d, and even though the light was off when I unplugged it.&nbsp; All because of that pesky retained message, it could have been published a year ago, and it will still come back to haunt you every time your device reboots until you overwrite it with another retained message.<\/p>\n<p>There are theoretically two ways to fix this issue:&nbsp; Either enable retained messages on all devices that are capable of controlling your tasmota device, or disable retained messages on all devices and clear the retained messages on your MQTT broker.&nbsp; I\u2019m going to suggest the later.<\/p>\n<p>I\u2019m going to be using Tasmota 6.2.1.2 in this video, but your experience should be pretty much the same if you\u2019re using a version of Tasmota released in the last 6 months or so.<\/p>\n<p><a href=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/retain-false.jpg\"><img decoding=\"async\" src=\"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-content\/uploads\/2018\/09\/retain-false-300x128.jpg\" alt=\"\" width=\"300\" height=\"128\"><\/a><\/p>\n<p>Lets start out by fixing your home assistant configuration, you\u2019ll need to make sure that your MQTT switches either have the entry, retain: false, or don\u2019t have a retain value at all (since the default retain value is false).<\/p>\n<p>Next you need to go into the tasmota console.&nbsp; Navigate to your tasmota device\u2019s IP and click on console in the main menu.&nbsp; &nbsp;Once you\u2019re in the console you\u2019ll need to type in <strong>switchretain off,<\/strong> and then you\u2019re going to type <strong>buttonretain on<\/strong>, followed by <strong>buttonretain off.&nbsp;<\/strong> The reason we did that is that you can see this results in tasmota sending a blank retained message to all the different command topics, which clears out any previously retained messages.<\/p>\n<p>Last you\u2019ll need to make sure your PowerOnState is correct.&nbsp; By default the PowerOnState is 3, which means it turns on to the last power state, if you\u2019ve changed it, you should change it back by typing <strong>PowerOnState 3<\/strong>.<\/p>\n<p>Now your sonoff should always restore it\u2019s previous state after reboot, no matter which device controlled it last.<\/p>\n<p>The second issue that I see consistently is with incorrect switch states being reported by home assistant after a restart.&nbsp; This issue has to do with the stat or state topic, which is where the tasmota device reports any changes in the state of the relay.&nbsp; The state topic is then used to tell home assistant if the switch is currently on or off.&nbsp; If you\u2019ve ever restarted home assistant and noticed that your switches are not in the correct position, then you need to adjust your powerretain settings.&nbsp; When power retain is on each change in state will result in a retained message published to the state topic.&nbsp; Any time home assistant restarts it will receive that last retained message containing tasmota\u2019s the current state immediately upon resubscribing to the state topic.<\/p>\n<p>To enable power retain, you just need to type <strong>powerRetain on<\/strong> into the tasmota console.<\/p>\n<p>The last common issue has to do with using tasmota as a door sensor to monitor the state of a garage door like in DrZzs sonoff SV video, or an interior door like in my sonoff door sensor video.&nbsp; The issue is that after a restart, home assistant won\u2019t show the correct state of the door until the door\u2019s state is changed. &nbsp;In this case we\u2019re going to fix this problem by turning <strong>SwitchRetain on<\/strong>.<\/p>\n<p>By using switchRetain we\u2019ll accomplish the same thing that we did with our home assistant switches, where upon restart home assistant will resubscribe to that command topic and receive the last retained message on that topic, which should be an accurate depiction of the current state of the door.&nbsp; In this case we don\u2019t need to worry about this messing with the state of the relay since we\u2019ll also be using a custom SwitchTopic to decouple our switch from the relay.<\/p>\n<p>To enable switchRetain in the event that you are using tasmota for a door sensor you can type \u201c<strong>switchRetain on<\/strong>\u201d into the tasmota console.<\/p>\n<p>The option also exists to publish telemetry messages with the retain flag using the SensorRetain on command, but I personally can\u2019t think of a legitimate reason to do so since this information is published every 5 minutes anyway.<\/p>\n<p>These 3 fixes should solve about 90% of the problems that I\u2019ve seen with tasmota on forums and facebook groups.&nbsp; Let me know down in the comments if I missed your issue, or if you have some other fringe case that wasn\u2019t addressed in this video.&nbsp; If you want to know more about MQTT settings like QoS and retain you should check out my video about how IoT devices communicate.<\/p>\n<p>A big thanks to my patreon supporters who allow me to make videos like this one where I\u2019m not really making anything.&nbsp; Most of the support for my channel comes from you guys using amazon affiliate links for the components that I use in my projects, but I didn\u2019t build anything today, I hopefully just helped you fix problems with devices that are already in your house.&nbsp; If you\u2019re interested in supporting my channel check out the links in the description.&nbsp; If you enjoyed this video, please consider subscribing, and as always, thanks for watching the hookup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tasmota Switches \u2013 Random On\/Off? Incorrect state reported?. Today on the hookup we\u2019re going to take a detailed look at the MQTT retain settings in Tasmota and figure out why your tasmota device may not be acting the way you want it to. Whether you\u2019ve got sonoffs, or shelly\u2019s or some other type of tasmotized [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2595,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-468","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"acf":[],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/comments?post=468"}],"version-history":[{"count":5,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/468\/revisions"}],"predecessor-version":[{"id":2350,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/posts\/468\/revisions\/2350"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media\/2595"}],"wp:attachment":[{"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/media?parent=468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/categories?post=468"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.thesmarthomehookup.com\/test_install\/wp-json\/wp\/v2\/tags?post=468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}