Template Fan Speed Script and Remove Oscillation Option

I have a DC ceiling fan.
It has 5 speeds.
I have set it up in Home Assistant using a Template Fan to configure it.
The template uses scripts which instruct a Broadlink RM4 Pro to fire the RF commands to control the fan.
It’s almost perfect but I have a couple of things I’m stuck on.

  • I can control the speed of the fan using the slider BUT when I set it to Zero% no command is fired off from my RM4 Pro
  • I would like to be able to remove the Oscillation option from my fan’s config so the oscillation switch/option doesn’t show up in the controls
  • When I turn the fan on, the switch toggles on for a moment and then toggles back off, though it does actually send the power command (single command for an/off), so I can’t see the current status reflected in HA, I’m guessing this requires optimistic mode or something but can’t quite figure it out
  • When I open the fan controls I have Two icons that reflect Two directions, my fan uses the same code to switch directions rather than Two separate codes where One is for each direction. I’d like to replace these 2 icons with a single icon that reflects the current direction.

I hope I have made everything clear.
Following is the code I used to configure this fan.

The fan:

- platform: template
  fans:
      living_room_fan:
        friendly_name: "Living Room Fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        preset_mode_template: "{{ states('input_select.preset_mode') }}"
        oscillating_template: "{{ states('input_select.osc') }}"
        direction_template: "{{ states('input_select.direction') }}"
        turn_on:
          service: script.lr_fan_power
        turn_off:
          service: script.lr_fan_power
        set_direction:
          service: script.lr_fan_direction
        set_percentage:
          service: script.lr_fan_set_speed
          data:
            percentage: "{{ percentage }}"
        speed_count: 5

The speed control script:

alias: lr_fan_set_speed
sequence:
  - service: remote.send_command
    target:
      entity_id: remote.rm4_pro_remote
    data:
      device: Living Room Fan
      command: |-
        {% if percentage == 0 %}
          b64:scAGAWqeBgAAAVcABaUJvB4KHQsdCgoeCR4KHgkeHgodCx0LHAsdCx0LHAwcCx0LCR8IHwkeCh4JHwkeHQsdCwm8HQsdCh4KCR8JHgoeCR8dCh0LHQscCx0LHQscDBwLHQsJHgofCB4JHwkeCh4dCx0KCrwdCx0KHQsJHgoeCR8JHh0LHQscCx0LHQscDBwLHQscCwkfCB8JHgkfCR8IHx0KHQsJvB0LHAsdCwkeCR8JHgkeHQsdCxwLHQscCx0KHQscDBwLCR4JHwkeCR8JHgkfHAsdCwm8HQodCx0LCR4JHgoeCR4dCx0KHQsdCxwLHQscCx0MGwsJHwgfCR4JHwkeCR8cCx0ABdw=
        {% elif percentage == 20 %}
          b64:scAAAaaeBgAJvB4JHgodCgoeCh0KHgoeHQodCx0KHQsdCxwLHQscDBwLCR4JHwkfCB8JHxwLCR4dCwm9HQodCx0KCR8JHgoeCR8dCh0LHAsdCxwMHAsdCxwLHQsIHwkfCR4JHwkeHQsJHh0LCbwdCx0LHAsJHwkeCR8JHh0LHQodCx0KHQsdCxwLHQscDAgfCR4JHwkeCh4dCwkeHQsJvB0LHQodCwkeCh4JHgoeHQscCx0LHAsdCxwMHAscDB0KCh4IIAgfCR4JHxwLCR8cCwm+HAodCx0LCR4JHwkeCR8dCh0LHAsdCx0LHAsdCxwLHQsIHwkeCh4JHwkeHQsJHh0ABdw=
        {% elif percentage == 40 %}
          b64:scB6ALqeBgAHHwkeCR8JHh0LCR4dCwkeCr0cCx0KHQsJHgoeCR4KHh0KHgodCx0KHQsdCxwLHQscDAgfCR8IHwkfHAsJHxwLCR8JvB0LHQoeCgkfCR4JHwkeHQsdCh0LHQscCx0LHAwcCx0LCB8JHwkfCB8dCwgfHQoJAAXc
        {% elif percentage == 60 %}
          b64:scAGAZKeBgAAAVgABXIJvB0KHgodCgodCh4KHgkeHgodCh0LHQodCx0LHAsdCxwLCR8IHwkeCh4JHxwLHQsJHgm9HQodCx0KCR8JHgkfCR4dCx0KHQsdCh0LHAsdCxwLHQsJHgkfCR4JHwkeHQscCwoeCb0cCx0LHAsJHwkeCR4KHh0LHQoeChwLHQsdCh0LHQscCwkfCR4JHwkeCR8cCx0LCR4JvB4KHQscCwkfCR4JHwkeHQsdCh0LHQodCx0LHAsdCxwLCR8JHgkfCR8IHx0LHAsJHwm9HAsdCh0LCR4KHgkeCh4dCx0KHQsdCxwLHQscCx0LHAwIHwkeCR8JHwgfHQscCwkABdw=
        {% elif percentage == 80 %}
          b64:scAAAbqeBgAKux4JHgodCwkeCh4JHgodHgodCh4KHQodCx0LHAsdCxwLCR4JHwkeCR8dCgkfCR4dCwm8HQodCh4KCR4KHgkeCh4dCh0LHQodCxwLHQscCx0LHAsJHwgfCR4JHx0KCR8JHh0LCbwdCxwLHQoKHQoeCR4KHh0KHQsdCh0LHAsdCxwLHAwcCwkeCR8IHwkeHQsJHgkfHAsJvB0KHQsdCwkeCR8JHgkeHQsdCh0LHAsdCxwLHQscCx0LCB8JHgkfCR4dCwkeCR8cCwm9HAsdCh0LCR4JHwkeCR8cCx0LHAsdCh0LHAsdCxwLHQsIHwkeCR8JHh0LCR4JHxwABdw=
        {% elif percentage == 100 %}
          b64:scD+ALCeBgAdCh0LHQoJHgoeCR4KHh0KHQodCx0KHQscCx0LHAscCwkfCB8JHgkfCR4JHh0LCR4JvRwKHQsdCgoeCR4JHgoeHQodCx0KHQscCx0LHAscCx0LCB8JHwgfCR4JHwkeHQoJHwm8HAsdCh0LCR4JHgkeCh4dCh0KHQsdCh0LHAsdCxwLHAsJHwgfCR4JHwkeCR4dCwkfCLwdCh0KHgoJHwkeCR4JHx0KHQscCx0KHQscCx0LHAscDAgfCR4JHwkeCR4JHxwLCR8IvB0LHAsdCwkeCR8IHwkeHQscCx0KHQscCx0LHAsdCxwLCB8JHwgfCR8IHwkeHQsJAAXc
        {% endif %}
mode: single

You don’t have to include oscillation into your template (I’m relatively certain of this) so if you don’t include it then it won’t show.

As for setting things to 0 not sending a command, that is the same as OFF, so your off command should be firing.

With the initial power on also sending a power off, is it possible that your base 64 string is sending a repeat? If you trained your RM with the actual remote and the remote sends repeats that may be the problem. You should be able to test this by using the remote and holding the button down and see if it changes anything. You can also try re-training the RM by just quickly single tapping that power button.

As far as the multi-directional icon, you can achieve this using an icon template in your Lovelace, sort of like this that I use for upload/download and a Button Card from HACS:

type: 'custom:button-card'
tap_action:
  action: more-info
entity: sensor.qb
name: qb
show_state: true
state:
  - value: downloading
    color: 'rgb(51, 255, 51)'
    icon: 'mdi:arrow-left-bold'
  - value: uploading
    color: 'rgb(255, 25, 25)'
    icon: 'mdi:arrow-right-bold'
  - value: up_down
    color: 'rgb(0, 68, 204)'
    icon: 'mdi:arrow-left-right-bold'