Xiaomi wireless switch doesn't work

hi guys…i’m a new member of this community so i m a bit inexperienced…so i installed home assistant on my raspberry pi 2 following the guide and finally i added xiaomi gateway…in my dashboard i can see my smart plug and my binary sensor switch…so i make an automation in this way to start the plug after ther press of the button

- id: '1535033206648'
  alias: test
  trigger:
  - event_data:
      click_type: single
      entity_id: binary_sensor.switch_158d000223e3f5
    event_type: click
    platform: event
  condition: []
  action:
  - data:
      entity_id: switch.plug_158d00022db44d
    service: switch.toggle

obviously doesn’t work…the click event of the button is not seen from home assistant and the plug stay off…but if i active the automation from the dashboard the plug start so i suppose that the problem is the switch…maybe is not compatible?..i guess is the model WXKG01LM

I’m guessing it’s the order you have the lines in your automation (for example the data and entity shouldn’t be before the service line).

Here’s a working example from my own HA using the switch that should give you the idea:

- alias: Quick Click to toggle Kitchen Pi Radio
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d000xxxxxxx
      click_type: single
  action:
    service: media_player.media_play_pause
    data:
      entity_id: media_player.kitchen_pi_radio

thank you for the answer…i tried to use your code but it doesn’t work yet

In your action: stanza, the entity_id: line is indented too far, take it out to the same level as the service: line

if i move the line the system in the home assistant home page says to me that there is an error in the configuration…i noted that in general neither a simple automation with a timer works…for example a i made this with the automation tool:

- id: '1535188931884'
  alias: timer test
  trigger:
  - at: '11:30:00'
    platform: time
  condition: []
  action:
  - data:
      entity_id: switch.plug_158d00022db44d
    service: switch.toggle

so the syntax should be correct because is made from the tool…but nothing happend

ok…i tried a lot of different automation and the only component that doesn’t work is the switch…i fear that the problem is the compatibility (it is strange because the system see it)…the switch it’s always off…i don’t know maybe is there some setting to change?

Does the switch work in the gui? If you click the button on the switch itself, does the gui change?

Do you have the key in configuration.yaml? If you don’t you can only observe but not control.

@faro92, just to clarify, is the Xiaomi switch you have the Zigbee version, or the WiFi version? If it is a WiFi switch, you will need to extract the token and save that into a configuration file for it to work correctly. You will also need to make sure you have the token for the hub, and have that stored in your configuration file.

If you can see it in HA already though, it’s likely it is a Zigbee switch. Have you allowed permissions in the MiHome app so you can control it with external devices?

You need to select the select the Gateway from the devices list, click the ‘3 dots’ on the top right, select about, tap the version info down the bottom until a new option called “Hub Info” pops up, select that option, click the left button of the popup (probably in Chinese) and enable the wireless communication protocol. This is also where the Gateway key is stored.

I hope that might help.

Ok male some clarifications…the gateway is correctly configured in configuration.yaml with also the key (i followed the guide)…then i didn’ add other things in configurazione.yaml…i have to add also the switch?..in the gui i can see the binary sensor switch but the register show me that it always off…i don’t know what versione i have i only know that the model is WXKG01LM

Does the switch connect to the bridge or does it have its own wifi IP address in your router?

The WXKG01LM is a 1st generation button. It’s zigbee connected to the hub. Do you get any messages in the HA log?

the switch is connected to the gateway and there is nothing in my router about it so i think that it has not its own ip…the log file show me that messages

2018-08-27 07:08:38 WARNING (MainThread) [homeassistant.components.http] You have been advised to set http.api_password.
2018-08-27 07:15:13 ERROR (MainThread) [frontend.js.latest.201808200] :0:0 Script error.

I have the same problem. My Xiaomi switch worked fine until last ha change. Only long press show a state change to on at HA. I checked it at my Home and switch works fine ( single, double and long_click). Also I tried to make automation with HA UI with any result. I have the same code like faro92 and I ajusted lines in my yaml file, but switch still not working…

I have the same code and doesn’t works. I’d wrote code like faro92 and doesn’t works. In the beginning my code worked fine but with last switch ha changes not works. Seems signal switch is too short to be recognized by HA. Only long press change switch state.

Have a look at the code here:

There have been a few changes to how automations, and the xiaomi switches are used, could be of use to you.

Finally i founded switch problem. Really problem is not on switch. Problem is Yi light bulb do not toggle with light toggle, you must change it by homeassistant toggle instructions…after this change automation with switch single click works fine again.