Can I use home assistant to always keep my sons lights off from 9PM till 8AM?

My son (3), turns on his room lights every time I leave the room, which keeps him awake. I was curious if I could create a script that will automatically turn the lights off, every time he turns them on between 9PM and 8AM.

I haven’t used Home Assistant before, but I have several smart home TP-Link Kasa Smart Switches. I was thinking I could install one in his room and then write a script or something that’s run every time the light is turned on, and then check the time, and then turn it off it’s between certain hours.

I of course need a way to override this from my phone.

In a perfect world, I’d be able to trigger this via Alexa, and not based on the current time, where I can say “Alexa, lock the lights in room”, and then after he falls asleep unlock them so I can change his diaper with the lights on in the middle of the night or something.

Thanks for any help!

Something like this-

trigger:
  - platform: state
    entity_id: light.your_light
    to: 'on'
    for: '00:00:20'
condition:
  - condition: time
    after: '21:00'
    before: '08:00'
  - condition: state
    entity_id: input_boolean.child_bedroom_light_override
    state: 'off'
action:
  - service: light.turn_off
    target:
      entity_id: light.your_light

Then you’ll need to create an input_boolean-

input_boolean:
  child_bedroom_light_override:
    name: Child Bedroom Light Override

If you want to override the automation, you just need to turn ON the input_boolean.

Edit:
(1) Implementing suggestion from Nick Rout

1 Like

I get the impression that would be very frustrating and will just cause junior to stand at the switch flicking it on and off until he gets the desired result, which won’t happen.

So behavior dependent I would introduce a delay so he thinks the light is turned on, gets back into bed and then the light turns off, He’ll get sick of getting up to turn it back on. Especially if the delay is somewhat random in length.

1 Like

What about just setting the brightness to 1 (or a little bit higher so it would work as a night light)

1 Like

Wow thanks for the solution!

So you are suggesting I use the child_bedroom_light_override to lock his light off, which I could interact with via alexa? How would I do that?

Is the only way to interact with the platform via yaml files? Do I have the ability to write scripts that are triggered based on certain events?

Besides YAML there are automations:

How about automatically dimming the light down to 0% over say 5-10 mins so that he does not notice.

What a curious statement.

You do realize that all automations are stored in YAML format? Even the ones created using the Automation Editor.

When using the Automation Editor to create/modify an automation, you can switch from visual mode to YAML mode. What is displayed is the automation in its true form.

No but ultimately all automations and scripts are stored in YAML format. It’s far more compact to show you an automation in YAML format then to post a series of screenshots captured from the Automation Editor.

It’s also more convenient and portable. Usually you can copy-paste an automation’s YAML code from the forum directly into the Automation Editor (while in YAML mode). Switch the Automation Editor to visual mode and it will be presented in GUI format.

1 Like

I apologize for my curious statement. I meant that besides writing YAML code you could use the automations feature in Home Assistant. And indeed the automations are translated into YAML.

1 Like

No. The input_boolean is used to manually override the automation when you want to change his diaper.

You can create a script that you can call from Alexa when you want to change his diaper-

alias: Changing Diaper Begins
mode: single
sequence:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.child_bedroom_light_override
  - delay: '00:00:02'
  - service: light.turn_on
    target:
      entity_id: light.your_light
    data:
      brightness: 50

After you are done, you can call this below script-

alias: Changing Diaper Finished
mode: single
sequence:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.child_bedroom_light_override
  - service: light.turn_off
    target:
      entity_id: light.your_light

OR - you can create an automation to turn the input_boolean OFF and turn the light OFF. You will need to find out how long on average does it takes for you to change his diaper. Below is an automation to turn it OFF after 10 minutes. Adjust the '00:10' accordingly to your needs.

alias: Turn OFF Input Boolean and Lights (Child Bedroom)
description: ''
mode: single
trigger:
  - platform: state
    entity_id: input_boolean.child_bedroom_light_override
    to: 'on'
    for: '00:10'
condition: []
action:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.child_bedroom_light_override
  - service: light.turn_off
    target:
      entity_id: light.your_light

Note: Unfortunately, I am unfamiliar with Alexa. Therefore, I do not know how can you call a script from it. Here’s an alternative for you if you are using iOS-

I think this (or the later gradual dim) suggestion addresses a valid safety concern too should light be needed either by your son or by you entering the room.