Problems with making scripts function as switches

I really need some help with this.

I am running 2 seperate scripts to turn my ceiling fan on/off. I have created a toggle switch template to control it in HA. When I toggle it on the fan turns on, but the toggle slide in HA turns itself off. The fan remains on and I am unable to toggle the fan off.

switch:
  - platform: template
    switches:
      living_room_ceiling_fan:
        friendly_name: "Living Room Ceiling Fan"
        value_template: "{{ is_state_attr('switch.living_room_ceiling_fan', 'sensor_state', 'on') }}"
        turn_on:
          service: script.living_room_ceiling_fan_on
        turn_off:
          service: script.living_room_ceiling_fan_off

Post what the switch looks like in developer tools

I was going to say try this:

switch:
  - platform: template
    switches:
      living_room_ceiling_fan:
        friendly_name: "Living Room Ceiling Fan"
        value_template: "{{ is_state_attr('switch.living_room_ceiling_fan', 'sensor_state', 'on') }}"
        turn_on:
          - service: script.living_room_ceiling_fan_on
          - service: homeassistant.update_entity
            entity_id: switch.living_room_ceiling_fan
        turn_off:
          - service: script.living_room_ceiling_fan_off
          - service: homeassistant.update_entity
            entity_id: switch.living_room_ceiling_fan

But then realised you are self referencing the template switch to get it’s own state:

value_template: "{{ is_state_attr('switch.living_room_ceiling_fan', 'sensor_state', 'on') }}"

You can’t do this. You need to monitor something other than the template switch you are constructing.

Is there a specific reason you want to use a script and or templates as turning a fan on and off is a pretty simple candidate for a standard automation?

Could you not simply make a Boolean for the front end and have a simple single automation using Boolean state as trigger to turn on or off?

Or is it part of something bigger or more complex that demands this type of use case?

1 Like

Thanks for all the replies. I was trying to use a script to communicate between my RF ceiling fan and HA with a broadlink. I decided to take a different route and use a helper toggle with an automation. I was unaware of what helpers did. Also all the videos I saw with broadlink used scripts. Im still relatively new to HA and yaml. Not very efficient yet lol.

Last question. I removed the text concerning switches from my config. yaml, but the switches still appear under my entities. How can I remove them completely?

Did you restart home assistant?

sure did. multiple times. maybe it will go away with the next update.

I did this way

created an input_boolean dummy switch

  gas_heater_dummy:
    initial: off
  - platform: template
    switches:
      gas_heater_ir:
        friendly_name: Lounge Gas Heater
        value_template: "{% if is_state('input_boolean.gas_heater_dummy', 'on') %}on{% else %}off{% endif %}"
        turn_on:
          service: script.turn_on
          entity_id: script.ir_turn_on_gas_heater
        turn_off:
          service: script.turn_on
          entity_id: script.ir_turn_off_gas_heater

now the Scripts

ir_turn_on_gas_heater:
  sequence:
    - data_template:
        payload: '{"Protocol":"NEC","Bits":32,"Data":0x416649B6}'
        topic: cmnd/IR-2/irsend
      service: mqtt.publish
    - data_template:
        entity_id: input_boolean.gas_heater_dummy
      service: input_boolean.turn_on
ir_turn_off_gas_heater:
  sequence:
    - data_template:
        payload: '{"Protocol":"NEC","Bits":32,"Data":0X4166B14E}'
        topic: cmnd/IR-2/irsend
      service: mqtt.publish
    - data_template:
        entity_id: input_boolean.gas_heater_dummy
      service: input_boolean.turn_off

That is pretty much equivalent to:

value_template: "{{ states('input_boolean.gas_heater_dummy') }}"

It just omits the case that the input boolean may be something other than on or off. Which TBH I have never seen happen. It’s a very stable core integration.

It’s also worth pointing out that your solution also requires that nothing outside of home assistant can change the state of the heater. Or the input boolean will get out of sync.

1 Like