New template fan question

Great release and like always, love how this moves forward!

How would one setup the new fan’s percentage? Tried a bit but because my fan has 6 speeds, meaning each speed is 16.66666% (100% / 6). How do you set that up. Tried a bit but the float part is causing my setup to not like it. any tips?

Below is what works but I have both preset_mode (which works) being shown and the percentage slider (which doesn’t work but I can’t find out how to remove).

Ideally I use only percentage.

platform: template
fans:
  livingroom_ceiling_fan:
    friendly_name: "Livingroom Ceiling fan"
    value_template: "{{ states('input_boolean.fan_state_livingroom') }}"
    #percentage_template: "{{ states('percentage_template.fan_speed_livingroom') }}"
    preset_mode_template: "{{ states('input_select.fan_speed_livingroom') }}"
    turn_on:
      service: script.livingroom_ceilingfan_on
    turn_off:
      service: script.livingroom_ceilingfan_off
    # set_percentage:
    #   service: script.livingroom_ceilingfan_percentage
    #   data:
    #     percentage: "{{ percentage }}"
    set_preset_mode:
      service: script.livingroom_ceilingfan_speed
      data:
        preset_mode: "{{ preset_mode }}"
    speed_count: 6
    preset_modes:
      - "0"
      - "1"
      - "2"
      - "3"
      - "4"
      - "5"
      - "6"```

and my script which changes speeds:

livingroom_ceilingfan_percentage:
  alias: "[LIVINGROOM] CeilingFan Percentage"
  mode: queued
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.broadlink_remote
        command: >-
          {% if percentage == 0 %}
            b64:skIcAAgPEw4TDhMOEw4TGQgZCBkJGQgZCQ4TGQkAAXwAAAAAAAAAAAAAAAA
          {% elif percentage == 17 %}
            b64:shYcAAkOFA0VDRQNFA4UGAoYChgJDhQYChgKGAkAAXwAAAAAAAAAAAAAAAA
          {% elif percentage == 34 %}
            b64:sh0cAAkOFA0UDRQOFA4UGAoYChkJDhQYCg4UGAkAAXwAAAAAAAAAAAAAAAA
          {% elif percentage == 51 %}
            b64:shocAAoNFQ0VDRQNFQ0UGAoYCg0VGAoYChgKGAoAAXsAAAAAAAAAAAAAAAA
          {% elif percentage == 68 %}
            b64:shocAAkOFA4UDhQOFA4TGQkYCQ4UDhQYCRkJGQkAAXwAAAAAAAAAAAAAAAA
          {% elif percentage == 85 %}
            b64:sh0cAAkNFA4UDhQOFA0UGAkOFBgKGAkZCQ4UGAoAAXwAAAAAAAAAAAAAAAA
          {% elif percentage == 100 %}
            b64:sigcAAoOFA4UDhQOFA4UGQkNFBgJGQkZCRgKGAkAAXwAAAAAAAAAAAAAAAA
          {% endif %}

divide the percentage by 100 and multiply by 6. You’ll end up with an integer. Make a modes list so you can access each mode with an index. I.e. item 1 in the list is accessed with 0 as an integer. Then build a safety check into accessing the mode from modes by giving it a default.

Here’s what it would look like. In this case modes[0] returns the fan at preset zero. You can change this to whatever as long as it’s 6 or below.

livingroom_ceilingfan_percentage:
  alias: "[LIVINGROOM] CeilingFan Percentage"
  mode: queued
  sequence:
    - service: remote.send_command
      data:
        entity_id: remote.broadlink_remote
        command: >-
          {% set mode = (percentage / 100 * 6) | round(0) | int %}
          {% set modes = [
            'b64:skIcAAgPEw4TDhMOEw4TGQgZCBkJGQgZCQ4TGQkAAXwAAAAAAAAAAAAAAAA',
            'b64:shYcAAkOFA0VDRQNFA4UGAoYChgJDhQYChgKGAkAAXwAAAAAAAAAAAAAAAA',
            'b64:sh0cAAkOFA0UDRQOFA4UGAoYChkJDhQYCg4UGAkAAXwAAAAAAAAAAAAAAAA',
            'b64:shocAAoNFQ0VDRQNFQ0UGAoYCg0VGAoYChgKGAoAAXsAAAAAAAAAAAAAAAA',
            'b64:shocAAkOFA4UDhQOFA4TGQkYCQ4UDhQYCRkJGQkAAXwAAAAAAAAAAAAAAAA',
            'b64:sh0cAAkNFA4UDhQOFA0UGAkOFBgKGAkZCQ4UGAoAAXwAAAAAAAAAAAAAAAA',
            'b64:sigcAAoOFA4UDhQOFA4UGQkNFBgJGQkZCRgKGAkAAXwAAAAAAAAAAAAAAAA',
            ] %}
          {{ modes[mode] if mode <= modes | length - 1 else modes[0] }}

You could also alter your script to not allow any mode above 6 by using a variable, then you won’t have a default.

livingroom_ceilingfan_percentage:
  alias: "[LIVINGROOM] CeilingFan Percentage"
  mode: queued
  variables:
    mode: >
      {{ (percentage / 100 * 6) | round(0) | int }}
    modes:
    - 'b64:skIcAAgPEw4TDhMOEw4TGQgZCBkJGQgZCQ4TGQkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:shYcAAkOFA0VDRQNFA4UGAoYChgJDhQYChgKGAkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sh0cAAkOFA0UDRQOFA4UGAoYChkJDhQYCg4UGAkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:shocAAoNFQ0VDRQNFQ0UGAoYCg0VGAoYChgKGAoAAXsAAAAAAAAAAAAAAAA'
    - 'b64:shocAAkOFA4UDhQOFA4TGQkYCQ4UDhQYCRkJGQkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sh0cAAkNFA4UDhQOFA0UGAkOFBgKGAkZCQ4UGAoAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sigcAAoOFA4UDhQOFA4UGQkNFBgJGQkZCRgKGAkAAXwAAAAAAAAAAAAAAAA'
  sequence:
    - condition: template
      value_template: "{{ mode <= modes | length - 1 }}"
    - service: remote.send_command
      data:
        entity_id: remote.broadlink_remote
        command: "{{ modes[mode] }}"

EDIT: You could even marry your 2 scripts together with 1 script that handles percentage or mode.

livingroom_ceilingfan_percentage:
  alias: "[LIVINGROOM] CeilingFan Percentage"
  mode: queued
  variables:
    mode: >
      {% if percentage is defined %}
        {{ (percentage / 100 * 6) | round(0) | int }}
      {% elif mode is defined %}
        {{ mode }}
      {% else %}
        0
      {% endif %}
    modes:
    - 'b64:skIcAAgPEw4TDhMOEw4TGQgZCBkJGQgZCQ4TGQkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:shYcAAkOFA0VDRQNFA4UGAoYChgJDhQYChgKGAkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sh0cAAkOFA0UDRQOFA4UGAoYChkJDhQYCg4UGAkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:shocAAoNFQ0VDRQNFQ0UGAoYCg0VGAoYChgKGAoAAXsAAAAAAAAAAAAAAAA'
    - 'b64:shocAAkOFA4UDhQOFA4TGQkYCQ4UDhQYCRkJGQkAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sh0cAAkNFA4UDhQOFA0UGAkOFBgKGAkZCQ4UGAoAAXwAAAAAAAAAAAAAAAA'
    - 'b64:sigcAAoOFA4UDhQOFA4UGQkNFBgJGQkZCRgKGAkAAXwAAAAAAAAAAAAAAAA'
  sequence:
    - condition: template
      value_template: "{{ mode <= modes | length - 1 }}"
    - service: remote.send_command
      data:
        entity_id: remote.broadlink_remote
        command: "{{ modes[mode] }}"
1 Like

Thank you very much Petro for providing this detail! This looks great and will try straight away!