New code for staying awake for the Ioniq BEV that works better and is smaller, it has to run at shorter intervals than the sleep timer.
I have it running every 2 minutes and the sleep timer is 300 sec (5 min).
The old code had to run at start and then at a longer interval than the sleep timer.
The new one can/should run several times within the sleep timer period.
What it does it just resets the sleep timer everytime it runs if charging or driving.
I think that the disable_sleep() can also be removed, but not 100% sure:
import os
import requests
BOT_TOKEN = '8888888888888888xxxxxxxxxxxx8888888888'
BOT_CHATID = '88888888888'
def poll():
charging_ccs = get_charging_ccs()
charging_normal = get_charging_normal()
soc = get_soc()
#if charging_ccs == 1 or (charging_normal != 1 and soc != -1):
if charging_ccs == 1 or charging_normal == 1 or soc != -1:
disable_sleep()
enable_sleep()
#bot_sendtext("sleep timer reset")
return {"msg": "sleep timer reset"}
#else:
#bot_sendtext("sleep timer running")
#return {"msg": "sleep timer running"}
# enable autopi sleep
def enable_sleep():
args = ['sleep']
kwargs = {
'enable': True,
'period': 300,
'reason': 'charge status',
}
__salt__['power.sleep_timer'](**kwargs)
# disable autopi sleep
def disable_sleep():
args = ['sleep']
kwargs = {
'enable': False,
}
__salt__['power.sleep_timer'](**kwargs)
def get_charging():
try:
args = ['charging']
kwargs = {
'mode': '21',
'pid': '01',
'header': '7E4',
'baudrate': 500000,
'formula': 'bytes_to_int(message.data[11:12])',
'protocol': '6',
'verify': False,
'force': True,
}
return (int(__salt__['obd.query'](*args, **kwargs)['value'])&128)/128
except:
return -1
# get display state of charge
#
def get_soc():
try:
args = ['soc']
kwargs = {
'mode': '21',
'pid': '05',
'header': '7E4',
'baudrate': 500000,
'formula': 'bytes_to_int(message.data[33:34])',
'protocol': '6',
'verify': False,
'force': True,
}
return __salt__['obd.query'](*args, **kwargs)['value']/2.0
except:
return -1
# Get Chrging CCS
def get_charging_ccs():
try:
args = ['CCS Plug']
kwargs = {
'mode': '21',
'pid': '01',
'header': '7E4',
'baudrate': 500000,
'formula': 'bytes_to_int(message.data[11:12])',
'protocol': '6',
'verify': False,
'force': True,
}
return (int(__salt__['obd.query'](*args, **kwargs)['value'])&64)/64
except:
return -1
# Get charging 230V
def get_charging_normal():
try:
args = ['J1772 Plug']
kwargs = {
'mode': '21',
'pid': '01',
'header': '7E4',
'baudrate': 500000,
'formula': 'bytes_to_int(message.data[11:12])',
'protocol': '6',
'verify': False,
'force': True,
}
return (int(__salt__['obd.query'](*args, **kwargs)['value'])&32)/32
except:
return -1
def bot_sendtext(message):
send_text = 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage?chat_id=' + BOT_CHATID + '&parse_mode=Markdown&text=' + message
requests.get(send_text)