Turn off all light bulbs when hassio starts

Never got to come back and work on it until today - thank you for solving it. Works like a charm. No more stress of black-outs when on holidays that turn the whole house into a Christmas tree when the electricity comes back!

This is my final config:

automations.yaml

- alias: HA recovers after power outage - turn off all lights
  trigger:
  - event: start
    platform: homeassistant
  condition:
  - condition: state
    entity_id: input_boolean.maintenance_mode
    state: 'off'
  action:
  - delay: 00:00:30
  - service: light.turn_off
    data:
      entity_id: group.bulbs
  - service: notify.telegram
    data:
      title: Home Assistant started
      message: Home Assistant started after unexpected shutdown.

groups.yaml:

bulbs:
  name: Light bulbs
  entities:
    - light.ikea_bulb_1
    - light.ikea_bulb_2
    - light.osram_bulb_1
    - light.osram_bulb_2

config.yaml

input_boolean:
  maintenance_mode:
    name: Maintenance mode
    icon: mdi:restart

notify:
  - name: telegram
    platform: telegram
    chat_id: !secret telegram_chat_id

Thank you all for your help!

is there any way that in base of to status, if i have a power outage, can remain old status after this power outageā€¦?

HomeAssistant remembers state across reboots unless you set ā€˜initialā€™ parameter of input_boolean variable.

the example I submitted works that way. ā€œMaintenance modeā€ will be on after reboot until you manually disable it.

thanks @higgis
so this example will keep the status of the light after power outage as if was a simple rebot of the service, right?

yes, you can try

so everytime that you want to reboot home assistant you need to put on the input boolean ā€œmaintenance modeā€ in order to keep status of your light, right?

@hjgsr the only issue that i see with this workaround is in the night. for instance, you have your light on and you have a power outage, this light that was ON when home assistant restart will turn off the light due the automation executed to turn off all the light.
in this case, any way to fix itā€¦?

thanks team for the time supporting the questions!!

You could use a combination of automations to have a way to have different things happen depending on when the power goes out. Pair this with scenes that describe the state of your lights during certain times of the day and you should be able to handle most situations. You could accomplish with with scripts as well.

Further, get yourself a small APC UPS that keeps Home Assistant powered on. If you tie that into the APCUPSd integration (https://www.home-assistant.io/integrations/apcupsd/), youā€™ll be able to detect when the unit flips over to battery power and have a good indication of when the power has gone out. That will give you time to flip the input_boolean (as suggested by @hjgsr) to false indicating that itā€™s a power outage and not maintenance mode. I currently do this with my setup (all my networking equipment is on a UPS in my attic) and it works great. I went with a small one for my setup and it gives me about 90 minutes of backup power.

1 Like

You can also use motion sensors and add condition to prevent turning off the light when presence is detected

1 Like

:sweat:
too many automationsā€¦ but this is what we have for nowā€¦ :frowning_face:

Hi
This is my code

- id: 'HA restart - switch off lights'
  alias: HA boot - turn off all lights
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: input_boolean.maintenance_mode
      state: 'off'
  action:
    - delay: '00:00:01'
    - service: light.turn_off
      entity_id: all
    - service: input_boolean.turn_off
      entity_id: input_boolean.maintenance_mode
    - service: notify.mobile_devices
      data:
        message: Server onverwachts herstart! Stroomstoring?  
- id: 'HA mode maintenance'
  alias: HA - enter maintenance mode
  trigger:
    - platform: homeassistant
      event: shutdown
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.maintenance_mode

However indeed , here too the maintenace mode doesnt work
It doesnt enable the boolean
Is this nowadays fixed somehow or do i need to manually switch the boolean too before i start (i dont like that idea butā€¦)

Iā€™m actually thinking that when the event ā€œshutdownā€ takes place, theres no time to turn on anything cause its disconnected then.
So we should turn on the boolean BEFORE shutdown event takes place, not sure if thats possible