Turn off all light bulbs when hassio starts

I bought Ikea Tradfri and Osram Smart+ bulbs and connected them to Conbee and I noticed that after power outage all of them turn on.

I would like to create a rule that every time hass.io boots up it will turn all lights off.

Use the HA start event. Then turn off all lights.

  - alias: Home Assistant Started turn off lights
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: light.turn_off
        entity_id: group.all_lights

Be aware that this will turn all your lights off anytime you restart HA, so be sure to turn the automation off whenever you are going to restart manually.

1 Like

I think I’d have another automation set a Boolean on shutdown so it can be checked against as a condition of the first automation.

1 Like

Thank you very much @silvrr now all my lights will stay turned off in case of power outage!

@samnewman86, it would be great if home assistant could “remember” the state before power outage but I don’t think it’s possible to do it this way because the system will not shut down, it will just stop working.

I was thinking about the possibility to add an exception when the restart is triggered on demand, for instance to update or to apply new configuration. Would it be possible to detect such “maintenance mode”?

Just to be clear, note that they will likely turn on, when the power is restored (this is the default for a number of bulbs) Then after HA boots, they will turn off.

You may want to check in the settings options for your bulbs and see if the power restore state is an option.

Yes, that’s the reason why I want to make this automation :slight_smile: I can easily imagine the scenario when power outage occurs in the middle of the night or when I am not at home and all my lights will be turned on all the time. Home Assistant takes around 5 minutes to boot so it’s not the best solution but so far I couldn’t find anything better

I was reading about it before creating post and it seems that so far only Philips Hue implemented this feature (but you’ll need Philips Hue Hub to upgrade firmware and change these settings). Ikea and Osram bulbs do not have it

It strikes me that these lights are not very ‘wife friendly’ (something most of use here strive for, just to avoid the complaints involved as we are trying to make their lives easier !!! 8 - 0 ) ( IMHO )
Bear in mind that building and testing interfaces, automations and scripts involves a LOT of restarts, so don’t expect a happy ‘other half’ if you want to test a new timing action whist they are ‘insert ANY activity here’
Do you get a lot of power outages ?
You may need to invest in equipment that retains last state as suggested above (I like z-wave gear but that’s just my opinion)
Silvrr’s automation should do exactly as you asked but there are caveats as noted, if you can’t move to retentive memory devices you may have to employ a complete hardware abstraction layer where you interact with HA and it then interacts with your equipment.
The issue there is that you can’t just switch a light on, you have to get a switch that tells HA to then turn the light on.
HA will retain last states (as long as you don’t use initial values in the store locations) and then uses a varient of Silvrr’s automation to run a script which goes though each stored value and maps that to the appropriate light.
The cost of the separate switches and the time involved to implement - would mean that I’d just change the equipment.
YMMV

Create an input Boolean called unexpected power off
Create an automation that sets this to false when ha is restarted manually. Then another automation that sets this to true 30seconds after power on.
Then create an automation to turn all lights off on power up if the unexpected power off Boolean is true.

1 Like

Thanks @Mutt but I just need simple rule to turn off all lights in case of power outage because it’s a deal breaker for me.

@samnewman86, I tried to follow your instructions but my configuration doesn’t work. Lights still turn off when I restart Home Assistant manually.

configuration.yaml

input_boolean:
  maintenance_mode:
    name: HA restarted on demand
    icon: mdi:restart

automation.yaml

- id: '1567199368534'
  alias: HA boot - turn off all lights
  trigger:
  - event: start
    platform: homeassistant
  condition:
  - condition: state
    entity_id: input_boolean.maintenance_mode
    state: 'off'
  action:
  - delay: 00:01
  - service: light.turn_off
    entity_id: group.all_lights
  - data:
      entity_id: input_boolean.maintenance_mode
    service: input_boolean.turn_off
- id: '1567201768135'
  alias: HA - enter maintenance mode
  trigger:
  - event: shutdown
    platform: homeassistant
  condition: []
  action:
  - alias: ''
    data:
      entity_id: input_boolean.maintenance_mode
    service: input_boolean.turn_on
1 Like

Sebastien,
Your code looks good, though it’s not quite the way I write mine, so Im guessing you have used the automation writing editor ?? Either way no bother.
The only thing I can see that may have tripped up your intended operation is that (to me at least) it seems that ‘often’ two lines have been reversed.
The creation of the input_boolean looks spot on.
The automation(s), I’d have written like this : -

- id: '1567199368534'
  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: group.all_lights
    - service: input_boolean.turn_off
      entity_id: input_boolean.maintenance_mode
- id: '1567201768135'
  alias: HA - enter maintenance mode
  trigger:
    - platform: homeassistant
      event: shutdown
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.maintenance_mode

I’m groping a bit as I’m doing this from memory, generally you need to call the service before you demand an action from it. (light.turn_on, input boolean.toggle etc.).
Though as always you need to check indentation and ensure that it passes the ‘configuration’, ‘server control’, ‘check config’
Any errors you get here “should” help you narrow down where you need to concentrate your efforts
Regards
Mutt

1 Like

I should also say that this work around, given that your lights normally ALL come on after a restart, (as silvrr pointed out, they will come on briefly) and will now ALL switch OFF.
So on any restart, you’ll get ALL the lights coming on regardless, but if it was NOT a ‘manual’ restart they will ALL go off again (after the boot delay, restart time and the small timer you have placed in the code above.

(Edit: Sorry, got the cases reversed, corrected now)

1 Like

Still not working. I set input boolean maintenance_mode manually to true and it works right. I checked the logs and I noticed that “HA - enter maintenance mode” is not triggered on restart. Is there any other way to set this input_boolean to true before HA shuts down?

Sebastian,
Sorry, still not got an HA instance near to me, to test on.
I’ve used the homeassistant start event myself before so with that and your comment that it works if you set the boolean manually, it seems that it ‘may’ be around the ‘shutdown’ event.
Just Checking : - did you replace your automations with the ones I gave ? did they throw up any errors in the ‘check cofig’ ? if so, what ?
If anything goes wrong with an automation, the system will just quit running it, so you may need to look at the logs. What I’d do is add some steps to the shutdown and monitor how they go. For example : -

- id: '1567201768135'
  alias: HA - enter maintenance mode
  trigger:
    - platform: homeassistant
      event: shutdown
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.maintenance_mode
    - service: input_boolean.turn_on
      entity_id: input_boolean.debug1
    - delay: '00:00:10'
    - service: input_boolean.turn_off
      entity_id: input_boolean.debug1

so create another input boolean, create a bit of ui so you can watch what happens to your maintenace bit and the debug bit and watch on your phone/tablet/another computer when you hit the restart
Another point, is restart the same as shutdown - maybe they are different events ???

Yes, I used your code and it passed validation without problems.

Start event works fine, automation on shutdown not, so I think it’s not the right trigger to change boolean before restart but I don’t see any other event for homeassistant platform except ‘start’ and ‘shutdown’

Sebastian,
Okay, did you modify the shutdown code to include debuging ?
Did you observe the debugging on another device ? (what happens ?)
Have you tried “shutdown” (I’m assuming this is ‘stop’ instead of ‘restart’) just to see if this triggers your automation ?
What do the logs show at the ‘stop’ (restart will clear the log, but ‘stop’ may leave the log intact) ?
Worst comes to worst, you will just have to remember to set your bit manually before you restart manually.
(Sorry, idiot question but 
 you did create the input_boolean.debug1 (or whatever) didn’t you ? as HA may go " yeah did all that, now what the hell does he mean by ‘debug1’ 
 crash out of that 
 ")
Maybe somebdy else has more knowledge/experience of the shutdown event ???
Regards

Disable the 2nd automation and see what state the maintenance mode Boolean is in after a restart.
Maybe it isnt getting set correctly
If you manually set it to on then restart does the 2nd automatically work as expected?

Everything works fine except automatically setting input boolean.
I tried all your suggestions but none of them worked. I tried homeassistant shutdown trigger, homeassistant_close and homeassistant_stop events but after checking logs I cannot see any of them being triggered either on restart or shutdown.

My workaround for now is to use the switch on dashboard every time I plan to reboot to prevent switching off all lights
image
Just need to remember to switch it off before leaving.

Sebastian,

Not sure I understand.
You restart with the bit set and you "don’t"get your lights turned off.
It also resets the bit on start up so should the system then crash 
 and restart 
 it then checks the bit and says “this was unplanned, turn ALL the lights off” - I thought that’s what you wanted ?
So you ‘ONLY’ have to set the bit, 5 secs before you initiate a manual restart and you are good to go.
What do you have to remember ?
Even my attention span is greater than 5 seconds

Hi, apologies for being newbie but if I paste this in my automation.yaml I get

Error loading /config/configuration.yaml: mapping values are not allowed here
  in "/config/automations.yaml", line 2, column 11

I tried several indenting and I can’t get it to go through. What am I missing? Thanks

No need to apologise for being new to HA, we have all been there. HA itself is only 6 years old so in a year you’ll be helping others on this forum :wink:
From a quick glance, assuming that this is a direct copy from your config (it’s pasted in correctly, so we’ll done for that) it looks like your final line entity_id: has 1 too many spaces before it.
Yaml spacing is alway two spaces at a time.
My first debugging is always just counting spaces.
Try that and come back to us.
Cheers