Only turn off light if it was turned on by a specific automation

I want to turn on my ceiling lights very dimmly during the night when motion is detected, and have them turn off when motion is no longer detected. But if I manually turn on the lights or dim them up, I don’t want them to turn off when motion is no longer detected. I figured the best way might be to have a condition on the no-motion trigger to only turn off if it was previously turned on by the motion-on trigger and noone has touch the lights since.

How can this be accomplished?

This will help you: State.context to UI automation - #24 by mbuscher

You could use a boolean helper to show that the specific automation has turned the lights on or you could add an extra action to all ways to turn on the light. This action will turn on or off that specific lights off automation, so the motion sensor turns it on and all others turn it off.

@pedolsky
I tried to look at the context object in state object but I could not find the automation that previously turned on the light.

I have a similar situation, I added a Boolean this gets turned on when I use the switch and the automation then has a condition, turn off lights only if Boolean is off…

action:
  - choose:
      - conditions:
          - condition: trigger
            id: garden-move
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 100
              kelvin: 2000
            target:
              entity_id:
                - light.garden_wall_lights_light
                - light.veranda_short_lights_light
                - light.veranda_long_lights_light
      - conditions:
          - condition: trigger
            id: garden-nomove
          - condition: and
            conditions:
              - condition: state
                entity_id: input_boolean.spa_mode
                state: "off"
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id:
                - light.garden_wall_lights_light
                - light.veranda_short_lights_light
                - light.veranda_long_lights_light
mode: single

That’s not necessary. Just add the conditions as shown in the link.

Try this test automation:


alias: 1 Test context
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.your_switch
    to: "on"
condition:
  - condition: not
    conditions:
      - condition: template
        value_template: "{{ trigger.to_state.context.parent_id == none }}"
      - condition: template
        value_template: "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: notify.persistent_notification
    data:
      message: |-
        context.id: {{ trigger.to_state.context.id }}

        parent_id: {{ trigger.to_state.context.parent_id }}

        user_id: {{ trigger.to_state.context.user_id }}