Turn on lights with first motion in morning

I’m a fiddler, not a programmer and as such, I’ve benefited greatly from all of the bits of code that users have posted in the forums. Thanks! Here’s my contribution.

What it does: When I first come downstairs, a motion sensor in my livingroom senses me and turns on some lights. Kind of a “welcome to the day” automation.

This includes: creating and changing the value of an input_boolean, reacting to time and to sunrise, using conditions for an action, using value_templates for conditions, exiting an automation by using a conditon inside the action

input_boolean:
  trigger_first_morning:
    name: Waiting for first morning motion
    icon: mdi:kettle

## These first two control t input_boolean that allows the "first morning action" to occur
## If the action is triggered, it will also disable this boolean. This assumes you have the sun platform enabled.

automation:
#turns it on at 5am
  - alias: Enable First Morning Trigger
    trigger:
      - platform: time
        after: '5:00'
    action: 
      service: homeassistant.turn_on
      entity_id: input_boolean.trigger_first_morning

# turns it off an hour after sunrise
  - alias: Disable First Morning Trigger
    trigger:
      - platform: sun
        event: sunrise
        offset: "01:00:00"
    action: 
      service: homeassistant.turn_off
      entity_id: input_boolean.trigger_first_morning


      
# This is the main automation. It triggers when my motion sensor is triggered
# (in this case, a motion sensor from a security system attached to my Vera)
  - alias: First Morning Motion
    trigger:
      platform: state
      entity_id: binary_sensor.livingroom_motion
      to: 'on'
    # only complete the automation if we're still waiting for the first motion
    condition:
        condition: state
        entity_id: input_boolean.trigger_first_morning
        state: 'on'
        
    action:
      # turn off the "waiting" boolean regardless of whether lights will turn on
      # so that this happens only once
      - service: homeassistant.turn_off
        entity_id: input_boolean.trigger_first_morning
        
      # But only turn on lights if the living room and kitchen lights are off or dimmed
      # If a condition tests false, the automation will end 
      - condition: and
        conditions: 
          - condition: numeric_state
            entity_id: light.livingroom_ec
            # if light is off, force a 0, otherwise use the brightness value
            value_template: '{% if states.light.livingroom_ec.state == "on"  %}{{ states.light.livingroom_ec.attributes.brightness }}{% else %}0{% endif %}'
            # brightness below 50% (255 = 100%)
            below: 127
          - condition: numeric_state
            entity_id: light.kitchen_bar
            value_template: '{% if states.light.kitchen_bar.state == "on"  %}{{ states.light.kitchen_bar.attributes.brightness }}{% else %}0{% endif %}'
            below: 127
          - condition: numeric_state
            entity_id: light.kitchen_ceiling
            value_template: '{% if states.light.kitchen_ceiling.state == "on"  %}{{ states.light.kitchen_ceiling.attributes.brightness }}{% else %}0{% endif %}'
            below: 127
                
      # Trigger a scene
      # You could add as many services or scenes as you'd like
      - service: scene.turn_on
        entity_id: scene.morning_first_motion

      
# here's the scene that gets called. Lights in 
# my living room and kitchen turn on.
scene:
  - name: Morning First Motion
    entities:
      light.kitchen_ceiling:
        state: on
        brightness: 127
      light.kitchen_bar:
        state: on
        brightness: 178
      light.kitchen_above_cabinet:
        state: on
        brightness: 178
      light.livingroom_ec:
        state: on
        brightness: 153
      light.livingroom_track:
        state: on
        brightness: 153
9 Likes

This is a great starting project for most and a good example. You really should consider adding it to the cookbook.

1 Like

I agree!

We need more examples of this magnitude to help instruct new users on how you can use the different parts of the system.

I myself is quite new to HASS and I have been frustrated on multiple occasions due to the lack of good examples. This is of course the usual behaviour with open source and I would like to give my kudos to the developers for producing such a potent system.

Hopefully I can bring my own examples to help newcomers in the future

/R

2 Likes

Thanks. My Git skills are too rusty to figure that out.

Thank you for sharing this @ih8gates - I’ve been trying to work out how to turn my coffee thingo on when I first get up but not keep turning it on. Didn’t even occur to me to use an input boolean.

1 Like

Same here. There seems to be no “simple” examples. I’m missing something in understanding this. I read one post where the guy was a programmer but couldn’t get his head wrapped around this. I’m similar. I do a lot of Programmable Logic Control and SCADA stuff. Even some of our older stuff was similar to this “type it out” over graphic programming. But there doesn’t seem to be a cheat sheet for the available HA commands. Every day I learn something new but still struggle with the syntax. Was going to post and ask for a simple On/Off four light example. I have 4 Cree bulbs which are seen by the Wink App and also have been discovered by the HA. I can turn them On and Off with HA. But when I try to program something I usually get an error and it doesn’t allow HA to start. So I go back into configuration, remove it, save it. restart HA, and it comes back. Hey I did at least get the Wink API programmed and that works…yeah…

1 Like

Even if you don’t want to dive into Git, you could always do a gist. Super easy to add to the Cookbook.

OK. How do I add it to the cookbook?

I thought you could use the gist. they changed the format. :frowning: I created a stub file for you in the cookbook though with most of the stuff. Check out the pull request and edit/adjust as necessary.

https://github.com/home-assistant/home-assistant.github.io/pull/1428/files

Carlo

1 Like