Combine on and off into the same automation

I know I must be missing something extremely obvious here but I’m at a loss for how to get this done. Apologies if my search totally missed where this is documented.

I have two simple automations. The first one, when the main light switch turns on, turns on 3 other plug-in switches. The second one is the opposite, main light switch turns off, and then those 3 other plug-in switches turn off.

As you can see, here is the simple automation.

Even though the items under the ‘Then do’ are called light.garage.x they are really just plugs that turn on and off and happen to turn on plug-in lights in this case.

Thank you for the help.

If you post the yaml (properly formatted using three backticks) we can edit it for you to show you how.

2 Likes

Hi there JoshFink,
There may be a solution but there is a slight problem.

Sadly we’re not mind readers (any more anyway, not after the last time we tried). Please share the YAML and any errors so we can see what you’ve done.

There is a simple automation here which turns lights on and off from a motion sensor. You may be able to adapt it to fit your situation.

Motion activated lights automation

The key point is that it uses trigger.to_state.state in the action, so that the action reflects the state of the trigger.

Here are some example of on and off in the same automation

- id: 'Garage Light on and off Via Motion'
  alias: Garage Lights On and Off via Motion
  description: Control Garage Lights with Shelly Motion Detection
  mode: single
  trigger:
  - id: 'on'
    platform: state
    entity_id: binary_sensor.garage_motion_sensors_group
    to: 'on'
  - id: 'off' 
    platform: state
    entity_id: binary_sensor.garage_motion_sensors_group
    to: 'off'
    for:
      minutes: 10
  condition: []
  action:
  - choose:
    - conditions:
      - "{{ trigger.id == 'on' }}"
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.garage_overhead_lights
    - conditions:
      - "{{ trigger.id == 'off' }}"
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.garage_overhead_lights
    default: []

For the Coffee Machine

- id: 'Turn on off coffee bar'
  alias: Turn On Off Coffee Bar
  description: Coffee Bar On Off
  trigger:
  - id: 'on'
    platform: time
    at: input_datetime.coffee_on
  - id: 'off'
    platform: time
    at: input_datetime.coffee_off
  condition: []
  action:
  - service: switch.turn_{{trigger.id}}
    target:
      entity_id:
      - switch.sonoff_100153b891
  mode: single

Pantry Light from Door Sensor

- id: 'Pantry - Turn on-off light if door is open-closed'
  alias: Pantry - on-off
  description: 'Turn on or off pantry light if the door is open or closed respectively'
  trigger:
  - platform: state
    entity_id: binary_sensor.pantry_door_sensor_access_control_window_door_is_open_2
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.pantry_door_sensor_access_control_window_door_is_open_2
    from: 'on'
    to: 'off'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.pantry_door_sensor_access_control_window_door_is_open_2
        state: 'on'
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.pantry_light
    - conditions:
      - condition: state
        entity_id: binary_sensor.pantry_door_sensor_access_control_window_door_is_open_2
        state: 'off'
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.pantry_light
    default: []
  mode: single

Outside Lights

- id: 'Outside Lights On and Off'
  alias: Outside Lights On and Off
  description: Turn on and off outside lights (garage and patio) at sunset and at 10:00
  mode: single
  trigger:
  - id: 'on'
    platform: sun
    event: sunset
  - id: 'off'
    platform: time
    at: "22:10:00"
  condition: []
  action:
  - choose:
    - conditions:  
      - "{{ trigger.id == 'on'}}"
      sequence:
      - service: switch.turn_on
        target:
          entity_id:
          - switch.s_outside_lights
          - switch.outside_back_patio_light
    - conditions:  
      - "{{trigger.id == 'off'}}"
      sequence:
      - service: switch.turn_off
        target:
          entity_id:
          - switch.s_outside_lights
          - switch.outside_back_patio_light
    default: []

Hope this gives you some automations to change and modify to fit you situation.

1 Like

Holy cow. Apologies. I never received a notification there were responses. Let me dig through these. Thank you for all that replied.