Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C sensor #159

Open
gio82c opened this issue Nov 26, 2024 · 6 comments
Open

I2C sensor #159

gio82c opened this issue Nov 26, 2024 · 6 comments

Comments

@gio82c
Copy link

gio82c commented Nov 26, 2024

Hi, if I remember correctly, a sensor that can read from the Pi's I2C bus has recently been integrated, I'm using a Suptronics X1202 UPS on my Pi 5, I would like to know if it is possible to read voltage and capacity, from their script I can do it and it would be great to have it on HA.

X1202 Script here: https://suptronics.com/Raspberrypi/Power_mgmt/x120x-v1.0_software.html

@pallago
Copy link
Contributor

pallago commented Nov 26, 2024

After starting a new branch for your modification:

First you need to put a modified version of https://github.com/suptronics/x120x/blob/main/bat.py file into the ext_sensor_lib folder. You need to modify it because it will be executed directly if you import it. Maybe you use an init() function where you define the bus. Also a start the main program (while true) in if __name__ == '__main__':). Try if you can execute it also also try whether you can import it and use it. Maybe you also consider a class for this.

Modify your config.py

ext_sensors = [["MY_SENSOR_NAME", "x1202", "0x36", [-300, 0]]
x1202 will be the device type
0x36 is the address
[-300, 0] will be the default values for temperature and capacity

Then you need to modify the src/rpi-cpu2mqtt.py file:

  1. you need to import your modified bat.py in line 29
  2. maybe you also initialize the bus there (line 29) but only if one of the sensor names matches x1202
  3. write the readout from line 151 if item[1] == "x1202":
  4. display the value if the argument is passed to the script (this is already done automatically)
  5. publish the values to MQTT and define the icon etc.. therefore, write your cide starting from line 572 elif what_config == "x1202_temp_status": and elif what_config == "x1202_cap_status":
  6. define exactly what should be published to MQTT. therefore write your code from line 760. also handle both cases (discovery_message is True or False)

There are many possibilities, this is just one idea. Maybe not the best. When I implemented the external sensors I tried already to give a little structure.

@gio82c
Copy link
Author

gio82c commented Nov 27, 2024

Thanks for the detailed feedback, but I think it is out of my programming skills to go and modify the code.

Knowing that it is possible gives me hope that someone can do it, or at least help me write the code and test it.

Congratulations on the great job you did, I'm a little jealous...hahaha

@pallago
Copy link
Contributor

pallago commented Nov 27, 2024

I wrote some code for you.

https://github.com/pallago/rpi-mqtt-monitor/tree/suptronicsx1202

  1. You need to pull it and change to the branch suptronicsx1202 by git branch suptronicsx1202 after downloading.

  2. Install smbus in the virtual environment (venv) by navigating into the repo, then source rpi_mon_env/bin/activate to enter into the virtual environment and then install it by pip install smbus

  3. Please run it to see whether it works. I always do my checkruns by sudo /home/pi/rpi-mqtt-monitor/rpi_mon_env/bin/python3 /home/pi/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py
    => You should see the new icons in home assistant
    Home Assistant Screenshot

  4. If it works, you need to modify the library https://github.com/pallago/rpi-mqtt-monitor/blob/suptronicsx1202/ext_sensor_lib/x1202.py, line 25-28 and lines 24-37. Since I do not have the sensor, i cannot test it. I just feed the values with "1V" and "100".
    Remark: the capacity corresponds to the state of charge (SOC) in this script. The capacity is used oftentimes as the amount of charge the battery can store. The capacity is used oftentimes in order to define the state of health (SOH) by dividing the current capacity (C_now) by the initial (C_0 @ t=0) one.

@gio82c
Copy link
Author

gio82c commented Nov 28, 2024

Thanks, you are really awesome!

I tried everything you told me, and when I send the checkrun command: sudo /home/pi/rpi-mqtt-monitor/rpi_mon_env/bin/python3 /home/pi/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py I receive:

[['UPS', 'x1202', '0x36', [1, 100]]]

but no new sensors in HA.

@pallago
Copy link
Contributor

pallago commented Nov 28, 2024

Did you try with the original master version?
You should be sure that the master version works as expected. Try to switch to the master version by git branch master and modify the config.py by setting the ext_sensors = False (and comment the others ext_sensor lines) and check whether the command sudo /home/pi/rpi-mqtt-monitor/rpi_mon_env/bin/python3 /home/pi/rpi-mqtt-monitor/src/rpi-cpu2mqtt.py works as it should, i.e. values show up in Home Assistant.

Only if this works, then we can continue to think why this does not work. In case it works, maybe a refresh (F5) in Home Assistant helps?

@gio82c
Copy link
Author

gio82c commented Nov 29, 2024

Maybe I'm doing something wrong...let's recap:

  • I have the master version installed and working, and I see all the sensors on Ha;
  • I copy and create a folder inside rpi-mqtt-monitor with the branch suptronicsx1202 files;
  • I enter the command 'git branch suptronicsx1202';
  • I create a virtual environment and install smbus;
  • I modify the config.py of the master version adding the 'ext_ sensor';
  • I try with the command to test everything.

At this point I'm sure I'm confusing something, because I always get the same output and no new sensors in HA, I've tried and tried several times, deleting and redoing everything from scratch but with no results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants