EV car power management

Now it works! Many thanks @AndersO

@jorgeli
Hi,
The script makes it stay awake when charging and when drivieng.
In all other cases sleep should be enabled and the dongle should go to sleep after 5 minutes (with the sleep timer set to 300).
Does your dongle sleep when the car is on, when charging or both?.

The dongle is awake Only the 5 min when i drive or charge and then go to sleepmode.
So just follow the dongle power management.

@jorgeli, weird.
How often is the job running that executes the script?
I’ve set it to 6 minutes + run on start, the script sets the sleep to 5 minutes (300 sec) and the setting for the dongle is set to 5 minutes as well.
Does the job have the command properly written?
My guess would be that there is something wrong with the job.

If all is ok I think you need to ask support in that case, mine never goes to sleep while driving or charging. When turned off it does.

Hi Anders
Thanks again for feedback :slight_smile:
Yes something i must have done wrong.
My config is like this:

@jorgeli
Hi, i think you are missing a space, for the Cron schedule I have a space after the 6 minutes: ‘*/6 * * * *’
Try that and see what happens

Thank you very much I thing it work now :slight_smile:

@jorgeli
Good to hear.
FYI there are spaces between all the stars, not sure you are aware of the and not sure it support it without stars.
the star are (* * * * *):

  • minute per hour
  • hour of the day
  • day of the month
  • month of the year
  • day of the week

I’ve followed all your tutorial but I only received: car unlocked.
Do you know why? Thanks

@ricard_ferre_jornet
Hi, I am not sure i understand, car unlocked would be no return or message from what I posted.
The only things i have posted is to keep the autopi awake and to report via telegram on charging, both only hor Ioniq Electric.

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)
2 Likes

I’ve copyed your new code and I’ve put my bot token and bot chadid but I can’t save the code when I put the create button in my autopi.

@ricard_ferre_jornet
I have no idea, I have never had any issues with create, save etc. Buttons they open windows and save etc.
Terry with another browser? I have inky used chrome.
You can always mail support, they are good at answering.

Is it the cloud that send out the custom code every time to reset sleep timer ?
or is the code on Autopi after update?
For me it works sometime and sometime it going to sleep anyway.
So i guess that i have could have bad conection when it going to sleep ?
I try now to increase the sleep timer and see if it workd better.

@jorgeli
I think it is all in the dongle, the code is saved to the dongle and executed there with local jobs.
With the new code the job needs to run at a much shorter interval than the sleep timer, the sleep timer is set in the code to 300 (5 min), what the code does is just resetting the sleep timer as defined in the code, see below.
If resetting the sleep timer without having the period defined it sets it to default which is I believe is 30 minutes
I have the job running every 2 minutes to make sure that it resets it at least twice in the sleep timer time span.

def enable_sleep():
    args = ['sleep']
    kwargs = {
        'enable': True,
        'period': 300,

The setting in the configuration of the dongle for the sleep timer is overwritten when the script is running.

Well is it local on the dongle ti don´t now what is wrong :roll_eyes:
I have the job for 2 min to ,but it’s like it doesn’t update every time and then it is power down?

Same when i test charge with only type 2 it is powering down ( have not test with CCS yet)

@jorgeli
Did you try to enter the command custom_code_name.poll e.g. my_sleep_reset.poll in the command prompt? What do you get then?
You should get ‘sleep timer reset
If you are driving or charging.

Hi
I have problem before when autopi is on but not conection to the cloud, and i think this was the problem this time to.
Because i take the sim card out and take the auto pi out from the car a while, and take it back again.
Now when i get to work it was online all the way and the position map show all the trip.

I will try today if the charge is reset the timer to :pray::pray::pray:

Have test the telegram message with no success.
But maybe this work to if the GSM modul is working now :smiley:

Thanks for all help .
I appreciate it very much :+1::+1::+1:

@AndersO and @el93mon how do I get the bot_chatid from telegram?
I found a echo bot that can tell me my own chatid, but can not figure out how to get the chatid for the bot?!?

Thx.

@Mikael_Madsen
Hi, when i did it I followed the steps in the guides.
Telegram (https://web.telegram.org ).
Check out the following to create a bot: https://www.sohamkamani.com/blog/2016/09/21/making-a-telegram-bot/

This is what you get from Telegram in the process:
You can also share your telegram contact to @get_id_bot to reveal their chat_id or forward a message, or send file or sticker to get their file ID
If you have any question or anything, please feel free to contact get_id_bot creator on @fredykardian, or join my channel @fredykardianchannel thanks.
Please Subscribe and leave any comments at my mini tutorial about telegram bot on Youtube https://www.youtube.com/watch?v=RO_W_EVCvt4