[Help] Repeated Action Based On MQTT Payload

Hi,

I am trying to create a python_script to use in an automation for my bedroom tower fan, which will repeat the broadlink switch service call enough times to get to the proper speed setting on the fan. Example, the fan is on high and I want it to go to low, the broadlink switch needs to be called twice (High -> Med -> Low). I have the MQTT Fan and Broadlink set up properly, but I am very new to the python_scripts and don’t know if I used it properly. Here is the python_script


fan_obj = hass.states.get('fan.bedroom_fan') 
fan = fan_obj.state
 


if fan in 'medium' and triggeredEntity == 'low': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
elif fan in 'high' and triggeredEntity == 'low': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
elif fan in 'low' and triggeredEntity == 'medium': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'}) 
elif fan in 'high' and triggeredEntity == 'medium': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
elif fan in 'medium' and triggeredEntity == 'high': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'}) 
elif fan in 'low' and triggeredEntity == 'high': 
   hass.services.call('switch', 'turn_on', { "entity_id" : 'switch.bedroom_fan__speed'})

Here is the automation that I have set for High speed payload

  trigger:
    platform: mqtt
    topic: bedroom_fan/speed/set
    payload: 'high'
  
  action:
    - service: python_script.bedroom_fan_speed
    - service: mqtt.publish
      data:
        topic: 'bedroom_fan/speed/state'
        payload: 'high'
    - service: mqtt.publish
      data:
        topic: 'bedroom_fan/on/state'
        payload: 'true'

Any help would be amazing!