Help with Sonoff Tasmota in Home Assistant

Dear community, I’m having troubles to configure my Sonoff Tasmota in Home Assistant, but most probably I’m missing some basic knowledge.
Some Info:
IP HA: 192.168.2.36
IP SONOFF: 192.168.2.46

Here is the Sonoff Tasmota config (FW: 5.12.0):
Image%20001

So, in Host (domus1) I setup the HA IP

Here is the configuration.yaml as broker is setup the tasmota IP:

mqtt:
  broker: 192.168.2.46
  port: 1883
  client_id: Sonoff_POW
  keepalive: 60
  username: mqttuser
  password: 12345
  protocol: 3.1
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true
switch:    
  - platform: mqtt
    name: "Sonoff POW"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/power"
    qos: 0
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    retain: true

And this is the result:

2018-04-15 09:49:13 ERROR (MainThread) [homeassistant.components.mqtt] Failed to connect due to exception: [Errno 111] Connection refused
2018-04-15 09:49:13 ERROR (MainThread) [homeassistant.setup] Setup failed for mqtt: Component failed to initialize.
2018-04-15 09:49:17 ERROR (MainThread) [homeassistant.setup] Unable to setup dependencies of switch.mqtt. Setup failed for dependencies: mqtt
2018-04-15 09:49:17 ERROR (MainThread) [homeassistant.setup] Unable to prepare setup for platform switch.mqtt: Could not setup all dependencies.

Could someone advise?
Thank you
Lucas

I have tasmota on 10 sonoff no problem

first I would do is install MQTTfx on a differance computer and set that to watch your topic.

stat/# will setup a wide card to the stat topic

your topic look good
so have you set tasmota topic

just did a read

this is MY MQTT

mqtt:
  broker: localhost
....
....

think its your MQTT setting
if its on the same computer as HA try using localhost

drop the password bit until you get it working thats what i did

I tried to setup broker as localhost but with the same result, damn! Maybe I’m missing something.
The concept behind the configuration should be:
Sonoff Tasmota try to connect to HA using port 1883, but that port is not in listening on HA, and on tasmota console I can see:
10:35:00 MQT: Connection failed to 192.168.2.36:1883

myle, if your configuration is working, could you please post it? I mean mqtt and switch configuration in configuration.yaml and sonoff one. Thank you.

my MQTT in

HA


mqtt:
  broker: localhost

one of my switchs

  - platform: mqtt
    name: "Hall light"
    state_topic: "stat/SONOFF10/POWER"
    command_topic: "cmnd/SONOFF10/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    qos: 0
    retain: true

HA and MQTT have the same ip address 192.168.1.240

hope this help

if not work un install it reboot re install reboot
that is switch off then on again

1st problem Small letters for power in command and CAPS in the state mine in CAPS

2nd is MQTT on the HA machine both are on the same machine a

did you use the HA version

image

DAMN, my bad! I completely missed the part on /etc/mosquitto/mosquitto.conf so most probably mosquitto listened on different port.

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Now finally the sonoff tasmota is cannnected to HA (HA starts without errors) and the GUI show the switch, problem is that if I press that switch, nothing work. I switch it on, and after a while it auto switch off itself… This is my final config:

mqtt:
  broker: localhost
  port: 1883
  client_id: Sonoff_POW              
  keepalive: 60         
  username: mqttuser
  password: 12345
  protocol: 3.1
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1             
    retain: true              
  will_message:            
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1        
    retain: true

switch:    
  - platform: mqtt
    name: "Sonoff POW"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/POWER"            
    qos: 0                       
    payload_on: "ON"   
    payload_off: "OFF"
    optimistic: false
    retain: true

What I seen is that Sonoff connect and disconnect continuosly from/to HA, so I got a lot of TIME_WAIT

root@raspberrypi:/home/homeassistant/.homeassistant# netstat -na | grep 1883
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN     
tcp        0      0 192.168.2.36:1883       192.168.2.46:7257       ESTABLISHED
tcp6       0      0 :::1883                 :::*                    LISTEN     
tcp6       0      0 ::1:1883                ::1:33969               TIME_WAIT  
tcp6       0      0 ::1:60687               ::1:1883                TIME_WAIT  
tcp6       0      0 ::1:50289               ::1:1883                TIME_WAIT  
tcp6       0      0 ::1:1883                ::1:46851               TIME_WAIT  
tcp6       0      0 ::1:53939               ::1:1883                TIME_WAIT  
tcp6       0      0 ::1:47959               ::1:1883                TIME_WAIT  
tcp6       0      0 ::1:54517               ::1:1883                TIME_WAIT

You seem to have the same client_id for both your MQTT broker (as set in the configuration.yaml) and the Sonoff Pow. They are both set to Sonoff_POW. This way lies madness.

I strongly suggest that you leave the Sonoff at its default: DVES_%06X

Thank you PianSom, now my connection between Sonoff and HA via MQTT is stable:

root@raspberrypi:/home/homeassistant/.homeassistant# netstat -na | grep 1883
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN     
tcp        0      0 192.168.2.36:1883       192.168.2.46:26225      ESTABLISHED

Also Sonoff GUI show me correct connection.

Btw, the switch still doesn’t work :frowning: I switch on, nothing happen, and then it auto switch off and from log I only got:

2018-04-15 16:14:35 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service_data=qos=0, payload=ON, retain=True, topic=cmnd/sonoff/POWER, service=publish, service_call_id=1976674064-36>

WAIT!WAITWAIT! It works! :smiley:
This is what’s wrong in my config. From Tasmota console we can see the right code (switching ON/OFF manually):

16:22:28 MQT: stat/Sonoff_POW/POWER = ON
16:22:32 MQT: stat/Sonoff_POW/RESULT = {“POWER”:“OFF”}

Then, these specific values must be configured also on switch part:

  - platform: mqtt
      name: "Bagno"
      state_topic: "stat/Sonoff_POW/POWER"      
      command_topic: "cmnd/Sonoff_POW/POWER"       
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

Thanks all for driving me to the solution! :wink:

all good

good here you got it fix now its just a copy and paste and change the topic for the next and next sonoff’s

Glad it worked.

You may also want to add an availability topic to your switch configuration - so you can see if there any problem with communication. Like this -

  availability_topic: "tele/Sonoff_POW/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"

Personally, I also like typing PowerRetain on at the Sonoff console, so that the actual power status is retained by the MQTT broker across, for example, power outages.

Thank you, added retain part, and I already issue the PowerRetain on command.
Plus, I added some useful sensor with infos coming from Sonoff

  - platform: mqtt
    name: "Sonoff_POW energy"
    state_topic: "tele/Sonoff_POW/ENERGY"
    value_template: "{{ value_json.Today }}"
    unit_of_measurement: "kWh"
  - platform: mqtt
    name: "Sonoff_POW power"
    state_topic: "tele/Sonoff_POW/ENERGY"
    value_template: "{{ value_json.Power }}"
    unit_of_measurement: "W"
  - platform: mqtt
    name: "Sonoff_POW voltage"
    state_topic: "tele/Sonoff_POW/ENERGY"
    value_template: "{{ value_json.Voltage }}"
    unit_of_measurement: "V"

BTW, For now I didn’t get any measurement on HA GUI, that’s strange :thinking:

remeber to get the energy read the switch MUST be ON and a Current passing thou it

myle, I don’t know what you mean, however, the Sonoff is already on, and on its console I can see:

08:11:32 MQT: tele/Sonoff_POW/SENSOR = {“Time”:“2018-04-16T08:11:32”,“ENERGY”:{“Total”:0.015,“Yesterday”:0.005,“Today”:0.010,“Period”:1,“Power”:22,“Factor”:0.60,“Voltage”:214,“Current”:0.169}}

So, you see? Voltage, Today and Power values are correctly reported by Sonoff, but these values are not present on HA. :thinking:

Image%20001

as the data you are trying to read is in the 2rd open { which is the ENERGY

so you have to change your
value_template: “{{value_json[‘ENERGY’].Power }}” and so on

Thank you for reply, but still no data on HA even with the following config :thinking:

- platform: mqtt  
  name: "Consumo Oggi"  
  state_topic: "tele/Sonoff_POW/ENERGY"  
  value_template: "{{ value_json['ENERGY'].Today }}"  
  unit_of_measurement: "kWh"  
- platform: mqtt  
  name: "Consumo Totale"  
  state_topic: "tele/Sonoff_POW/ENERGY"  
  value_template: "{{ value_json['ENERGY'].Total }}"  
  unit_of_measurement: "kWh"  
- platform: mqtt  
  name: "Voltaggio"  
  state_topic: "tele/Sonoff_POW/ENERGY"  
  value_template: "{{ value_json['ENERGY'].Voltage }}"  
  unit_of_measurement: "V"  
- platform: mqtt  
  name: "Consumo in Watt"  
  state_topic: "tele/Sonoff_POW/ENERGY"  
  value_template: "{{ value_json['ENERGY'].Power }}"  
  unit_of_measurement: "W" 

And Sonoff continues to report on its console the correct values:

09:22:14 MQT: tele/Sonoff_POW/STATE = {“Time”:“2018-04-16T09:22:14”,“Uptime”:“0T00:02:15”,“Vcc”:3.139,“POWER”:“ON”,“Wifi”:{“AP”:1,“SSId”:“CSIWIFI”,“RSSI”:100,“APMac”:“XXXXXXXXXXXXX”}}
09:22:14 MQT: tele/Sonoff_POW/SENSOR = {“Time”:“2018-04-16T09:22:14”,“ENERGY”:{“Total”:0.023,“Yesterday”:0.005,“Today”:0.018,“Period”:0,“Power”:21,“Factor”:0.70,“Voltage”:210,“Current”:0.144}}

WAIT! Got it!
state_topic: "tele/Sonoff_POW/SENSOR"
This is the right Topic, now it works! Thank you again for your time! :wink:

nice

now its just a copy paste for the next one .

its good when the light (powder) comes on LOL

Could anyone help me? I’m having a total mind block. I set up my Sonoff switches ages ago and now can’t work out to fix this issue, I’ve just had to reboot my internet, so I’m assuming that all of the switches ip address’s have changed, now they don’t work in HA, how do I fix this?