Automation light turn off only wenn turn on by automation

I want to control a light with a pir sensor.

If motion is detected, turn on light (with several conditions)
If pir sensor goes off (no motion during 1:30), turn off light - but only, if turned on by automation.

this is my code for first automation:

  alias: Licht Gallerie bei Bewegung
  description: Licht in Gallerie einschalten wenn Cover Strasse geschlossen ist
  trigger:
  - platform: state
    entity_id: binary_sensor.deconz_pir_gallerie_rechts
    to: 'on'
  condition:
  - condition: and
    conditions:
    - condition: numeric_state
      entity_id: sensor.deconz_pir_gallerie_rechts_lux
      below: '10'
    - condition: state
      entity_id: switch.shelly_1pm_entree_gallerie
      state: 'off'
    - condition: time
      before: '23:00'
      after: 06:00
    - condition: state
      entity_id: cover.entree
      state: closed
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.shelly_1pm_entree_gallerie
  mode: single

this is my actual second automation to turn off the light, which always turn off the light

  alias: Licht Gallerie ausschalten
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.deconz_pir_gallerie_rechts
    to: 'off'
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    entity_id: switch.shelly_1pm_entree_gallerie
  mode: single

what is the best way to get the goal?
I tought, I use a helper who is put to on by first automation and use this as condition in second automation (and put it back to off).

Is there a better solution?

Create an input_boolean to monitor whether the switch was turned on by the automation…

input_boolean:
  automated_light:

Turn it on with the switch in the automation…

alias: Licht Gallerie bei Bewegung
description: Licht in Gallerie einschalten wenn Cover Strasse geschlossen ist
trigger:
  platform: state
  entity_id: binary_sensor.deconz_pir_gallerie_rechts
  to: 'on'
condition:
  - condition: numeric_state
    entity_id: sensor.deconz_pir_gallerie_rechts_lux
    below: 10
  - condition: state
    entity_id: switch.shelly_1pm_entree_gallerie
    state: 'off'
  - condition: time
    before: '23:00:00'
    after: '06:00:00' 
  - condition: state
    entity_id: cover.entree
    state: 'closed' 
action:
  service: homeassistant.turn_on
  entity_id:
    - switch.shelly_1pm_entree_gallerie
    - input_boolean.automated_light

Use a condition in the turn off automation so it only runs if the input boolean is on, and if it is turn it off with the switch…

alias: Licht Gallerie ausschalten
trigger:
  platform: state
  entity_id: binary_sensor.deconz_pir_gallerie_rechts
  to: 'off'
condition:
  condition: state
  entity_id: input_boolean.automated_light
  state: 'on'
action:
  service: homeassistant.turn_off
  entity_id:
    - switch.shelly_1pm_entree_gallerie
    - input_boolean.automated_light
1 Like

thanks for your quick reply, so I do it with a Toggle-Helper (=input.boolean). :relaxed:

1 Like