eWelink integration doesn't work with switch.toggle - BUG - [Workaround]

Hello to all,
I’m new to this great community and would like to share a workaround for the eWelink integration bug:

When you try to create an automation with the eWelink Sonoff smart switch, eg. “Action:” call service → switch.toogle → entity (switch), the automation will not work, or rather it will not be able to find the switch. In the Home Assistant Log you can read this " Unable to find referenced entities switch.XXXXXX".The cause of this error is an integration bug and you can read the developer’s POV on their github

How to? You can call a simple script as a service in your automation:

Copy the script code into your scripts.yaml:

'1234567890123': #edit this key
  alias: Toggle eWelink
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: &device switch.XXXXXXXXX #change the switch ID (find it in state tab under developer tools)
        state: 'on'
      sequence:
      - service: switch.turn_off
        entity_id: *device
    - conditions:
      - condition: state
        entity_id: *device
        state: 'off'
      sequence:
      - service: switch.turn_on
        entity_id: *device

and in action of your automation.yaml:

  action:
  - service: script.turn_on
    target:
      entity_id: script.1234567890123 # copy here the script key
  mode: single

WARNING: Do not modify the script with the UI editor, because HA reformats the code and causes the script stop

I hope this can help someone…

Notes:
If the problem has already been discussed and solved, please apologize in advance for the uselessness of my post.
I’m not english, sorry for my bad grammar.

Tested on:
Home Assistant 2021.11.5

Alvaro

2 Likes
123: #edit this key
  alias: Toggle eWelink
  sequence:
  - choose:
    - conditions:
      - condition: state
        entity_id: &device switch.XXXXXXXX_1 #change the switch ID (find it in state tab under developer tools)
        state: 'on'
      sequence:
      - service: switch.turn_off
        entity_id: *device
    - conditions:
      - condition: state
        entity_id: *device
        state: 'off'
      sequence:
      - service: switch.turn_on
        entity_id: *device

I’ve basically copied your code word for word (except my switch ID), but when I try run the script I get “Failed to call service script/turn_on. Service not found”

it is explained in the add-on:

Troubleshooting

To solve Failed to call service xxxxx/xxxxxxx. Service not found. issue, use File editor to edit configuration.yaml. Append the following info to end of file:

switch:
  - platform: template
    switches:
      ewelink_virtual_switch:
        turn_on:
          service: switch.turn_on
        turn_off:
          service: switch.turn_off

cover:
  - platform: template
    covers:
      ewelink_virtual_cover:
        open_cover:
          service: cover.open_cover
        close_cover:
          service: cover.close_cover
        stop_cover:
          service: cover.stop_cover
        set_cover_position:
          service: cover.set_cover_position

fan:
  - platform: template
    fans:
      ewelink_virtual_fan:
        value_template: "{{ states('input_boolean.state') }}"
        turn_on:
          service: fan.turn_on
        turn_off:
          service: fan.turn_off
        set_preset_mode:
          service: fan.set_preset_mode

2 Likes

Great thank you, I clearly missed that. I found that my script was actually the problem too as I had 123, and not ‘123’

Initially I solved the problem following what you said.
However, now I found that switch.toggle doesn’t work, but switch.turn_on and switch.turn_off do.
Then you can use this command directly.