Run script based on slider percentage

Does anyone know how to call a script based on a certain slider perentage? i have an RF fan integrated with boradlink and each speed is a script. So I would like to use a slider that calls each speed based on percentage range. This is what I have so far. icon changes just fine(someone else gave me that code), but I’m trying to incorporate the script and dont know how.


type: custom:stack-in-card
cards:
  - type: custom:mushroom-template-card
    entity: input_number.fan_slider
    icon: |
      {% set vol_level = states(entity) | float %}
      {% if vol_level == 0 %}
        mdi:fan
      {% elif vol_level > 80 %}
        mdi:volume-high
      {% elif vol_level < 40 %}
        mdi:volume-low
      {% else %}
        mdi:volume-medium
      {% endif %}
    trigger:
      platform: state
    action: |
      {% set vol_level = states(entity) | float %}
      {% elif vol_level > 80 %}
        service: remote.send_command
        data:
          device: cigar_fan
          command: 6
      target:
        entity_id: remote.wi_fi_universal_remote_remote
    icon_color: indigo
    primary: Cigar Exhaust
    secondary: |
      {% set vol = states(entity) | float * 1 %}
      {{ vol | round | int }}%
  - type: custom:my-slider
    entity: input_number.fan_slider
    radius: 12px
    height: 42px
    mainSliderColor: rgb(var(--mush-rgb-indigo))
    secondarySliderColor: rgba(var(--mush-rgb-indigo), 0.2)
    mainSliderColorOff: rgb(var(--mush-rgb-disabled))
    secondarySliderColorOff: rgba(var(--mush-rgb-disabled), 0.2)
    thumbHorizontalPadding: 0px
    thumbVerticalPadding: 0px
    thumbWidth: 0px
    card_mod:
      style: |
        ha-card {
          padding: 0px 11px 11px;
          --mush-rgb-disabled: 189,189,189;
          --mush-rgb-indigo: 63, 81, 181;
        }

Try copying your icon section to your action section and then replace the MDI lines with your action lines.

I don’t see anywhere in the Mushroom card docs that says you can incorporate an automation into a mushroom template card. The card allows the execution of actions based on tap, double-tap, or hold. You have two options:

  1. Create an automation that is triggered off the state of your input_number helper.
  2. Switch to a template number which incorporates automation actions into the entity itself.

Ok so I have my input_number. Let’s assume I’m able to figure out how to create the automation I need for a specific speed.

How do I tie that into a slider? That’s the part im having the most trouble with

Automation Basics
UI Automation Editor
Automation YAML

There are many ways to do what you want in an automation. Based on what you have posted, one way to do it would be something like the following… (make sure you insert the values for the commands in the variables section)

trigger:
  - platform: state
    entity_id: input_number.fan_slider
    for: "00:00:02"
condition: []
action:
  - variables:
      vol_level: "{{ trigger.to_state.state | float(0) }}"
      command: >
        {% if vol_level == 0 %}
        
        {% elif vol_level > 80 %}
          6
        {% elif vol_level < 40 %}
        
        {% else %}
        
        {% endif %}
  - service: remote.send_command
    data:
      device: cigar_fan
      command: "{{ command }}"
    target:
      entity_id: remote.wi_fi_universal_remote_remote

My God you solved it for
Me . I’m literally nauseous I’ve been working on this for so long lol

Thank you.

Happy to help… FWIW, you can probably lower the for: on the trigger to 1 second instead of 2. Having a for: defined helps keep the automation actions from executing too often when you are using the slider to change the value, but there’s always a balance to figure out between how “bouncy” and how “laggy” you are willing to have an automation act.

Cool cool thanks again

Before you go, please consider marking Didgeridrew’s helpful post with the Solution tag. It will help others find answers to similar questions.

For more information, refer to guideline 21 in the FAQ.

Please mark Didgeridrew’s post with the Solution tag (not my post).

My bad lol all set

Hey would you be able to show me your mushroom card code for this? Trying to do the exact same thing but struggling to figure out how I can have my 5 speed DC fan controlled via Broadlink use the slider

type: custom:restriction-card
restrictions:
  confirm:
    block: true
card:
  type: custom:stack-in-card
  cards:
    - type: custom:mushroom-template-card
      primary: Fan
      secondary: |
        {% set vol_level = states(entity) | float %}
        {% if vol_level == 1 %}
          Off
        {% else %}
        {% set vol = states(entity) | float * 1 %}
        {{ vol | round | int }}%
        {% endif %}
      icon: mdi:fan
      icon_color: |
        {% set vol_level = states(entity) | float %}
        {% if vol_level == 1 %}
          #696969
        {% else %}
          indigo
        {% endif %}
      entity: input_number.fan_slider
      tap_action:
        action: call-service
        service: script.cigar_fan_off
        data: {}
        target: {}
      card_mod:
        style:
          mushroom-shape-icon$: |
            ha-icon {
              --icon-animation: rotation {{ 5 / states(config.entity) | float * 10 }}s linear infinite;
            }
                @keyframes rotation {
              100% {
                transform: rotate(360deg);
              }
            }
    - type: custom:my-slider-v2
      entity: input_number.fan_slider
      styles:
        card:
          - height: 42px
        container:
          - border-radius: 12px
        track:
          - background: >
              [[[ if (entity.state == "off") return
              "rgba(var(--mush-rgb-disabled), 0.2)";
               else return "rgba(120, 120, 120, 0.2)";
              ]]]
        progress:
          - background: >
              [[[ if (entity.state == "off") return
              "rgb(var(--mush-rgb-disabled))";
               else return "rgb(var(--mush-rgb-indigo))";
              ]]]
        thumb:
          - width: 0px
      card_mod:
        style: |
          ha-card {
            padding: 0px 12px 12px;
            --mush-rgb-disabled: 189,189,189;
            --mush-rgb-indigo: 63, 81, 181;
          }

Hello. Disregard the restriction card part. It’s an exhaust fan that I can’t hear from a certain part of my house so I have this to prevent me from turning it on by accident.

Thanks so much James - keen to give this a try tonight

Happy to help :slight_smile:

Hey James,
I’ve replaced ‘script.cigar_fan_off’ to ‘script.master_fan_power’ (because that’s my RF script for fan power)
and removed the top stuff re exhaust fan, however while I’m not getting errors in code, I’m just getting a blank mushroom card when I save it. Did you do anything else outside of creating the mushroom fan card to get this to work?

Did you create the input_number entity, under helpers? That’s the only thing I can think of that will be preventing this from working aside from a typo.

Oops. No, thanks!

Ok, Ive now got the slider working and my input_number.fan slider automation set up, however every time I move the slider on my dashboard i get an error of: failed to call service fan/set percentage
is the percentage slider now deprecated with an update HA? or its more likely Ive got something wrong