Automation Switch Sonoff

Recently installed HA on a Raspberry Pi.
Now taking little baby steps into automation.

I installed MQTT and I managed to get a Sonoff switch on the Homepage which I can switch off and on.
Nice! But now I want more. :innocent:

How can I toggle the switch off or on automatic.
Let’s say OFF at 22:00

To my understanding, I have to edit the YAML file
I found a Automation tab in Configuration but I can’t get it to work.
So, edit the YAML file.
I came up with this:

switch SonoffBasic1:
  - platform: mqtt
    name: "Sonoff Switch01"
    icon: mdi:power-plug
    state_topic: "stat/NiRiSonoffBasic1/POWER"
    command_topic: "cmnd/NiRiSonoffBasic1/POWER"
    availability_topic: "tele/NiRiSonoffBasic1/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true
    automation:
        -alias: Switch off
        trigger:
        - at: '22:00:00'
        platform: time
        entity_id: switch.sonoff_switch01
    action:
        service: switch.turn_off
        entity_id: switch.sonoff_switch01

But (ofcourse) it is not working, otherwise I wouldn’t be here :kissing_heart:
Can somebody push me in the right direction?

Change state_topic: “stat/NiRiSonoffBasic1/POWER” to state_topic: “stat/NiRiSonoffBasic1/RESULT”
Also add: optimistic: true.
I’ll remove:
availability_topic: “tele/NiRiSonoffBasic1/LWT”
payload_available: “Online”
payload_not_available: “Offline”

the “automation:” section doesn’t go within the switch configuration.

It is a totally separate section and resides at the same level as the “switch:” section.

So you eventually need something like this:

switch:
  - platform: mqtt
    name: "Sonoff Switch01"
    icon: mdi:power-plug
    state_topic: "stat/NiRiSonoffBasic1/POWER"
    command_topic: "cmnd/NiRiSonoffBasic1/POWER"
    availability_topic: "tele/NiRiSonoffBasic1/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: true

automation:
  - alias: Switch off
    trigger:
      - at: '22:00:00'
        platform: time
        entity_id: switch.sonoff_switch01
    action:
      service: switch.turn_off
      entity_id: switch.sonoff_switch01
1 Like

If this is a switch, why the heck are you manually configuring it? I have 9 Sonoff/Tasmota devices for 3+ years all configured using MQTT discovery and never a glitch…

Hi David, I am totally new on Home Assistant and still learning
I just followed a tutuorial and I mananged to get my Sonoff working (with newly flashed software)
If you have a better solution, please teach me. Do you know a good tutorial, please share.

As of yesterday, it worked…but after an update of HA my device is not recognized any more :frowning:
I know have an error message on my Homescreen: Enitity not available