Skip to content

Instantly share code, notes, and snippets.

@SqyD
Last active November 23, 2022 07:30
Show Gist options
  • Save SqyD/a927ab612df767a0cc892bcde23d025c to your computer and use it in GitHub Desktop.
Save SqyD/a927ab612df767a0cc892bcde23d025c to your computer and use it in GitHub Desktop.

Revisions

  1. SqyD revised this gist May 22, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    ## MQTT client to control a PWM fan from a raspberry pi
    A simple python script to control a fan from Home Assistant.

    # Note: I have replaced this Raspberry based solution with an Wemos/ESPHome based one you can find here:
    https://gist.github.com/SqyD/38d10391c2e21988406d2bdaec24f031

    * Installing dependencies on a stock Raspbian install:

  2. SqyD revised this gist Sep 16, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    ## MQTT client to control a PWM fan from a raspberry pi
    A simple python script to control a fan from Home Assistant.


    * Installing dependencies on a stock Raspbian install:

    ```
  3. SqyD revised this gist Sep 15, 2017. 3 changed files with 26 additions and 1 deletion.
    6 changes: 6 additions & 0 deletions 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,10 @@ sudo pip3 install paho-mqtt
    * Set permissions:
    ```
    sudo chmod 644 /lib/systemd/system/mqtt_fan.service
    ```
    * Enable and test the service:
    ```
    sudo systemctl daemon-reload
    sudo systemctl enable mqtt_fan.service
    sudo systemctl start mqtt_fan.service
    ```
    17 changes: 17 additions & 0 deletions configuration.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # Snippet to add to a Home Assistant configuration yaml file
    mqtt:

    fan:
    - platform: mqtt
    name: "My Fan"
    command_topic: "homeassistant/test_fan/on/set"
    state_topic: "homeassistant/test_fan/on/state"
    speed_command_topic: "homeassistant/test_fan/speed/set"
    speed_state_topic: "homeassistant/test_fan/speed/state"
    payload_low_speed: "low"
    payload_medium_speed: "medium"
    payload_high_speed: "high"
    speeds:
    - low
    - medium
    - high
    4 changes: 3 additions & 1 deletion mqtt_fan.py
    Original file line number Diff line number Diff line change
    @@ -59,8 +59,10 @@ def on_message(client, userdata, message):
    client.publish(speed_state_topic, fan.speed_state)

    # Send messages in a loop
    client = mqtt.Client("qstream")
    client = mqtt.Client("myfan")
    client.on_message = on_message
    # If your homeassistant is protected by as password, uncomment this next line:
    # client.username_pw_set('homeassistant','yourpasswordhere')
    client.connect(broker)
    client.subscribe(on_topic)
    client.subscribe(speed_topic)
  4. SqyD revised this gist Sep 15, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,6 @@ sudo pip3 install paho-mqtt
    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
    * Set permissions:
    ```sudo chmod 644 /lib/systemd/system/mqtt_fan.service```
    ```
    sudo chmod 644 /lib/systemd/system/mqtt_fan.service
    ```
  5. SqyD revised this gist Sep 15, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,4 @@ sudo pip3 install paho-mqtt
    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
    * Set permissions:
    `sudo chmod 644 /lib/systemd/system/mqtt_fan.service`
    ```sudo chmod 644 /lib/systemd/system/mqtt_fan.service```
  6. SqyD revised this gist Sep 15, 2017. 2 changed files with 2 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions 1st_readme.md
    Original file line number Diff line number Diff line change
    @@ -9,3 +9,5 @@ sudo pip3 install paho-mqtt

    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
    * Set permissions:
    `sudo chmod 644 /lib/systemd/system/mqtt_fan.service`
    1 change: 0 additions & 1 deletion mqtt_fan.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    #!/usr/bin/python3

    import time
    import paho.mqtt.client as mqtt
    from gpiozero import PWMOutputDevice

  7. SqyD renamed this gist Sep 15, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. SqyD revised this gist Sep 15, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    * Installing dependencies on a stock Raspbian install:


    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt

    ```
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt
    ```

    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
  9. SqyD revised this gist Sep 15, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,11 @@
    * Installing dependencies on a stock Raspbian install:


    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt


    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
  10. SqyD revised this gist Sep 15, 2017. 2 changed files with 14 additions and 1 deletion.
    10 changes: 10 additions & 0 deletions mqtt_fan.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    [Unit]
    Description=MQTT Fan
    After=multi-user.target

    [Service]
    Type=idle
    ExecStart=/usr/bin/python3 /home/pi/mqtt_fan.py

    [Install]
    WantedBy=multi-user.target
    5 changes: 4 additions & 1 deletion raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    Installing dependencies on a stock Raspbian install:
    * Installing dependencies on a stock Raspbian install:

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt

    * Create the python script as /home/pi/mqtt_fan.py
    * Create a systemd service definition file as /lib/systemd/system/mqtt_fan.service
  11. SqyD revised this gist Sep 15, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    Installing dependencies on a stock Raspbian install:

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
  12. SqyD revised this gist Sep 15, 2017. 1 changed file with 5 additions and 6 deletions.
    11 changes: 5 additions & 6 deletions raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    {code}
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt
    {code}
    Installing dependencies on a stock Raspbian install:
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt
  13. SqyD revised this gist Sep 15, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    {code}
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt
    sudo pip3 install paho-mqtt
    {code}
  14. SqyD created this gist Sep 15, 2017.
    68 changes: 68 additions & 0 deletions mqtt_fan.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    #!/usr/bin/python3

    import time
    import paho.mqtt.client as mqtt
    from gpiozero import PWMOutputDevice

    # MQTT configuration
    broker = '127.0.0.1'
    device_id = 'test_fan'
    on_topic = 'homeassistant/' + device_id + '/on/set'
    on_state_topic = 'homeassistant/' + device_id + '/on/state'
    speed_topic = 'homeassistant/' + device_id + '/speed/set'
    speed_state_topic = 'homeassistant/' + device_id + '/speed/state'

    # gpio settings
    gpio_pin = 18
    pwm_freq = 5000
    speeds = {'low': 0.05, 'medium': 0.4, 'high': 1}

    class PwmFan:
    on_state = 'OFF'
    pwm = PWMOutputDevice(gpio_pin,frequency=pwm_freq)
    speed_state = 'high'

    def switch(self, onoff):
    if onoff == 'ON':
    if self.on_state == 'OFF':
    self.on_state = 'ON'
    self.set_speed(self.speed_state)
    elif onoff == 'OFF':
    if self.on_state == 'ON':
    self.on_state = 'OFF'
    self.pwm.off()

    def set_speed(self,speed):
    if speed in speeds:
    if self.on_state == 'ON':
    self.pwm.value=speeds[speed]
    self.speed_state = speed


    fan = PwmFan()

    def on_message(client, userdata, message):
    payload = str(message.payload.decode("utf-8"))
    topic = message.topic
    if topic == on_topic:
    print("ON/OF command received")
    if (payload == 'ON') or (payload == 'OFF'):
    print("turning " + payload)
    fan.switch(payload)
    client.publish(on_state_topic, payload)
    if payload == 'ON':
    client.publish(speed_state_topic, fan.speed_state)
    elif message.topic == speed_topic:
    if payload in speeds:
    print('Setting speed to ' + payload)
    fan.set_speed(payload)
    if fan.on_state == 'ON':
    client.publish(speed_state_topic, fan.speed_state)

    # Send messages in a loop
    client = mqtt.Client("qstream")
    client.on_message = on_message
    client.connect(broker)
    client.subscribe(on_topic)
    client.subscribe(speed_topic)
    client.loop_forever()
    4 changes: 4 additions & 0 deletions raspberry_install.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip python3-dev python3-gpiozero
    sudo pip3 install paho-mqtt