Hass startup trigger

Is there a way to trigger an automation at Hass startup? I would like to set it up so that should I ever restart Hass due to config tweaks my Heat Control set points can be reset dependant on the time of day.

You can listen for the event homeassistant_start and act accordingly.

Look at past topics, there is a way to put a startup script in the custom_component folder in your hass config folder and then call that script from the config file.

for example put this in the config file

startup:

This is a sample startup.py script in the custom_component folder:

[code]import time
import datetime
import homeassistant.components as core
import os

day_of_week = datetime.date.today().weekday() # 0 is Monday, 6 is Sunday
now = datetime.datetime.now().time()
night_lights = [‘light.hall’, ‘light.main’, ‘light.employee’, ‘light.tv_room’, ‘light.master_bed’]

The domain of your component. Should be equal to the name of your component

DOMAIN = “startup”

List of component names (string) your component depends upon

DEPENDENCIES = [‘group’,‘light’,‘switch’,‘sensor’]

def setup(hass, config):

print ('Startup routine starting')

# Night Lights
if now < datetime.time(5,30) or now > datetime.time(17,00):
    # Turn on Lights
    for light in night_lights:
        print ('Turning on %s' %light)
        core.turn_on(hass, light)
else:
    # Turn off Lights
    for light in night_lights:
        print ('Turning off %s' %light)
        core.turn_off(hass, light)

    print ('Turning off reception light')
    core.turn_off(hass, 'switch.reception_light')

# Set Alarm state
print (hass.states.get('sensor.alarm'))
if "on" in str(hass.states.get('sensor.alarm')):
    hass.states.set('switch.alarm', 'on')
else:
    hass.states.set('switch.alarm', 'off')

# return boolean to indicate that initialization was successful
print ('Startup routine finished')
return True[/code]

Here is a tested and working sample:

[code]- alias: ‘HA_Start’
trigger:
platform: event
event_type: homeassistant_start
condition:

  • platform: sun
    after: sunset
    after_offset: “-01:30:00”
  • platform: time
    before: ‘23:30:00’
    action:
    service: homeassistant.turn_on
    entity_id: switch.SW1[/code]
    This code could be included into the configuration.yaml file, at automation section. Of course you may change or erase the conditions and the parameters.
10 Likes

For anyone else reading this now, its worth mentioning that since version 0.42 the above startup trigger no longer works.

“Starting 0.42, it is no longer possible to listen for event homeassistant_start. Use the ‘homeassistant’ platform below instead.”

1 Like

So is there no way to trigger an automation at startup? I have a device that I need to make sure is turned on after startup. Thanks.