Light switch behaviour given last state

Hi there community.
Exposition: I have a door sensor and a swtich for the entrance lights. For now, it works like this:
When the door sensor changes to opened, the lights turn on, when door sensor closes, lights turn off in 60s. Ok, easy.
What I would achieve: When the door sensor is opened, IF the entrance lights are already on BEFORE opening the door sensor, when that door sensor closes, the lights should turn off immediatly; but if the lights are off BEFORE opening the door sensor, when the door sensor closes, the light should remain on 60s and then turn off.

As I have no idea with templates and programming, looking there and asking GPT, I’ve come with the next code, but HA says “Message malformed: extra keys not allowed @ data[‘action’][0][‘choose’][0][‘default’]
What’s malformed about this code?

alias: on off entrance lights
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.entrance_sensor_opening
    to: "on"
  - platform: state
    entity_id: binary_sensor.entrance_sensor_opening
    to: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ is_state('light.entrance_light', 'on') }}"
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.last_light_state
            data: {}
        default:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.last_light_state
      - conditions:
          - condition: template
            value_template: >-
              {{ is_state('binary_sensor.entrance_door_opening', 'off') and
              is_state('input_boolean.last_light_state', 'on') }}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: entrance
              entity_id: light.entrance_light
      - conditions:
          - condition: template
            value_template: >-
              {{ is_state('binary_sensor.entrance_door_opening', 'off') and
              is_state('input_boolean.last_light_state', 'off') }}
        sequence:
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              area_id: entrance
              entity_id: light.entrance_light
mode: single

Seems that the problem is with the DEFAULT but…what should be there?

NOTE: I’ve tried deleting just the line default: then I can save, but there’s no action when opening door in any situation.

Thanks in advance

You have two triggers - have you been able to achieve the desired result with two automations?

1 Like

Hi. Thanks for your answer.

Originally I had 2 automations but since using the ID I can have in the “same automation” 2 triggers for me it’s a lot cleaner having everything packet in 1 automation than having 2.
But my problem is still that I need to store the previous value of the light and use it when the door sensor knocks the trigger.
I mean the trigger will always be the door sensor and the action will be lights ON / OFF / delay 60s but must “remember” the last state of the light before triggering.
With 2 (or 3)automations (1 for ON, 1 for OFF, 1 for DELAY 60s) the problem is still the same. The one whose action is to turn the lights off immediately must “remember” the last state (ON) of light previous to the door sensor to trigger in.

Have a nice day

Perhaps the HACS integration Variables+History would help.

I’ll take a look right now. Thanks!

Well, I’ve just achieved the desired behaviour finally. I’ve tried variables+history, but in the end I’ve found a workaround with input booleans and forcing them to change the value.
The resulting code looks like this:

alias: Turn ON/OFF/DELAY entrance lights
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.entrance_door_opening
    to: "on"
  - platform: state
    entity_id: binary_sensor.entrance_door_opening
    to: "off"
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ is_state('light.entrance_light', 'off') }}"
        sequence:
          - service: light.turn_on
            data: {}
            target:
              device_id: "id_number"
          - service: input_boolean.turn_off
            data: {}
            enabled: true
            target:
              entity_id: input_boolean.last_state_light_entrance
      - conditions:
          - condition: template
            value_template: >-
              {{ is_state('binary_sensor.entrance_door_opening', 'off') and
              is_state('input_boolean.last_state_light_entrance', 'on') }}
        sequence:
          - service: light.turn_off
            data: {}
            target:
              area_id: entrance
              entity_id: light.entrance_light
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.last_state_light_entrance
      - conditions:
          - condition: template
            value_template: >-
              {{ is_state('binary_sensor.entrance_door_opening', 'off') and
              is_state('input_boolean.last_state_light_entrance', 'off') }}
        sequence:
          - delay:
              hours: 0
              minutes: 1
              seconds: 0
              milliseconds: 0
          - service: light.turn_off
            data: {}
            target:
              area_id: entrance
              entity_id: light.entrance_light
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.last_state_light_entrance
mode: single

The main problem as usual with people without coding formation is a basic. I didn’t declare in configuration.yaml the input boolean “last_state_light” so was impossible for the automation to update or change values.

I think that with variable+history should be possible to do the same, but I can’t get it to work.

Thanks.