Sonoff Tasmota without MQTT or custom components

I am so happy that I flashed my Sonoff Basic RF devices with Tasmota, leaving behind the nightmare that was eWeLink behind a firewall. The very detailed instructions on the Tasmota Wiki took the pain out of the process, and totally liberated the devices.

Then I learned that Tasmota devices have the ability to be controlled very simply indeed using HTTP GET requests (like in your browser).

  • http://MySonoffIp/cm?cmnd=Power%20Off will return {"POWER":"OFF"}
  • http://MySonoffIp/cm?cmnd=Power%20On will return {"POWER":"ON"}
  • http://MySonoffIp/cm?cmnd=Power will query and return one of the two states above

credit - https://github.com/arendst/Sonoff-Tasmota/wiki/Commands#sending-commands-with-web-requests - NB this is NOT anything to do with the Http Binary Sensor integration on the REST API exposed by HA’s own web server.

Given this practical interface, I wanted to be a bit of a luddite and avoid the complexity of adding the whole MQTT layer. I have seen that there is a custom component for handling the Tasmota http but I also wanted to see if it could be done in pure configuration files.

Option 1

switch:
  platform: command_line
  switches:
    my_sonoff_switch:
      command_on:      "/usr/bin/curl -X POST http://192.168.12.34/cm?cmnd=Power%20On"
      command_off:     "/usr/bin/curl -X POST http://192.168.12.34/cm?cmnd=Power%20Off"
      command_state:   "/usr/bin/curl -X GET http://192.168.12.34/cm?cmnd=Power"
      value_template:  '{{value_json.POWER == "ON" }}'
      friendly_name:   "My Sonoff switch"

# credit - https://community.home-assistant.io/t/90157/54

Option 2 switch template with rest command

### The Rest_command:

Copy to clipboard
rest_command:
  fan_slow_on:
    url: 'http://192.168.12.34/cm?cmnd=Power2%20On'
  fan_slow_off:
    url: 'http://192.168.12.34/cm?cmnd=Power2%20Off'

### The Switch:

switch:
  - platform: template
    switches:
      fan_slow:
        value_template: "{{ is_state('switch.fan_slow', 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.fan_slow_script
        turn_off:
          service: rest_command.fan_slow_off

# credit https://community.home-assistant.io/t/42304

What else

In theory, if you were really that bothered, you could also do either or both the following on the tasmota devices:

  • disable MQTT
  • enable mDNS Discovery

Last words

I will end by saying that the overwhelming message I saw from the community whilst looking at ‘simple’ methods like this was:

Just install MQTT and it all just works

However, until I have more than a couple of Sonoff devices, I think a handful of config lines beats spinning up a new docker container, and learning about a whole new component.

2 Likes

Hm actually, setting up tasmota devices with mqtt requires 0 lines :joy::rofl:. All you need to set up is the broker. Actually, all you need to do is install it and remember the ip address. Tasmota has mqtt autodiscovery so no config lines are required to set up your devices using this method. Only a working broker is needed and obviously you have to add the broker to HA via integrations (if you set it up as standard all you need to put in the config is the ip address of the broker).

Then in your tasmota device (every single one of them) you go to Configuration>mqtt and put the ip address of your broker and change the topic name to something you can remember e.g. living_room_light. After that you go to the console of your tasmota device and type SetOption19 on. done!!!

You should be able to find all the related entities in your Home Assistant config now.
If it is a switch only you will get a few entities, if they have sensors you will get those automatically in your config as well.

E.g. a sonoff basic will pass through a few entities: obviously the switch itself (if you followed the example it will be switch.living_room_light) and you will get a battery sensor, a wifi sensor, an uptime sensor and some random information sensor.

If you would want all this in HA with your method you will have to write a lot of lines of code. This method only requires you to set up a broker and activate auto discovery on your sonoff.

Good luck!

Edit: I forgot to mention, that when adding devices this way, you do not need to restart Home Assistant!

Edit2: you only need to set up the broker once. If all went well you will never have to touch it again. Setting up new devices with tasmota would only require you to enter the broker ip in tasmota, the topic name and the SetOption19 command.

For example:

These are all tasmota devices, but I have nothing configured in switches. My switch.yaml (or configuration.yaml depending on where you set them up) is completely empty. Same goes for my LED controllers which are tasmota devices as well. I have nothing configured in lights regarding mqtt. All done automatically.

1 Like

Is there any message if device is not working? WiFi issue for instance?

It will just return as unavailable in HA,

These ones are actually ESPHome ones (as I have recently gone the ESPHome route. But tasmota gave the same message in HA.

For anyone who might be trying to use the curl command line option mentioned in the OP, you might be interested in a couple of improvements, which

  1. Simplify the binary command and ensure they work in all environments
  2. Add in resilience against failure in connectivity - When the devices (or the network) were unavailable the logs would fill with Command failed: curl [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined
switch:
  platform: command_line
  switches:
    my_sonoff_switch:
      command_on:      curl -s -m 5 -X POST 'http://192.168.12.34/cm?cmnd=Power%20On' 
      command_off:     curl -s -m 5 -X POST 'http://192.168.12.34/cm?cmnd=Power%20Off'
      command_state:   curl -s -m 5 -X GET  'http://192.168.12.34/cm?cmnd=Power' || echo -n {\"POWER\":\"OFF\"}
      value_template:  '{{value_json.POWER == "ON" }}'
      friendly_name:   "My Sonoff switch"

In my case I am fine with an error being logged if you try to turn on a device that is not connected, but if you want to suppress errors on command_on and _off then simply add either of the following to the end of the commands: || : or || true

1 Like

Thank you so much @artmg!

I’m a very very noob programmer, but despite this (using some online tutorial) I managed to flash my Sonoff Mini with Tasmota firmware (Sonoff Basic profile).

I thought the hard part was over, but immediately after installed Tasmoadmin add-on I realized
that there was no complete integration on Home Assistant Entities (like deCONZ for example) without activating a MQTT layer.

I don’t know what exactly MQTT or a Broker is, so I was bracing myself… But then I found this topic!
I added your lines in my switches.yaml and all is working like a charm:

  • No eWeLink
  • No Tasmoadmin needed
  • No MQTT layer

What else? :+1:

The only thing I notice (I don’t know if it can be improved), is if that I use the physical switch on my Sonoff, home assistant has some seconds of delay to update the switch status.

The principle of mqtt is simple though. See it like this: mqtt is a short message service where devices sends messages to the broker.

The broker is nothing more than a centralized information center (Like a wikipedia site). Device B asks the Broker if Device A is alive, the broker will ask Device A if it is alive. If Device A answers with a yes, the broker will answer Device B and tell it that Device A is alive.

Ofc this is a simplified version. Though I see no benefits in not using MQTT other than the extra point of failure (though after having used MQTT for a few years now I can safely say that failures are pretty rare, as in I never had them before).

Why use mqtt then? Well simply because it supports autodiscovery with Home Assistant. Adding devices like tasmota is super easy with auto discovery and mqtt and requires zero config within Home Assistant to make it work. All you’d need to do is setup mqtt on the tasmota device (usually only ip address is needed) and enable auto discovery. It will them import all the known sensors to that specific device automatically in to Home Assistant.

I’m glad this helps you @RikyUnreal
The downside of such a simple solution is that it relies on ‘polling’, periodically checking with the switch to see if it’s on or off. The Command Line Switch implementation does not seem to have an update_interval setting so I don’t know what controls how frequently this checking happens.

MQTT does not do ‘polling’. Device A sends a message, device B listens, and receives is as soon as A sends. I think your confusion comes from the fact that Tasmota sends (normally, can be changed) a tele messages indicating the device is still online.

Hi @francisp - if you check the topic name of this thread, the OP and the message I was replying to you’ll see that they all talk about ‘without mqtt’ - the solution being shared here DOES do polling and does NOT use mqtt

Actually, OP also stated that he did this because he didn’t know what MQTT is/does.
But really, all this shows us that there are now a few known ways to connect a (say sonoff) to HA. Either this method, the mqtt route or the ewelink custom-component which I think is great. Why? Well because choice is great!

Just wanted to share that this thread helped me utilize a esp8266 running tasmota, with a extremely cheap 315mhz transmitter attached to it, to control a variety of light switches that arent ‘automagically’ found in homeassistant. again, thank you.