Looking for automation help

Hi, I don’t know why I am having so much trouble with this, but I am trying to build an automation that when my Aqara motion sensor is triggered and it is dark, it turns an Aqara smart switch on. Then, once the sensor hasn’t sensed motion for 5 minutes, it turns the smart switch back off.

I have tried several different setups, but nothing seems to work. Does anyone have one built like this yet that you would be willing to share?

Thank you!
-David

  - alias: turn on
    trigger:
      platform: state
      entity_id: binary_sensor.your_motion_sensor_here
      to: 'on'
    condition:
      condition: state
      entity_id: sun.sun
      state: "below_horizon"
    action:
      - service: switch.turn_on
        entity_id: switch.your_switch_here
        
  - alias: turn off
    trigger:
      platform: state
      entity_id: binary_sensor.your_motion_sensor_here
      to: 'off'
      for:
        minutes: 5
    action:
      - service: switch.turn_off
        entity_id: switch.your_switch_here

Thank you, Tom!! This is so helpful!!!

One question - instead of using the sun below horizon condition, I would prefer to use the light sensor and set the condition for when the LUX level is 0. That way, the night light only comes on when all other lights are off. Just not sure how to correctly implement this - everything I have tried doesn’t work.

Thank you!

Change the condition to:

    condition:
      condition: numeric_state
      entity_id: sensor.your_lux_sensor_here
      below: 1

Thank you…I am trying that tonight!

Can I ask you one other question? I have another automation set that when my front porch lights are on (they come on at dusk through my Caseta hub) and I unlock the front door lock, the entryway lights turn on. I would like to put a condition that only turns the front entry lights on when the porch lights are on AND all downstairs lights are off. I don’t have any idea how to use all downstairs lights off as a condition. Do I just have to list each one with an AND?

  alias: Front Entry Light on when door unlocked
  description: ''
  trigger:
  - platform: device
    device_id: 8f42afe329a0f0739acba765f6ddd1f2
    domain: lock
    entity_id: lock.front_door
    type: unlocked
  condition:
  - condition: device
    type: is_on
    device_id: c08e0f19918bf4d855c35a74e46416eb
    entity_id: switch.exterior_porch_lights
    domain: switch
  action:
  - type: turn_on
    device_id: 4bf2dbcce8d891b846ef60cee5e5328e
    entity_id: switch.front_entryway_main_lights
    domain: switch
  mode: single

Thank you for all your help.
-David

No need. Conditions are AND by default. Just list the extra condition under the existing one.

but if I wanted it to only turn on if all downstairs lights are off, do I need to list each light as a separate condition?

Sorry, one other question. On the first automation you were helping me with, I am getting this error:

duplicated mapping key at line 72, column -118:
      trigger:
      ^

It is for the second trigger to turn the light off after 2 minutes:

  alias: Kids bathroom nightlight
  trigger:
      platform: state
      entity_id: binary_sensor.presence_5
      to: 'on'
  condition:
      condition: numeric_state
      entity_id: sensor.lightlevel_6
      below: 1
  action:
      - service: switch.turn_on
        entity_id: switch.kids_bathroom_plug
  trigger:
      platform: state
      entity_id: binary_sensor.presence_5
      to: 'off'
      for:
        minutes: 5
  action:
      - service: switch.turn_off
        entity_id: switch.kids_bathroom_plug

In YAML, a list is denoted by a leading -. Look at @tom_l’s first example: the two separate automations are items in the list, both starting with - alias:.

You need to add - alias: some description at the start of your second automation, in the line before the trigger.

As it stands, you have one automation with two triggers, which is causing the error.

That also applies if you want multiple conditions. Your request for all lights to be off can be written like this (docs), and I’ve added the sun condition back in as an example to show the format:

condition:
  - condition: state
    entity_id:
      - light.first
      - light.second
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: "below_horizon"

The automation documentation is extensive and high-quality. Please take some time to read it carefully.

1 Like

The easiest way around that would be to make a light group containing all the downstairs lights. Then use that in the condition.

@Troon - yup, that did it! When I was modifying, I accidentally got rid of that second alias. Thank you!!!

@tom_l - ah, yeah - that makes the most sense. I will give that a try and will let you know if I run into any issues.

Thank you so much - this was extremely helpful. I am trying to get comfortable with building these automations…then I think I will need to go through and assign better entity names to my devices and start to break off my automation YAML to make it a bit easier to nagivate.

Have a great night!

-David

Argh…I do have one other question…my Lutron Caseta dimmer lights are light.entity but the ones on on/off switches are listed as switch.entity.

Does this matter? Do I (or is there any reason) to change the light.entity ones to switch.entity?

I guess I don’t understand the difference and if there is any reason why HA set them up this way instead of all being lights.

light:
  - platform: group
    name: Downstairs Lights
    entities:
      - light.dining_room_ceiling_lights
      - light.family_room_main_lights
      - switch.kitchen_main_lights
      - switch.playroom_light

Thanks,
David

Yes it matters. You have two options:

  1. convert your switches to lights, and use those in the light group.

Or

  1. use the entities as they are (switches and lights) in a normal group instead of a light group:

Personally I’d go with option 1 as it also has the advantage that your switches now look like lights in Lovelace.

Ah, ok, that makes sense now. And that is good to know for the switch I am using as a nightlight in the bathroom. I will go and add all those to the config.

Last question for the night, I promise!!

Adding the group as a condition - not exactly sure of the syntax. Am I close here?

  - condition: group
    type: is_off
    entity_id: group.downstairs_lights
    domain: group    

Thank you again.

The light group is a bit different than normal groups in that it creates a light not a group. So your condition would be like this:

   - condition: state
     entity_id: light.downstairs_lights
     state: ‘off’

Ugh. I’m on my mobile. Those are the wrong type of quotes around off, but you get the idea.

Ah, ok…that makes sense.

Now really, truly last question for the night (promise!!!)

In my config, I added the following:

light:
  - platform: switch
    name: Kitchen Lights
    entity_id: switch.kitchen_main_lights
  - platform: switch
    name: Playroom Light
    entity_id: switch.playroom_light
  - platform: switch
    name: Front Entry Lights
    entity_id: switch.front_entryway_main_lights
  - platform: switch
    name: Front Porch Lights
    entity_id: switch.exterior_porch_lights
  - platform: switch
    name: Backyard Lights
    entity_id: switch.exterior_backyard_lights
  - platform: switch
    name: Front Side Garage Light
    entity_id: switch.exterior_side_garage_lights
  - platform: switch
    name: Front Pool Light
    entity_id: switch.exterior_pool

  - platform: group
    name: Downstairs Lights
    entities:
      - light.dining_room_ceiling_lights
      - light.family_room_main_lights
      - switch.playroom_light
      - switch.kitchen_main_lights

But when I validate my config, I get this error:

Invalid config for [light.group]: Entity ID 'switch.playroom_light' does not belong to domain 'light' for dictionary value @ data['entities']. Got ['light.dining_room_ceiling_lights', 'light.family_room_main_lights', 'switch.playroom_light', 'switch.kitchen_main_lights']. (See ?, line ?).

I assume I’m just doing something wrong, but I don’t know what.

Thanks,
David

  - platform: group
    name: Downstairs Lights
    entities:
      - light.dining_room_ceiling_lights
      - light.family_room_main_lights
      - switch.playroom_light  ## These two need to be lights. You made the lights above.
      - switch.kitchen_main_lights ## 

Hey @tom_l - I am adapting this automation (turning on the entryway lights when the front door is unlocked) to turning on my kitchen light when the MyQ garage door is open.

The part I am not clear on is how to determine which device to use (in this case, the garage door is cover.garage_door and the MyQ Gateway is a binary sensor. So in this case, how would you know which entity is the correct one to use?

And once you do figure out which entity, how do you find the states (or type) the entity uses to know which to use?

Thanks for the help!!

-David

Think about it. What triggers your actions?

The garage door opening. So use that.

You can see the current state of entities in the Developer Tools States menu.

That makes sense. Thank you! I will give it a try.

If I am looking up states in the Dev Tools states menu, if I want to check the other states, do I just have to put the device in another state and see how it is being reported in the States menu?

Thanks!
-David