Can’t execute script in package

I am trying to create a package that translates my simple ceiling fan with three speeds (via remote) as a HomeAssistant-fan.
I have created a fan template that controls the fan via various scripts. Unfortunately these scripts are not found.

Error when calling the service fan/turn_on. Unable to find service script.wohnzimmer_fan_switchon

But the script exists. I’ve been tearing my hair out for two weeks now. Can anyone help me? What’s wrong with this package?

# Keep track of the fan state
input_boolean:
  wohnzimmer_fan_state:

# Keep track of fan speed
input_number:
  wohnzimmer_fan_percentage:
    name: Wohnzimmer Ventilator Prozent
    min: 0
    max: 100

fan:
  - platform: template
    fans:
      wohnzimmer_fan:
        friendly_name: "Wohnzimmer Ventilator"
        unique_id: wohnzimmer_fan
        value_template: "{{ states('input_boolean.wohnzimmer_fan_state') }}"
        percentage_template: "{{ states('input_number.wohnzimmer_fan_percentage') }}"
        turn_on:
          service: script.wohnzimmer_fan_switchon
        turn_off:
          service: script.wohnzimmer_fan_switchoff
        set_percentage:
          service: script.wohnzimmer_fan_set_speed
          data:
            percentage: "{{ percentage }}"
            
script:
    wohnzimmer_fan_switchon:
      alias: Ventilator einschalten
      sequence:
      - alias: Ventilator auf low schalten
        service: wohnzimmer_fan_low
      - alias: Status auf on setzen
        service: input_boolean.turn_on
        target:
          entity_id: input_boolean.wohnzimmer_fan_state
      - alias: Prozent auf 33 setzen
        service: input_number.set_value
        target:
          entity_id: input_number.wohnzimmer_fan_percentage
        data:
          value: 33
      mode: single
      
    wohnzimmer_fan_switchoff:
      sequence:
      - alias: Ventilator ausschalten
        service: wohnzimmer_fan_off
      - alias: Status auf off setzen
        service: input_boolean.turn_off
        target:
          entity_id: input_boolean.wohnzimmer_fan_state
      - alias: Prozent auf 0 setzen
        service: input_number.set_value
        target:
          entity_id: input_number.wohnzimmer_fan_percentage
        data:
          value: 0
      mode: single
      
    wohnzimmer_fan_set_speed:
      sequence:
      - service: input_number.set_value
        target:
          entity_id: input_number.wohnzimmer_fan_percentage
        data:
          value: '{{ percentage }}'
      - choose:
        - conditions:
          - condition: numeric_state
            entity_id: input_number.wohnzimmer_fan_percentage
            above: 83
          sequence:
          - service: script.wohnzimmer_fan_hi
        - conditions:
          - condition: numeric_state
            entity_id: input_number.wohnzimmer_fan_percentage
            above: 50
          sequence:
          - service: script.wohnzimmer_fan_mid
        
        - conditions:
          - condition: numeric_state
            entity_id: input_number.wohnzimmer_fan_percentage
            above: 33
          sequence:
          - service: script.wohnzimmer_fan_low
        default: 
          sequence:
          - service: script.wohnzimmer_fan_off
      mode: single
    
    wohnzimmer_fan_off:
      action: call-service
      service: remote.send_command
      data:
        device: Ventilator
        command: Off
      target:
        device_id: 24c8fd2e58f1ff37b0ff9ab382e6de11
    
    wohnzimmer_fan_low:
      action: call-service
      service: remote.send_command
      data:
        device: Ventilator
        command: Low
      target:
        device_id: 24c8fd2e58f1ff37b0ff9ab382e6de11
        
    wohnzimmer_fan_mid:
      action: call-service
      service: remote.send_command
      data:
        device: Ventilator
        command: Mid
      target:
        device_id: 24c8fd2e58f1ff37b0ff9ab382e6de11
    
    wohnzimmer_fan_hi:
      action: call-service
      service: remote.send_command
      data:
        device: Ventilator
        command: Hi
      target:
        device_id: 24c8fd2e58f1ff37b0ff9ab382e6de11

Are you able to run those scripts manually from Developer? If not then you can’t run them via a template either. If both can’t find them then check your logs to see if the scripts aren’t loading because of a problem.

the only thing I see is that your indentation under the “script:” header is different from the ones above. That might cause issues.

try moving everything under “script:” back to the left 2 spaces:

script:
  wohnzimmer_fan_switchon:
    alias: Ventilator einschalten
    sequence:
    - alias: Ventilator auf low schalten
      service: wohnzimmer_fan_low
    - alias: Status auf on setzen
      service: input_boolean.turn_on
      target:
        entity_id: input_boolean.wohnzimmer_fan_state
    - alias: Prozent auf 33 setzen
      service: input_number.set_value
      target:
        entity_id: input_number.wohnzimmer_fan_percentage
      data:
        value: 33
    mode: single

  wohnzimmer_fan_switchoff:
      sequence:
.
.
.

My goodness, that was a tough one. Some lines of it I had apparently from old tutorials and I fear I will never really understand YAML.
In the end, I clicked most of it together in the script editor to get the indentations right. Sometimes something is indented, sometimes a dash is put in front of it. This has nothing to do with the other programming languages I usually use. :slight_smile:

And such error messages are not very helpful either. Sigh.

Got None. (See ?, line ?).

But here it is, this code works. Maybe someone can do something with it.

input_boolean:
  wohnzimmer_fan_state:

input_number:
  wohnzimmer_fan_percentage:
    name: Wohnzimmer Ventilator Prozent
    min: 0
    max: 100

fan:
  - platform: template
    fans:
      wohnzimmer_fan:
        friendly_name: Wohnzimmer Ventilator
        unique_id: wohnzimmer_fan
        value_template: "{{ states('input_boolean.wohnzimmer_fan_state') }}"
        percentage_template: "{{ states('input_number.wohnzimmer_fan_percentage') }}"
        turn_on:
          service: script.wohnzimmer_fan_switchon
        turn_off:
          service: script.wohnzimmer_fan_switchoff
        set_percentage:
          service: script.wohnzimmer_fan_set_speed
          data:
            percentage: "{{ percentage }}"
            
script:
  wohnzimmer_fan_switchon:
    sequence:
    - alias: Ventilator auf low schalten
      service: script.wohnzimmer_fan_low
    - alias: Status auf on setzen
      service: input_boolean.turn_on
      target:
        entity_id: input_boolean.wohnzimmer_fan_state
    - alias: Prozent auf 33 setzen
      service: input_number.set_value
      target:
        entity_id: input_number.wohnzimmer_fan_percentage
      data:
        value: 33
    mode: single
    icon: mdi:fan
    
  wohnzimmer_fan_switchoff:
    sequence:
    - alias: Ventilator ausschalten
      service: script.wohnzimmer_fan_off
    - alias: Status auf off setzen
      service: input_boolean.turn_off
      target:
        entity_id: input_boolean.wohnzimmer_fan_state
    - alias: Prozent auf 0 setzen
      service: input_number.set_value
      target:
        entity_id: input_number.wohnzimmer_fan_percentage
      data:
        value: 0
    mode: single
    icon: mdi:fan-off
    
  wohnzimmer_fan_set_speed:
    sequence:
      - service: input_number.set_value
        data:
          value: '{{ percentage }}'
        target:
          entity_id: input_number.wohnzimmer_fan_percentage
      - choose:
          - conditions:
              - condition: numeric_state
                entity_id: input_number.wohnzimmer_fan_percentage
                above: "79"
            sequence:
              - service: script.wohnzimmer_fan_hi
                data: {}
          - conditions:
              - condition: numeric_state
                entity_id: input_number.wohnzimmer_fan_percentage
                above: "49"
            sequence:
              - service: script.wohnzimmer_fan_mid
                data: {}
          - conditions:
              - condition: numeric_state
                entity_id: input_number.wohnzimmer_fan_percentage
                above: "0"
            sequence:
              - service: script.wohnzimmer_fan_low
                data: {}
        default:
          - service: script.wohnzimmer_fan_off
            data: {}
    mode: single
    icon: mdi:fan-plus
  
  wohnzimmer_fan_off:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.wohnzimmer_remote
      data:
        device: Ventilator
        command: 'Off'
    icon: mdi:fan-off
  wohnzimmer_fan_low:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.wohnzimmer_remote
      data:
        device: Ventilator
        command: 'Low'
    icon: mdi:fan-speed-1
  wohnzimmer_fan_mid:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.wohnzimmer_remote
      data:
        device: Ventilator
        command: 'Mid'
    icon: mdi:fan-speed-2
  wohnzimmer_fan_hi:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.wohnzimmer_remote
      data:
        device: Ventilator
        command: 'Hi'
    icon: mdi:fan-speed-3

Now I have also slowly understood that a script in which there is even a small error is not even recognized and immediately acknowledged with "Unable to find service ".

I have shortened the code even further and improved the functionality. Switching on and off are now decoupled from the speed. I.e. if you switch off at 100%, it will be set to 100% again when you switch on. If you pull the slider to 0%, the fan is switched off.

# Keep track of the fan state
input_boolean:
  wohnzimmer_fan_state:

# Keep track of fan speed
input_number:
  wohnzimmer_fan_percentage:
    name: Wohnzimmer Ventilator Prozent
    min: 0
    max: 100

fan:
  - platform: template
    fans:
      wohnzimmer_fan:
        friendly_name: Wohnzimmer Ventilator
        unique_id: wohnzimmer_fan
        value_template: "{{ states('input_boolean.wohnzimmer_fan_state') }}"
        percentage_template: "{{ states('input_number.wohnzimmer_fan_percentage') }}"
        turn_on:
          service: script.wohnzimmer_fan_switchon
        turn_off:
          service: script.wohnzimmer_fan_switchoff
        set_percentage:
          service: script.wohnzimmer_fan_set_speed
          data:
            percentage: "{{ percentage }}"
script:
  wohnzimmer_fan_switchon:
    sequence:
    - service: input_boolean.turn_on
      target:
        entity_id: input_boolean.wohnzimmer_fan_state
      data: {}
    - choose:
      - conditions:
        - condition: numeric_state
          entity_id: input_number.wohnzimmer_fan_percentage
          below: '1'
        sequence:
        - service: input_number.set_value
          data:
            value: '33'
          target:
            entity_id: input_number.wohnzimmer_fan_percentage
    - service: script.wohnzimmer_fan_set_speed
      data:
        percentage: '{{ states(''input_number.wohnzimmer_fan_percentage'') }}'
    mode: single
    icon: mdi:fan
  wohnzimmer_fan_switchoff:
    sequence:
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.wohnzimmer_fan_state
    - service: script.remote_send_command
      data:
        command: 'Off'
    mode: single
    icon: mdi:fan-off
  wohnzimmer_fan_set_speed:
    sequence:
    - service: input_number.set_value
      data:
        value: '{{ percentage }}'
      target:
        entity_id: input_number.wohnzimmer_fan_percentage
    - choose:
      - conditions:
        - condition: numeric_state
          entity_id: input_number.wohnzimmer_fan_percentage
          above: '79'
        sequence:
        - service: script.remote_send_command
          data:
            command: Hi
      - conditions:
        - condition: numeric_state
          entity_id: input_number.wohnzimmer_fan_percentage
          above: '49'
        sequence:
        - service: script.remote_send_command
          data:
            command: Mid
      - conditions:
        - condition: numeric_state
          entity_id: input_number.wohnzimmer_fan_percentage
          above: '0'
        sequence:
        - service: script.remote_send_command
          data:
            command: Low
      default:
      - service: script.wohnzimmer_fan_switchoff
    mode: single
    icon: mdi:fan-plus
  remote_send_command:
    sequence:
    - service: remote.send_command
      target:
        entity_id: remote.wohnzimmer_remote
      data:
        device: Ventilator
        command: '{{ command }}'
    icon: mdi:remote

Hi, could you help me with this script ? I want to use it to control a AC fan. (5 settings based on xx %)

I tried pasting the code into “scripts” (menu automations and scripts) , but immediately got an error “Message malformed: extra keys not allowed @ data[‘input_boolean’]” Or how do I go about this?

Also, I need to change the entity id’s I guess ? Anything else I need to consider?