Homeassistant Python Blinking Issue

switch in the configuration.yaml -

switch:
    - platform: rpi_gpio
      ports:
          22: TestLED1
          12: TestLED2
    - platform: command_line
      switches:
        medium:
          command_on: 'python3 /home/pi/pwm-led-medium.py'
          command_off: 'python3 /home/pi/pwm-led-mediumoff.py'
    - platform: command_line
      switches:
        high:
          command_on: 'python3 /home/pi/pwm-led-high.py'
          command_off: 'python3 /home/pi/pwm-led-mediumoff.py'

python code medium.py -

#!/usr/bin/python3

import RPi.GPIO as GPIO # Remember to run as superuser (sudo)
import time

print("LED DIMMER USING PULSE WIDTH MODULATION")
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)   # This example uses the BCM pin numbering
GPIO.setup(12, GPIO.OUT) # GPIO 25 is set to be an output.

pwm = GPIO.PWM(12, 144)   # pwm is an object. This gives a neat way to control the pin.
pwm.stop()
                         # 25 is the BCM pin number.
                         # 10 is the frequency in Hz.

print("25%")             # Display 50 on the screen
pwm.start(0)
pwm.ChangeFrequency(255)         # This 50 is the mark/space ratio or duty cycle of 50%
pwm.ChangeDutyCycle(75)         # Values from 0 to 100 are allowed including numbers like 33.33
while True:
    time.sleep(.01)               # Infinite loop


pwm.stop()               # Turn PWM off

GPIO.cleanup()           # Always clean up at the end of programs.

python code high.py -

#!/usr/bin/python3

import RPi.GPIO as GPIO # Remember to run as superuser (sudo)
import time

print("LED DIMMER USING PULSE WIDTH MODULATION")
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)   # This example uses the BCM pin numbering
GPIO.setup(12, GPIO.OUT) # GPIO 25 is set to be an output.

pwm = GPIO.PWM(12, 144)   # pwm is an object. This gives a neat way to control the pin.
pwm.stop()
                         # 25 is the BCM pin number.
                         # 10 is the frequency in Hz.

print("25%")             # Display 50 on the screen
pwm.start(0)
pwm.ChangeFrequency(255)         # This 50 is the mark/space ratio or duty cycle of 50%
pwm.ChangeDutyCycle(100)         # Values from 0 to 100 are allowed including numbers like 33.33
while True:
    time.sleep(.01)               # Infinite loop


pwm.stop()               # Turn PWM off

GPIO.cleanup()           # Always clean up at the end of programs.

When I switch between the two in home assistant, they constantly blink and go back and forth. I’m not sure what I’m doing wrong and am completely stuck. Please help! :frowning:

oh and here it is to turn off

#!/usr/bin/python3

import RPi.GPIO as GPIO # Remember to run as superuser (sudo)
import time

print("LED DIMMER USING PULSE WIDTH MODULATION")

GPIO.setmode(GPIO.BCM)   # This example uses the BCM pin numbering
GPIO.setup(12, GPIO.OUT) # GPIO 25 is set to be an output.

pwm = GPIO.PWM(12, 10)   # pwm is an object. This gives a neat way to control the pin.

pwm.stop()
GPIO.cleanup

print("0%")             
pwm.start(0)
pwm.ChangeFrequency(255)         # This 50 is the mark/space ratio or duty cycle of 50%
pwm.ChangeDutyCycle(0)         # Values from 0 to 100 are allowed including numbers like 33.3
time.sleep(1)             


pwm.stop()               # Turn PWM off

GPIO.cleanup()           # Always clean up at the end of programs.

Oh and btw I’m not a programmer by any means, so excuse my mess ups

Hey, can you edit your posts so the code is formatted properly? Just highlight the code and click the “</>” button in the text editor on your post.

My bad, fixed!