Need help with Fan template + Scenes

How to choose dynamic scenes based on selected percentage value?

fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          scene: scene.office_fan_power
        turn_on:
          scene: scene.office_fan_speed_1
        set_percentage:
          scene: {{ Change scene based on percentage value selected }}
        speed_count: 5
fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          scene: scene.office_fan_power
        turn_on:
          scene: scene.office_fan_speed_1
        set_percentage:
          - scene: |
              {% set d = {
                0: 'scene.office_fan_power',
                20: 'scene.office_fan_speed_1',
                40: 'scene.office_fan_speed_2',
                60: 'scene.office_fan_speed_3',
                80: 'scene.office_fan_speed_4',
                100: 'scene.office_fan_speed_5'} %}
              {{ d[percentage] }}
        speed_count: 5
Configuration warnings
Invalid config for 'template' from integration 'fan' at configuration.yaml, line 26: Entity ID {% set d = {
  0: 'scene.office_fan_power' is an invalid entity ID for dictionary value 'fans->office_fan->set_percentage->0->scene', got "{% set d = {\n  0: 'scene.office_fan_power',\n  20: 'scene.twenty_percent_scene',\n  40: 'scene.forty_percent_scene',\n  60: 'scene.sixty_percent_scene',\n  80: 'scene.eighty_percent_scene',\n  100: 'scene.office_fan_speed_1'} %}\n{{ d[percentage] }}\n"

Thanks! appreciate your help
It throws warning

Try with the full action instead of the shorthand version:

        set_percentage:
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}

Thanks!!! It works
But it does not save the state; when i update the percentage of a fan from UI and close the window, it reset back to normal state

You need an additional action to set the value of the input number… you’ll need then for on and off as well:

fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        turn_off:
          - scene: scene.office_fan_power
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.state
        turn_on:
          - scene: scene.office_fan_speed_1
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.state
        set_percentage:
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}
          - action: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"

Still not working

You need to be more descriptive… What troubleshooting have you tried?

  1. Have you restarted Home Assistant?
  2. Is the fan entity created at all?
  3. Do any functions work?
  4. Are all the entity IDs correct?
  5. Do all the scenes work on their own?
  6. Are there any errors in the logs?

Functionality is working fine!! Fan is not storing the state
From Logs

Referenced entities input_boolean.state are missing or not currently available
Referenced entities input_number.percentage are missing or not currently available

See #4 in the post above.

Entities that start with input_ (sometimes known as Helpers) are user-created… if you didn’t create them, they don’t exist. If you created them, and their entity IDs don’t match what was used in the example configuration, change the configuration to match the entity IDs you have.

Thanks! I am able to do it for state but unable to do for percentage

Error

Template variable warning: dict object has no element Undefined when rendering '{% set d = { 0: 'scene.office_fan_power', 20: 'scene.office_fan_speed_1', 40: 'scene.office_fan_speed_2', 60: 'scene.office_fan_speed_3', 80: 'scene.office_fan_speed_4', 100: 'scene.office_fan_speed_5'} %} {{ d[office_fan_percentage] }}'
Template variable warning: 'office_fan_percentage' is undefined when rendering '{{ office_fan_percentage }}'

config

input_boolean:
  office_fan_state:
    name: Office Fan Power
    
input_number:
  office_fan_percentage:
    name: Office Fan Speed
    initial: 20
    min: 0
    max: 100
    step: 20


# Example configuration.yaml entry
fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.office_fan_state') }}"
        percentage_template: "{{ states('input_number.office_fan_percentage') | int }}"
        turn_off:
          - scene: scene.office_fan_power
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.office_fan_state
        turn_on:
          - scene: scene.office_fan_speed_1
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.office_fan_state
        set_percentage:
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[office_fan_percentage] }}
          - action: input_number.set_value
            target:
              entity_id: input_number.office_fan_percentage
            data:
              value: "{{ office_fan_percentage }}"
        speed_count: 5

Thanks brother!!! Done
Its working

input_boolean:
  office_fan_state:
    name: Office Fan Power
    initial: false
    
input_number:
  office_fan_percentage:
    name: Office Fan Speed
    initial: 0
    min: 0
    max: 100
    step: 20


# Example configuration.yaml entry
fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.office_fan_state') }}"
        percentage_template: "{{ states('input_number.office_fan_percentage') | int }}"
        turn_off:
          - scene: scene.office_fan_power
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.office_fan_state
        turn_on:
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.office_fan_state
          - action: input_number.set_value
            target:
              entity_id: input_number.office_fan_percentage
            data:
              value: 20
          - scene: scene.office_fan_speed_1
        set_percentage:
          - action: input_number.set_value
            target:
              entity_id: input_number.office_fan_percentage
            data:
              value: "{{ percentage }}"
          - action: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
              entity_id: input_boolean.office_fan_state
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}
        speed_count: 5

You went a little bit overboard on the last part… The variable percentage in the template fan is not the same as the object ID of your Input number, it’s its own thing:

input_boolean:
  office_fan_state:
    name: Office Fan Power
    
input_number:
  office_fan_percentage:
    name: Office Fan Speed
    initial: 20
    min: 0
    max: 100
    step: 20


# Example configuration.yaml entry
fan:
  - platform: template
    fans:
      office_fan:
        friendly_name: "Office fan"
        value_template: "{{ states('input_boolean.office_fan_state') }}"
        percentage_template: "{{ states('input_number.office_fan_percentage') | int }}"
        speed_count: 5
        turn_off:
          - scene: scene.office_fan_power
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.office_fan_state
        turn_on:
          - scene: scene.office_fan_speed_1
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.office_fan_state
        set_percentage:
          - action: scene.turn_on
            target:
              entity_id: |
                {% set d = {
                  0: 'scene.office_fan_power',
                  20: 'scene.office_fan_speed_1',
                  40: 'scene.office_fan_speed_2',
                  60: 'scene.office_fan_speed_3',
                  80: 'scene.office_fan_speed_4',
                  100: 'scene.office_fan_speed_5'} %}
                {{ d[percentage] }}
          - action: input_number.set_value
            target:
              entity_id: input_number.office_fan_percentage
            data:
              value: "{{ percentage }}"
       
1 Like