Xiaomi Wifi switch and Sonoff and yeelight control

Hello Everyone,

Iam very new to home assistant.
I have it setup on a RPi 3 running hassio

I have a Few Xiaomi devices and wanted to see how can i use them to do some automation at home
I was able to to add the Xiaomi gateway to home assistant and i am able to get it working with the proximity sensor and a Xiaomi Wifi switch.

What i want to achieve it to use the Xiaomi Wifi switch to turn on and off the yeelight bulb’s that i have added directly to home assistant.

I can add them to the Mi home application but i want to control then using home assistant.

Also i have 2 sonoff’s added in my Home assitant and want to know how to control then using the Xiaomi Wifi switch.

Don’t know the switches but unless there’s something specific for them in the component you’d need to write a script with the switch state as the trigger and turn_on/off or toggle as the action. This type of thing is a fundamental of the way HA works, getting everything to work together. Check the HA docs, it’ll tell you how to do it.

Hello Bobby

Could you give me sample config for the change of state and the how to add a trigger.
Sorry i very new to coding

Here’s a sample automation…

automation:      
  alias: Switch YeeLight On
  trigger:
    - platform: state
      entity_id: sun.sun
      to: 'below_horizon'
  action:
    - service: homeassistant.turn_on
      entity_id: light.yeelight

So, a couple of things about this automation, it will only trigger when the sun goes below the horizon - so if the sun is already down it won’t fire if you start HA up while the sun is already below the horizon. You will need to either supply the entity_id of either the yeelight or the wifi switch it’s plugged into - if discovered they will be in the states page in the side menu <> <---- use to access, the switch will look something like this switch.wifi_switch_158XXXXXXXXXX

Hope that helps.

Do you have those wall switches? Or the wireless smart switch? I have the wireless smart switch (the round one) and use it to turn on/off Hue Lights and Sonoff S20 smart sockets.

I can post my automation later, if you are interested?

Here is my automation for xiaomi round button and yeelight.

- id: '1000'
  alias: 'light on bathroom 07-21'
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_15
      from: 'off'
      to: 'on'
  condition:
    - condition: time
      after: '07:00:01'
      before: '21:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id:
        - light.bathroom
      data:
        brightness: 255

- id: '1001'
  alias: 'light on bathroom 21-07'
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_15
      from: 'off'
      to: 'on'
  condition:
    - condition: time
      after: '21:00:01'
      before: '07:00:00'
  action:
    - service: light.turn_on
      entity_id:
        - light.bathroom
      data:
        brightness: 15

Hi Everyone

Thanks a lot for trying and helping me

I have one of those Wifi Switches
Link : https://www.gearbest.com/alarm-systems/pp_610095.html

What i m interested is in getting the awitch to work with Yeelight and with a Sonoff That i have connected with a tubelight

Here are the entities

Tube light :
light.living_room_tubelight

Wifi Switch :
binary_sensor.wall_switch_right_158d000170c47b

here is my config file

light:

  • platform: yeelight
    devices:
    192.168.1.64:
    name: Lamp Shade
    192.168.1.68:
    name: Coridor
    192.168.1.66:
    name: Bedroom
    192.168.1.67:
    name: Entry
  • platform: mqtt
    name: “Living Room Tubelight”
    command_topic: “cmnd/sonoff/power”
    state_topic: “stat/sonoff/POWER”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    retain: true
  • platform: mqtt
    name: “Manyata Room Tubelight”
    command_topic: “cmnd/sonoff1/power”
    state_topic: “stat/sonoff1/POWER”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    retain: true

So an automation could look like that:

- alias: Turn on lights
  trigger:
    platform: event
    event_type: click
      event_data:
        entity_id: binary_sensor.wall_switch_right_158d000170c47b
        click_type: single
  action:
    - service: switch.turn_on
        entity_id: switch.living_room_tubelight
    - service: switch.turn_on
        entity_id: switch.manyata_room_tubelight
    - service: light.turn_on
      data:
        entity_id: light.lamp_shade
    - service: light.turn_on
      data:
        entity_id: light.coridor
    - service: light.turn_on
      data:
        entity_id: light.bedroom
    - service: light.turn_on
      data:
        entity_id: light.entry

You could then use “binary_sensor.wall_switch_left_158d000170c47b” to turn off the lights?! As far as I know, MQTT Switches (like the Sonoff) do not support the “toggle” option, like lights do???!??? So I’m not (yet) 100% sure how to handle a toggle command for MQTT Switches.

But I hope this will help to solve your problem - or at least give you some ideas?!

1 Like

Sonoff can do on, off or toggle, if you look at the tasmota Mqtt page you’ll see… cmnd/sonoff/power toggle … is an option.

Giving it a try right now
Will update you if it works or not.

Hi Daniel

It worked i was able to get the Switch to file the Yeelight with the following automation

  • alias: Turn on lights
    trigger:
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.wall_switch_right_158d000170c47b
    click_type: single
    action:
    • service: light.turn_on
      data:
      entity_id: light.lamp_shade

But how to i use the same switch again to turn off the light?
Now if the light is on and i try to press the same switch it does not do any thing.

Hey, I wasn’t sure if the Sonoff’s can toggle - that’s why I mentioned to use the other button to turn it off. But as Bobby_Nobble confirmed: They can toggle.

So instead of
light.turn_on / light.turn_off
just use
light.toggle

or switch.toggle for the Sonoff’s

1 Like

trying it out right now

works fine with the light but no so good with the sonoff
tried this config but no luck

  • alias: Turn on lights
    trigger:
    platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.wall_switch_right_158d000170c47b
    click_type: single
    action:
    • service: switch.toggle
      data:
      entity_id: light.living_room_tubelight

Any idea’s how to get it working with the Wifi Switch?

I think it has to be switch.xxxx instead of light.xxxx as the entiy id!

You don’t need data: either :slight_smile:

Not necessarily. But I like to put it in there in case I add other options later.

Giving it a try right now.