Thanks.
I exactely copy - past your post without change. ``` si this one
Only I put my token and chat_bot ID. The same as V1
import logging
import requests
import pickle
import os
log = logging.getLogger(name)
bot_token = ‘759619847:AAEei0EZ0z7HfGWfaazvI6SAhb2ny-zx2oU’
bot_chatID = ‘735785677’
“”"
Poll to see if car is being charged. If so :
-
Disable auto sleep whilst charging
-
Send Telegram message when charging starts
-
Send Telegram message when charging reaches 80%
-
Send Telegram message when charging reaches 90%
-
Send Telegram message when charging reaches 100%
-
Send Telegram message when charging stops
“”"
def poll():
enable sleep in case anything goes wrong below
enable_sleep()
load previous status
persistance = load()
check if we are driving or charging
driving = get_driving()
if driving == 1 or driving == -1:
if persistance[‘charging’] == True:
bot_sendtext(“Charge arrete. Derniere charge connu “+format(persistance[‘SOC’],’.1f’)+”%”)
persistance[‘charging’] = False
save(persistance)
return {“msg”: “Not charging”}
now we are charging
disable_sleep()
batt_power = get_charging_power()
soc = get_soc()
alert if just started to charge
if persistance[‘charging’] == False:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
70% alert
#if soc >= 70 and persistance[‘SOC’] < 70:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
75% alert
#if soc >= 75 and persistance[‘SOC’] < 75:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
80% alert
if soc >= 80 and persistance[‘SOC’] < 80:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
85% alert
#if soc >= 85 and persistance[‘SOC’] < 85:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
90% alert
if soc >= 90 and persistance[‘SOC’] < 90:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
99.5% alert
if soc >= 99.5 and persistance[‘SOC’] < 99.5:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
100% alert … not work because 100% is impossible
#if soc >= 100 and persistance[‘SOC’] < 100:
bot_sendtext("Voiture en charge a "+format(batt_power,’.2f’)+“kW. Etat de la batterie “+format(soc,’.1f’)+”%”)
each % alert
#lastsoc = persistance[‘SOC’]
#for level in xrange(1, 100):
#if soc >= level and lastsoc < level:
bot_sendtext("Charging now at a rate of "+format(batt_power,’.2f’)+“kW. State of charge now “+format(soc,’.1f’)+”%”)
break
store status for next time
persistance[‘charging’] = True
persistance[‘SOC’] = soc
save(persistance)
return {“msg”: "Charging at "+format(batt_power,’.2f’)+“kW, SOC now “+format(soc,’.1f’)+”%”}
send message to telegram
def bot_sendtext(bot_message):
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
log.info ("Sending message with "+bot_message);
requests.get(send_text)
load persistance
def load():
try:
persistance = pickle.load( open( ‘charge_status.p’, ‘rb’ ) )
except:
persistance = { ‘charging’: False, ‘SOC’: 0 }
return persistance
save persistance
def save(persistance):
pickle.dump( persistance, open( “charge_status.p”, “wb” ) )
delete persistance
def delete():
os.remove(“charge_status.p”)
check if we are driving. Returns :
0 - charging
1 - driving
-1 - can’t read data
def get_driving():
try:
args = [‘driving’]
kwargs = {
‘mode’: ‘220’,
‘pid’: ‘101’,
‘header’: ‘7E4’,
‘baudrate’: 500000,
‘formula’: ‘bytes_to_int(message.data[53:54])’,
‘protocol’: ‘6’,
‘verify’: False,
‘force’: True,
}
return (int(salt[‘obd.query’](*args, **kwargs)[‘value’])&4)/4
except:
return -1
get charging power
def get_charging_power():
args = [‘charging_power’]
kwargs = {
‘mode’: ‘220’,
‘pid’: ‘101’,
‘header’: ‘7E4’,
‘baudrate’: 500000,
‘formula’: ‘(twos_comp(bytes_to_int(message.data[13:14])256+bytes_to_int(message.data[14:15]),16)/10.0)((bytes_to_int(message.data[15:16])*256+bytes_to_int(message.data[16:17]))/10.0)/1000.0’,
‘protocol’: ‘6’,
‘verify’: False,
‘force’: True,
}
return salt[‘obd.query’](args, **kwargs)[‘value’]-1.0
get display state of charge
def get_soc():
args = [‘soc’]
kwargs = {
‘mode’: ‘220’,
‘pid’: ‘105’,
‘header’: ‘7E4’,
‘baudrate’: 500000,
‘formula’: ‘bytes_to_int(message.data[34:35])/2.0’,
‘protocol’: ‘6’,
‘verify’: False,
‘force’: True,
}
return salt[‘obd.query’](*args, **kwargs)[‘value’]
enable autopi sleep
def enable_sleep():
args = [‘sleep’]
kwargs = {
‘enable’: True,
}
salt’power.sleep_timer’
disable autopi sleep
def disable_sleep():
args = [‘sleep’]
kwargs = {
‘enable’: False,
}
salt’power.sleep_timer’