Living Room Lights

Hello everyone, I am rather new with HA and in need of help.

Here is what I am trying to do:

  1. When I enter the living room the light should turn on. The light should remain on as long as there is motion detected or the tv in the living room is on.

  2. The light should turn off if the tv is off and there is no motion detected for 10 minutes.

I already have the motion sensor, the tv and the lights set up in home assistant, and they are all working and being detected, I just need to figure out how to create this automation.

  1. The ID for the TV is: media_player.lg_webos_tv_oled77c16la
  2. The ID for the sensor is: binary_sensor.vardagsrums_sensor_1_motion_2
  3. The ID for the lamps: light.fonster and light.taklampan_vardagsrummet

Any help would be appreciated.
/Thanks

What have you tried?

post what you have tried and someone can guide you from there.

Honestly not much. I know how to create a simple automation in the gui, that if motion is detected, turn on the light, and I know how to add an action to turn off the light after 10 minutes.

But I have no idea how to make it check if there is movement before the light is turned off, nor do I know how to make it check if the tv is on.

Those checks are called conditions.

So in your case you should make two automations

  • one that turns the lights on with motion

  • another that turns the light off after 10 minutes of no motion as the trigger with two conditions - that the tv is off and there has been no motion for 10 minutes.

All of that can be done via the UI.

Tried to paste this code into the automations.yaml (not sure if the code is correct or not?)

- id: '12345678'
  alias: Turn on light when motion detected, turn off after 10 minutes with no motion and TV off
  trigger:
    - platform: device
      device_id: binary_sensor.vardagsrums_sensor_1_motion_2
      domain: binary_sensor
      type: motion
      for:
        seconds: 1
  action:
    - service: light.turn_on
      entity_id: light.fonster
    - service: light.turn_on
      entity_id: light.taklampan_vardagsrummet
  condition:
    - condition: state
      entity_id: media_player.lg_webos_tv_oled77c16la
      state: 'off'
  delay:
    minutes: 10

But I am getting the following error:
Logger: homeassistant.config
Source: config.py:508
First occurred: 3:11:26 AM (1 occurrences)
Last logged: 3:11:26 AM

Invalid config for [automation]: [delay] is an invalid option for [automation]. Check: automation->delay. (See /config/configuration.yaml, line 12).

you need to remove…

delay:
    minutes: 10

…because, as the error tells you, it’s not a valid option.

I would use a state trigger instead of a device trigger tho.

So change the trigger to:

  trigger:
    - platform: state
      entity_id: binary_sensor.vardagsrums_sensor_1_motion_2
      to: 'on'
      for:
        seconds: 1

Otherwise everything looks fine.

Now you need to create the second automation to turn the lights off.

This is the reply I get when I copy/paste your message: (I just want to make sure that everything is correct before I proceed?)

It looks like the issue with the delay field has been resolved, and you have been advised to use a state trigger instead of a device trigger for your automation.

Using a state trigger allows you to specify the state that the trigger entity should be in for the automation to be triggered. In this case, the trigger entity is the motion sensor, and the state that you want to trigger the automation is “on” (indicating that motion has been detected).

To use a state trigger in your automation, you will need to modify the trigger field as follows:

  trigger:
    - platform: state
      entity_id: binary_sensor.vardagsrums_sensor_1_motion_2
      to: 'on'
      for:
        seconds: 1

The for field is optional, but it can be used to specify a duration for which the trigger state must be maintained before the automation is triggered. In this case, the for field is set to 1 second, which means that the automation will be triggered as soon as the motion sensor enters the “on” state.

Once you have modified the trigger field in your automation code, save the file and restart Home Assistant to apply the changes. The automation should now be active and will turn on the light in the living room when motion is detected.

To turn the light off after a period of no motion, you will need to create a second automation that is triggered by the absence of motion. You can do this by using a state trigger with the from field set to “on” and the to field set to “off”. For example:

- id: '12345679'
  alias: Turn off light after 10 minutes with no motion and TV off
  trigger:
    - platform: state
      entity_id: binary_sensor.vardagsrums_sensor_1_motion_2
      from: 'on'
      to: 'off'
      for:
        minutes: 10
  action:
    - service: light.turn_off
      entity_id: light.fonster
    - service: light.turn_off
      entity_id: light.taklampan_vardagsrummet
  condition:
    - condition: state
      entity_id: media_player.lg_webos_tv_oled77c16la
      state: 'off'

This automation will be triggered when the motion sensor transitions from the “on” state to the “off” state, and it will turn off the light in the living room if the transition is maintained for 10 minutes and the TV is off.

Where did that info in the post above come from? it looks like it was copied from somewhere else where some was giving you an explanation.

But regardless all of that looks correct except the advice that you need to restart HA for the automation changes to take effect. You can do that if you want or you can just go to developers tools->yaml tab and reload automations from there instead of restarting your entire HA instance.

I personally like to put the condition section after the trigger section when I write my automations but it doesn’t technically matter. it will work either way.

I just think it makes more sense from a program flow perspective → the automation gets triggered. then it checks the conditions. if the conditions are true then it runs the actions.

Oh I just copied and pasted your message into an AI. Given that I am still learning my ways around HA, and dont know anything about programming or yaml, I wanted to see that the information it gave me is correct.

The code that I first posted was suggested by the AI when I explained to it what I wanted to achieve, but since it gave me an error with the delay, I wanted to see if everything else looks fine, or if there are any additional errors before I proceed.

About the restart, I am curious, the AI suggested that I can place the code into a new .yaml file and put it into /config and HA will automatically load the code once restarted. But I dont know if that is true or not? If its true, why are all the other automations put inside the automations.yaml file?

yeah, that’s not true.

that code needs to be copied into the automations.yaml file. And then reload automations from the developers tools.

where do you access the AI that gave you that info? I’d like to check it out. It looks fairly impressive if it can give you (mostly) working code.

and another thing in the initial post I’m pretty sure the AI gave you the wrong trigger config.

I was initially going to ask you where you got that trigger code because it didn’t look right to me but I figured since I gave you a better trigger then there was no point in making things more complicated at the time.

Now that I know where it came from it makes sense that it wasn’t right and that trigger would have never worked as far as I know.

You can have a look at the AI here: ChatGPT: Optimizing Language Models for Dialogue

Ok thats good to know, I have noticed that it can give you different solutions to the same idea sometimes. But it is usually able to solve it eventually if you give it enough information.

I am sure that I was a bit vague when I explained my initial idea to it. Especially since english is not my native language.

I was going to try out the AI but it wants my phone number and I’m not into giving that unless I trust the site. I see no need for it to have that info.

So I guess I’m not trying it out. :laughing:

Yeah I was reluctant to put in my number as well, but figured its the only way. :slight_smile:
Oh well, I am sure I will have more questions in the future and pass along more code that it suggests along the way as I learn HA.

Thanks for helping me out tho.

1 Like