Can you run python on a specific time?

Right now, I do this, it’s not a very nice solution :slight_smile:

next = datetime.datetime.now() + datetime.timedelta(seconds=rate);

while min < max:
    if datetime.datetime.now() > next:
        next = nextTime(rate)
        min = min + increment
        service_data = {'entity_id': entity_id, attribute: min}
        hass.services.call(service, action, service_data)

Why not use crontab to schedule when you want to run your script?

Hrm I need values from HA to be passed into the crontab somehow. This script, in practice runs every five seconds for a minute, every day at a give, but different, time. It’s for an alarm clock.

How do you set your time?
Another way would be to have an automation that runs every minute with a condition (time) and calls your python script?

Right now it’s from the home assistant control panel, but in the future I hope to get some phyiscal turning knob that feeds into HA.

An automation could work, if I run it every 5 seconds and if HA/python could some how store and update a value/timestamp somewhere that is accessible from python. Then, I could run a script every 5 seconds that checks against that timestamp and if conditions are right sends a message to HA.

Have you thought about MQTT? I use that A LOT to communicate between my various python scripts and HA…

Appdaemon has some good scheduler calls for this sort of thing.

1 Like

I realised i can just use ha:s api:s like .data.get(‘entity_id’).