Python script to read status laptop battery

Python and python framework

Moderator: leecollings

Post Reply
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Python script to read status laptop battery

Post by AllesVanZelf »

I would like to share my findings regarding reading the status of my laptop battery.
You might have suggestions to make this easier or better. :)

I created 2 dummy devices
- 1 percentage - idx 525 - Battery capacity (lading)
- 2 text - idx 516 - Battery status (charging, full, etc)

The python script:

Code: Select all

!/usr/bin/python

import sys
import ssl
import time
import datetime
import time
from urllib2 import urlopen
from time import sleep
 
# change these values
# domoticz = URL to the Domoticz server
# idx_ are the id's of the devices corresponding to the data in domoticz
domoticz = "http://192.168.188.61:8090"
idx_laptopaccustatus = 516
idx_laptopacculading = 525
# End 

battery_status_file = "/sys/class/power_supply/BAT0/status"
battery_lading_file = "/sys/class/power_supply/BAT0/capacity"

def accustatuslaptop():
#   time.sleep(0.1)
   file = open(battery_status_file, "r")
   return file.read()

def acculadinglaptop():
#   time.sleep(0.1)
   file = open(battery_lading_file, "r")
   return file.read()

# EEEPC-Acculading
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_laptopacculading)+'&nvalue=0&svalue='+str(acculadinglaptop()))
response.read()

# EEEPC-Accustatus
response= urlopen(domoticz+'/json.htm?type=command&param=udevice&idx='+str(idx_laptopaccustatus)+'&nvalue=0&svalue='+str(accustatuslaptop()))
response.read()
I execute this python code in a crontab -e like:

Code: Select all

*/12 * * * *  /home/bart/bin/batterycheck.py
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Post Reply