Skip to content

Dynamic time trigger #163

Closed
Closed
@jlo88

Description

@jlo88

Trying to create dynamic time trigger, I have an input_datetime in hass, say input_datetime.test_starttime which only has a time and no date. I want a trigger to take place at this time.

I hoped something like this could work:

@time_trigger("once(str(input_datetime.test_starttime))")
def test_dynamic():
    log.info("triggered dynamic")

This can unfortunately not be parsed. I would like my triggers to be dynamic so I can configure time schemes on the front end without having to go into the python code. The same could be useful in the time_active decorator as well.

Possible work-around for now, have something triggered every minute and check versus the input time from hass, there has to be a better way? This is what I came up with:

@service
@time_trigger("cron(* * * * *)")
def test_dynamic_workaround(entity_id="input_datetime.test_starttime"):    
    # Current time as epoch: 
    current_time = round(datetime.datetime.now().timestamp())

    # Start/stop times as epoch
    start_time_epoch = round(convert_hass_time_to_epoch(state.get(entity_id)))

    log.info(current_time)
    log.info(start_time_epoch)

    if current_time==start_time_epoch: 
        log.info(f"triggered")
    
def convert_hass_time_to_epoch(hassTime):
    """Converts a time from home assistant xx:xx:xx format to epoch of today at that time"""
    today = datetime.date.today()
    dateStr=f"{today.month}.{today.day}.{today.year} {hassTime.hour}:{hassTime.minute}:{hassTime.second}"
    time_object = time.strptime(dateStr, "%m.%d.%Y %H:%M:%S")    
    return time.mktime(time_object)

I hope I'm not overseeing something obvious here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions