Adding Sonoff Basic R2 flashed with Tasmota to Home Assistant

I used a couple Sonoff Basics to automate my Christmas lights this year. I setup a few automations based on how we wanted them to turn on and off. We are going to walk through manually adding a Sonoff Basic R2 flashed with the Tasmota software without using auto discovery. Then we will setup a basic automation. If you haven’t already flashed and configured Tasmota you might want to checkout my article about that first.

Sonoff Basic R2 + Tasmota (no solder)
This is a walkthrough of flashing a Sonoff Basic R2 device with the Tasmota [https://github.com/arendst/Tasmota] firmware so that it can connect to an MQTT broker. I decided to write this because I wanted an easy way to automate my Christmas lights through Home Assistant. Doing this for the first t…

Switch Template:

The template is pretty straight forward, and you can reference the docs for adding an MQTT Switch and all the different configuration options that are available. You can copy the template below into config/configuration.yaml.

switch:  
  - platform: mqtt
    name: "Cord Switch"
    state_topic: "tasmota/stat/cord_switch/RESULT"  
    value_template: "{{ value_json.POWER }}"
    command_topic: "tasmota/cmnd/cord_switch/POWER"
    payload_on: "ON"
    payload_off: "OFF"
    availability_topic: "tasmota/tele/cord_switch/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
cord_switch.yaml

This switch template is assuming you used the same topic formatting in my other article. Be sure your topics match what you configured in the Tasmota web ui.

The Automation

This automation will turn on the lights in the morning around when I get up, then shut them off again around work. They then come on at night and shut off around bed time.

automation:
  - alias: "Christmas - Off Morning"
    trigger:
      platform: time
      at: "09:00:00"
    action:
      service: switch.turn_off
      entity_id: switch.cord_switch
  - alias: "Christmas - Off Night"
    trigger:
      platform: time
      at: "22:00:00"
    action:
      service: switch.turn_off
      entity_id: switch.cord_switch
  - alias: "Christmas - On Morning"
    trigger:
      platform: time
      at: "05:00:00"
    action:
      service: switch.turn_on
      entity_id: switch.cord_switch
  - alias: "Christmas - On Night"
    trigger:
      platform: time
      at: "17:00:00"
    action:
      service: switch.turn_on
      entity_id: switch.cord_switch
christmas.yaml

If you were using this automation for outdoor lights you might want to use the trigger for sunrise and sunset.

trigger:
  platform: sun
  event: sunrise

Wrapping Up

All thats left to do at this point is to check the config, and if there are no errors then restart Home Assistant. In the left nav click on Configuration and then Server Control. You’ll see this screen.

Once you’re here click the Check Config button and give it a minute. You should see a Configuration valid message. If not you will see errors telling you the lines that have the issue.

After the configuration is validated, click the restart button down at the bottom to restart Home Assistant.

Now you’re set to go with some automated Christmas lights.