Hello, I discovered HomeAssistant recently.
with HA, I build interface to monitor lamp from ESP8266 (truned into ESPduino) via MQTT platform.
But, I’m facing a problem with switch that automatically “turn off” after switch to “turn on”.
This is my script
after adding optimistic value to the script, the problem solved. But, I want to keep switch like a toggle,
not like a push button (in optimistic mode) :
You should use “optimistic : false” and confirm the state of your switch to HA with the “state_topic”. Without the confirmation, the switch (in HA) turns in its previous state. I’ve made a simple example with an esp8266.
Is it possible to look at your code? Did you try to capture the traffic with Wireshark or to use a MQTT client (MQTTfx for example) to subscribe to these two topics?
I’m sorry I cant capture image for publish/subscribe process using mosquitto now, because I left my project in the office. But I tried to check it last week, publish/subscribe process was going well in mosquitto command. When I published (send “1” to payload), the subscribed monitor gave me “Light On”/“Light Off” status.
I had this as HA was not getting a payload (0 or 1) back on that state_topic. If it does not, the switch will turn on then 1 second later, off again. Install MQTT.fx and subscribe to your topic /test/confirm to ensure the payload is coming back and exactly what it is then modify your yaml to match. Here my working code using a Sonoff and ESPEasy (R121).
Furthermore, if you add the following to the top of your yaml just under ‘homeassistant’ as I’ve shown, the switch will look so much better.
homeassistant:
# Add this to your existing configuration
# Only the `entity_id` is required. All other options are optional.
customize:
switch.sonoff3:
friendly_name: Sonoff 03 (floor)
icon: mdi:lightbulb-outline
assumed_state: false
Thanks for your big help xbmcnut, now I have change payload_on : “1” & payload_off : “0” and its working.
But I’m facing new problem, if I make more than one switch, how to handle it?
Is it possible to share your code in HA and esp? I want to know about topic arrangement to handle more than one switch.
Thanks before.
That part is actually pretty easy. Simply name your switches uniquely or follow the guide here for defining the best layout approach. As my yaml is small at the moment, I’m using the following:
Very very big thanks, I’ve tried & it works.
Now I want to add timer in HA, I found many example about it, but till now automation script like didnt work well. This is the script :
Sorry man, I’ve not done any automation as yet as I’m slowing building my assets and working on creating different files for everything as my house is a live install. Migrating from Vera so I have to be careful not to break anything or the wife will break me!
I read over this thread, and still didn’t have the solution I was looking for. I was having the same issue @xbmcnut was having, where when i loaded homeassistant, i was getting the state set on the mqtt topic, but the switch would not stay in the state I set.
What i found was that I need to have the state_topic and the command_topic the same. When i did that, it was able to read the state correctly, as well as setting. I only found this by SSHing into by Pi running HA, and subscribed to the topic, so i could monitor what going on.
And for making the switch look prettier as @xbmcnut suggested (i am using this as a switch for turning on or off notifications for my washing machine - project notes soon to come as i finish) here is the code under the customize in main HA root:
I am using a Sonoff device with the firmware provided by arendst/Sonoff-Tasmota project. Thanks to the tips I got from this topic I finally got the MQTT switch to work properly. I just wanted to share with you the steps I followed and I hope it helps.
Set optimistic to false, since you have a state_topic to tell you the actual state of the switch. The firmware allows you to set PowerRetain to “1” so the last state info will always be available.
In my case I realized the state_topic is case-sensitive - the command_topic is not. If you do not set the topic properly, you will not receive a valid status and the switch will keep turning off after you turn it on.
Set the payload_on and payload_off to be the exact value you receive when you subscribe to the state_topic. I used mosquitto_sub to check what I was receiving. Even though the command works with either “on” or “1”, the state_topic always sends “ON” as a response.