Cheap sensors (temperature, motion and light) + MQTT

changed the script to #! usr/bin/python3
Changed the pimqtt.service to python3, as well…still nothing. Works if run manually, but not as a service.

I’m lost…
Try changing the type of your system file from idle to simple?
and you re saying crontab doesn’t want to start it either?

I think I found something:

Tried it via rc-local and writing its logs somewhere. Here is the output:
My IP address is 10.0.0.12
Traceback (most recent call last):
File “/home/pi/pir.py”, line 6, in
import paho.mqtt.client as mqtt
ImportError: No module named paho.mqtt.client

Going after this now.

Cheers,
Antonio

@Antonio_Santos That seems a bit strange since you told me your script was working fine…
How did you install the paho module?

For systemd: Have you checked the permissions on the files? Do you have any other failed units?

This would imply that the paho.mqtt.client module has been installed for the local user but is not available as root. The safest way to progress is to run the service as the user you test with by changing User= line in your service file. Presumably User=pi

Right on!
Got it working by adding User=pi to the service file.
thanks a million!

1 Like

@gpbenton thx!

@Antonio_Santos Glad you finally managed to get it right.

Now how can I improve the tutorial because I didn’t have to specify User=pi in my system file when @Antonio_Santos had to?

You have to understand where pip installs modules, and when it is available to which users.

At some point, your system had installed the module as root user, whereas other systems may not. To avoid this confusion in the future, I recommend using a separate virtual environment for each python program, which will define and supply the modules (and versions ) necessary for that program.

@gpbenton So you mean that because I installed the paho module as the pi user with sudo (sudo pip3 install …), I’ve installed it system-wide whereas @Antonio_Santos had installed it only for the pi user?
So sudo pip3 install is the problem here? as well as a huge security problem?

Whenever you use sudo you start acting as the root user, with privileges to sensitive parts of the system. It is best avoided unless it is necessary. The default raspbian install makes it easy to do (without requiring a password) which tends to make casual use of sudo too common.

Installing pip modules as root isn’t really a major security problem (programs still have to run as root to have root privileges), but it isn’t necessary. You can use virtual environments to ensure that all the necessary modules are available to a program, and they are the correct versions, without using sudo.

Trying to implement the LDR python script on a Raspberry Pi3 and getting an error when running the script (manually)

Traceback (most recent call last):
  File "light.py", line 35, in <module>
    client = mqtt.Client()
AttributeError: module 'paho.mqtt.client' has no attribute 'Client'

Should be

   client = mqtt.client()

ah yes i tried correcting that and got:

Traceback (most recent call last):
  File "light.py", line 35, in <module>
    client = mqtt.client()
AttributeError: module 'paho.mqtt.client' has no attribute 'client'

Are you using the exact same code or have you made some modifications?
How did you name your python script?

I am afraid I have to correct myself. I just checked at https://pypi.org/project/paho-mqtt/#description and

client = mqtt.Client()

was actually correct - at least if you do

import paho.mqtt.client as mqtt

first.

I think as @barbarin says, we need more information.

@jnvd3b: My bad. Just noticed you named your script light.py…
Still need to see the entire code.
If a .pyc file has been generated after you launched the script, then you should probably delete it before launching the script again… ( this is what i was thinking about: https://sopython.com/canon/96/attributeerror-module-object-has-no-attribute-foobar/ )

I just found this thread. Thank you Barbarin for sharing. I am a nubee and all help is greatly appreciated. I have your temperature sensor code working. This my first mqtt code so I am tickled beyond belief.

I do have a question for anyone that may be able to help. I have the code displaying degrees Fahrenheit. How do I format the deg F to display only one decimal place?

Thanks in advance.
Mike
Here is the part of my code that I think needs to be modified.
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_f

I would prefer to leave the code for the sensor to report as accurately as it can, and then round using the template function round() in the HA configuration file. See

Thanks. I have no idea how to do that so more research straight ahead.
I had a bad feeling my code was too simple. :frowning:
…in my configuration.yaml file…

#Garage Temperature
sensor 04:

  • platform: mqtt
    state_topic: “home-assistant/garage/temperature”
    name: Garage_Temperature
    unit_of_measurement: F

@MTStringer

Just add this:
value_template: ‘{{ value|round(1) }}’

1 Like