How to make a switch with two scripts

Hey,

I’ve only just started using Home Assistant and I need some help.
I have a ceiling fan that I’m trying to control using a Logitech Harmony. I did get it working using these scripts

to turn fan on:

ceiling_fan_fan_medium:
  alias: Fan Medium
  sequence:
  - service: remote.send_command
    data:
      entity_id: remote.harmony_hub
      command:
      - Fan Medium
      device: Ceiling Fan

to turn fan off:

ceiling_fan_fan_off:
  alias: Fan Off
  sequence:
  - service: remote.send_command
    data:
      entity_id: remote.harmony_hub
      command:
      - Fan Off
      device: Ceiling Fan

But it’s not how I want it. It looks clean in the Home Assistant UI


but when I use Homekit it looks like a mess.

I was wondering if anyone could help me turn those two scripts into one on and off switch for homekit. I’ve tried using ChatGPT to help me but it hasn’t worked lol.

Thanks.

It never does.

Assuming the command accepts a template:

  • Create a Toggle Helper (input_boolean), call it Fan.
  • Create an automation (can do it in the UI if you prefer, but you’ll have to Edit as YAML for the template):
trigger:
  - platform: state
    entity_id: input_boolean.fan
action:
  - service: remote.send_command
    data:
      entity_id: remote.harmony_hub
      command:
      - "{{ {'on': 'Fan Medium', 'off': 'Fan Off'}[trigger.to_state.state] }}"
      device: Ceiling Fan

You can then put that toggle in the dashboard.

If you didn’t want to use a template, you can put Choose logic in the action instead to achieve the same thing.

There’s an easier solution:

I got it working with this

- alias: Ceiling Fan State Changed
  trigger:
    platform: state
    entity_id: input_boolean.ceiling_fan_state
  action:
    - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.ceiling_fan_state
            state: 'on'
        sequence:
          - service: remote.send_command
            data:
              entity_id: remote.harmony_hub
              command: Fan Medium
              device: Ceiling Fan
      - conditions:
          - condition: state
            entity_id: input_boolean.ceiling_fan_state
            state: 'off'
        sequence:
          - service: remote.send_command
            data:
              entity_id: remote.harmony_hub
              command: Fan Off
              device: Ceiling Fan

Thank you

The best way to represent a fan is not a switch, not a script or input select, but …ehhh… a fan :wink:

1 Like