Motion Lights based off of input select drop down

I have a dropdown input select configured with 3 options, day, evening, night. I set the input select with multiple button taps on a homeseer dimmer. I’d like to tie these options to different brightness motion lights. Meaning, if the house is in night mode and there is motion, dont turn on the light. If the house is in day mode and there is motion, turn the light on 100%, and turn off 5 minutes later once motion stops. If the house is in evening mode and there is motion, turn the light on 10%, and turn off 5 minutes later once motion stops.

I cant seem to figure out how I can include this in one automation. It seems as if i need to be able to define a condition for each action, but i cant figure out how to do so.

thanks in advance for any suggestions.

- alias: your automation
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_sensor
    to: 'off'
    for: '00:05:00'
  action:
    - service: light.turn_on
      data:
        entity_id: light.your_light
        brightness_pct: >
          {% if is_state('binary_sensor.motion_sensor', 'on') %}
            {% if is_state('input_select.your_input', 'day')%}
              100
            {% elif is_state('input_select.your_input', 'evening') %}
              10
            {% else %}
              0
            {% endif%}
          {% else %}
            0
          {% endif %}

I hope it works, I cannot test it. I set two “else 0” to avoid log errors, you can skip the former if you don’t mind that, like this:

    - service: light.turn_on
      data:
        entity_id: light.your_light
        brightness_pct: >
          {% if is_state('binary_sensor.motion_sensor', 'on') %}
            {% if is_state('input_select.your_input', 'day')%}
              100
            {% elif is_state('input_select.your_input', 'evening') %}
              10
            {% endif%}
          {% else %}
            0
          {% endif %}

Somebody might be able to help you better though

This is what I came up with. this code is a result of what i configured in the automation GUI. How do I edit it in the GUI to shut off after 5 minutes

id: '1634785178928'
alias: Mode Motion Lights
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 6817295ed58cb513e68d2d06ae1cba08
    entity_id: binary_sensor.master_bedroom_motion_ias_zone
    domain: binary_sensor
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.time_of_day
            state: Day
        sequence:
          - type: turn_on
            device_id: a2a5e56387907f0a71154aa9f8572a85
            entity_id: light.master_bathroom_light_3
            domain: light
            brightness_pct: 100
      - conditions:
          - condition: state
            entity_id: input_select.time_of_day
            state: Evening
        sequence:
          - type: turn_on
            device_id: a2a5e56387907f0a71154aa9f8572a85
            entity_id: light.master_bathroom_light_3
            domain: light
            brightness_pct: 20
    default: []
mode: single```

Mine does include the off part. You could achieve the same thing with the choose clause, with a way longer code though.

If you want to use the ui, set your motion sensor to off for 5 minutes as a trigger as well. Then in every choose clause you should add the current state of the motion sensor as a condition as well as the state of the input_select…

  • when motion sensor is on and input select is day, then do
  • when motion sensor is on and input select is evening, then do
  • default, light off

This also adds the feature of not turning on the light if it’s already on and not turning it off if it’s already off (implemented in the second condition).

alias: example 1
variables:
  light: "light.your_light" 
  mode: "{{ states('input_select.your_input' }}"
trigger:
  - platform: state
    entity_id: binary_sensor.your_motion
    to: 'on'
  - platform: state
    entity_id: binary_sensor.your_motion
    to: 'off'
    for: '00:05:00'
condition:
  - "{{ mode in ['day', 'evening'] }}"
  - "{{ (trigger.to_state.state == 'on' and is_state(light, 'off')) or
         (trigger.to_state.state == 'off' and is_state(light, 'on')) }}"
action:
  - choose:
      - conditions: "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - service: light.turn_on
            target:
              entity_id: "{{ light }}"
            data:
              brightness_pct: "{{ 100 if mode == 'day' else 10 }}"
    default:
      - service: light.turn_off
        target:
          entity_id: "{{ light }}"
mode: single


A shorter way to do the action

alias: example 2
variables:
  light: "light.your_light" 
  mode: "{{ states('input_select.your_input' }}"
trigger:
  - platform: state
    entity_id: binary_sensor.your_motion
    to: 'on'
  - platform: state
    entity_id: binary_sensor.your_motion
    to: 'off'
    for: '00:05:00'
condition:
  - "{{ mode in ['day', 'evening'] }}"
  - "{{ (trigger.to_state.state == 'on' and is_state(light, 'off')) or
         (trigger.to_state.state == 'off' and is_state(light, 'on')) }}"
action:
  - service: light.turn_on
    target:
      entity_id: "{{ light }}"
    data:
      brightness_pct: "{{ 100 if mode == 'day' else 10 if mode == 'evening' else 0 }}"
mode: single

He’s using the GUI for building his automations… you might scare him with all that :blush:

Thanks. Unfortunately, I am still using the GUI at this point. Only a week in at this point. Below is what Im looking to accomplish in a bit more detail, as well as my webcore piston (that i did code, not use gui :smile: ).

more details
New to HA. I’m coming from SmartThings with webcore. In webcore, I had most of my lights setup as follows:

When in night mode, motion sensor would not turn on light

When In day mode, motion sensor would turn on light @ 100%, then shut off 5 minutes after motion stops.

When In evening mode, motion sensor would turn on light @ 10%, then shut off 5 minutes after motion stops.

I also had a virtual switch for each room that would be used to deactivate the motion lighting. That way the lights stay on or off, without auto turning on or off. I might use this to keep the lights off while watching tv, or keeping the lights on if stationary at a desk.

In addition, I also had it setup so that if the light were on, and the setting to disable the motion sensor is off, and there was no motion for 30 minutes, it would turn the lights off, and turn the motion sensor back on so it starts activating lights again w motion.

It also does the opposite. If the light was off, and the setting to disable the motion sensor is off, and there was no motion for 30 minutes, it would keep the lights off and turn the motion sensor back on so it starts activating the lights again according to what the mode was set to, day, evening, night.

I defined a drop down list in HA for the different modes. I also came cross the blueprint yet another motion automation. This has most of what I’m looking for except that it doesn’t take the day, night, evening setting into consideration.

Thanks for any guidance the community can provide.

old webcore piston that did what i described above.

Doesn’t matter. Copy-paste the posted example into the Automation Editor while it’s in YAML mode. Save and done.

Not all that different from sharing pistons.

I told you how to do it through the GUI a couple of posts above.

The Home Assistant equivalent would be an Input Boolean.

If you want one automation to handle multiple motion sensors (where each motion sensor represents a specific room), you would list them in the first State Trigger (see the first example in the documentation). FWIW, the piston example you provided appears to be focused on just one motion sensor (identified in the define section).

If i Look t example two, i want to make sure I understand you code:

  • your defining the variable “light” as the light in the specific room
  • your defining the variable “mode” the input select is set to, ex day, evening, night
  • trigger - can you help me understand why there are two trigger statements, one that says if the motion changes to on, and another that says if the motion changes to off for 5 minutes. I understand that i need the two triggers to perform two different actions, i just dont understand why they are together.
  • in condition - you are saying only do the action if the mode is day or evening?
  • in condition - you are saying move forward if the motion changes to on and the light is currently off or if the motion changes to off and the light is currently on? With the second statement here, why isnt the 5:00 needed in this statement?
  • for action the service light.turn_on means we are going to turn a light on
  • why is traget empty under action?
  • entity_id: is referring back the set variable
  • data: you are defining the brightness percentage as 100 for day and 10 for evening
  • mode: single? what does that mean?

Few followup questions:

  • What editor to you use to make these files - im currently using file editor to make small changes to my configuration.yaml file. is there a better editor?
  • i am just using one motion sensor, if i use an input boolean and name it something like master bedroom light motion activated, I can set that to true or false?
  • if so, would i add that to the conditions. something like if master bedroom light motion activated = true? that way it checks for that too?
  • to take care of the issue if someone sets the master bedroom light motion activated to false, forgets about it, leaves the room, i’d like to set that boolean back to true and turn the light off if it was left on. Can I do that in this same file?

Sorry for the wordiness, i just wanted to make sure I understand and provide enough detail. Thank you, and the community for your support.

trigger - can you help me understand why there are two trigger statements

Because there are two events of interest, when motion is detected and when there has been no motion for 5 minutes.

in condition - you are saying only do the action if the mode is day or evening?

Correct. According to your requirements, if it’s night there’s no need to control the light.

in condition - you are saying move forward if the motion changes to on and the light is currently off or if the motion changes to off and the light is currently on?

Yes, it prevents sending needless commands to the light.

With the second statement here, why isnt the 5:00 needed in this statement?

It’s not need because the second trigger only occurs after the state has been off for 5 minutes.

why is traget empty under action?

It’s not empty, it contains entity_id.
Refer to the examples in Automation Actions

mode: single? what does that mean?

Refer to Script Modes

What editor to you use to make these files

To make the one I posted, I simply used the default Text Editor application in Ubuntu. However, I normally use Visual Studio Code to compose my automations. I have installed on my laptop but it’s also available as as Add-on from Add-On Store.

i am just using one motion sensor, if i use an input boolean and name it something like master bedroom light motion activated, I can set that to true or false?

Yes but you set an input_boolean to on or off (not true or false).

if so, would i add that to the conditions. something like if master bedroom light motion activated = true? that way it checks for that too?
Yes. The condition can confirm the input_boolean is on before allowing the automation to proceed to execute its action.

to take care of the issue if someone sets the master bedroom light motion activated to false, forgets about it, leaves the room, i’d like to set that boolean back to true and turn the light off if it was left on. Can I do that in this same file?

You can but it will require enhancing the automation’s trigger and condition.

Currently, the automation’s trigger and condition are designed with the assumption that only the motion sensor serves to trigger this automation (via the two State Triggers). If you add a third State Trigger to monitor if the input_boolean has been off for, say, 20 minutes, that activity is unrelated to the motion sensor. As a consequence, the condition needs to understand which one of the three triggers has just occurred.

could it be configured to not monitor a 3rd inout boolean, but instead monitor the same motion detector for inactivity but for a longer period of time?

Thanks

Yes but it would require a third State Trigger and additional logic in the condition. It’s all feasible but you should know it requires modifications to the example posted above.

Can someone point me to a good resource that I can use to learn how to create automations via a file/code instead of in the automation gui?

Trying to make my way through this. Added the below to my automations.yaml file, but its not working. I can see the motion detector detects the motion in the dashboard.

- id: "Master Bedroom Motion"
  variables:
    light: "light.master_bedroom_light"
    mode: "{{ states('input_select.time_of_day' }}"
  trigger:
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "on"
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "off"
      for: "00:05:00"
  condition:
    - "{{ mode in ['Day', 'Evening'] }}"
    - "{{ (trigger.to_state.state == 'on' and is_state(light, 'off')) or
      (trigger.to_state.state == 'off' and is_state(light, 'on')) }}"
  action:
    - service: light.turn_on
      target:
        entity_id: "{{ light }}"
      data:
        brightness_pct: "{{ 100 if mode == 'day' else 10 if mode == 'evening' else 0 }}"
  mode: single```

got it, was missing a )

- id: "Master Bedroom Motion"
  variables:
    light: "light.master_bedroom_light"
    mode: "{{ states('input_select.time_of_day') }}"
  trigger:
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "on"
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "off"
      for: "00:05:00"
  condition:
    - "{{ mode in ['Day', 'Evening'] }}"
    - "{{ (trigger.to_state.state == 'on' and is_state(light, 'off')) or
      (trigger.to_state.state == 'off' and is_state(light, 'on')) }}"
  action:
    - service: light.turn_on
      target:
        entity_id: "{{ light }}"
      data:
        brightness_pct: "{{ 100 if mode == 'Day' else 10 if mode == 'Evening' else 0 }}"
  mode: single```

So, the code to works to turn to lights on at 100 and 10% depending on the mode, however, the light will not turn off with inactivity after 5 minutes. Does anyone know what might be wrong?

had to make some modifications. the below works

- id: "Master Bedroom Motion"
  variables:
    light: "light.master_bedroom_light"
    mode: "{{ states('input_select.time_of_day') }}"
  trigger:
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "on"
    - platform: state
      entity_id: binary_sensor.master_bedroom_motion_ias_zone
      to: "off"
      for: "00:05:00"
  condition:
    - "{{ mode in ['Day', 'Evening'] }}"
    - "{{ (trigger.to_state.state == 'on' and is_state(light, 'off')) or
      (trigger.to_state.state == 'off' and is_state(light, 'on')) }}"
  action:
    - service: light.turn_on
      target:
        entity_id: "{{ light }}"
      data:
        brightness_pct: >
          {% if trigger.to_state.state == 'on' %}
            {{ 100 if mode == 'Day' else 10 }}
          {% else %}
            0
          {% endif %}
  mode: single

Hey @tommyk203, this guy’s website helped me a lot when I began coding. There are some tutorials for begginers that turned out very useful.