Raspberry Pi Fan

Sorry but why do you think return is wrong. every function needs to have a return. This function’s return is in finally which is always executed in the try/catch situation hence why return is in finally.

It might be better to use latest version of python but in any case can you try Pip3 instead of pip?
If you can use python3.9 try this:

python3.9 -m pip install paho-mqtt

try looking here for instructions on how to install. https://pypi.org/project/paho-mqtt/

My problem is i cannot install python3 on my work laptop which is what i am mostly using.

Hi umerfranz.
hi already have latest version of python:

:~ $ python --version
Python 2.7.16

:~ $ python3 --version
Python 3.9.0

It’s 2 days i’m on paho-mqtt · PyPI
:slight_smile: anyway thank you very much for your time and your help

once again sorry for not directly helping with your python code. Did you try the alternate approach of using Raspberry PI GPIO directly from Home assistant? this will remove the need for this python code.

details in same post at this anchor Raspberry Pi Fan

Or are you keen to get the python code working? (if i were you i would not give up until i figure this out but then there is a still a limit to how long you want to spend you time and energy on this)

Also try using the find command to see if there are any other variants of Paho on the system. and maybe you are not using the correct one.

Use pip3 to install paho again maybe?

I would like to get this script working. It’s will be a good school for learn.
I think the i understood the problem but i don’t know how to resolve it now.
Script in under a subfolder of \home path. This script is autostarted so i think that it is started from root user and paho i think is installed with pi user. I tried to istall paho with sudo ut it don’t resolve the problem…

This is been the solution!!! :- )Thank you!!!

sudo pip3 install paho-mqtt

Now i have just a little problem…

when i try to read the sensor, the result is unknow

this is what i inserted configuration.yaml

Sensor:
  - platform: mqtt
    name: "Fan RPM"
    state_topic: "RPI/FanSpeed"

and this is what i put inside the script:

import paho.mqtt.publish as publish
.......
......

publish.single("RPI/FanSpeed", current_speed, hostname="[My_ip_hostname]", auth = {'username':"[my_mqtt_user]",'password':"[my_mqtt_password]"})

Glad the import issue is resolved. Did you try running the python script directly and printing out the fanspeed to see if it is acquiring and logging data actually?

if it is then next thing to check is if your MQTT server is up and running. You can use MQTTSpy to monitor if the script is actually logging any readings. Then the last part is to make sure your Hassio is connected to this mqtt server. if you used Hassio addon for MQTT then last thing should not be a problem, the server and connection to it are automatically setup.

I’m using mqtt explorer to monitor mqtt publish and i don’t see this publish.
Mqtt is already configured in HA and it is working. I have other publish that arrive to my mqtt broker. I don’t know how i could print the fanspeed…

print([your variable like fanspeed])

In your code i do not see you establishing the connection first with the MQTT server. try to follow my original code. you have to first connect with the server before calling the publish function. also remember to put proper error etc mesasge logs

it’s not enough to set hostname, username and password? I can’t see in your script where you estabilish connection with server…

You are right actually, my bad. So what is the result of this command? this should be put in a try catch and log printed out.

don’t work… no error but don’t print nothing. Now i want to try a simply script just to understand if paho is working. i don’t know python so i should to find something to test…