Automation based on a trigger and a state

I have a zwave wall switch and a door switch connected to a MQTT sensor. I want to trigger a “turn off all lights and music” event when I leave the home, but to ensure that the kids dont press this button all the time killing the lights.

Option 1: When the door is open and the button is pressed the script will run
Option 2: When the door is opened and the button is pressed between 2 and 5 seconds after, the script will run

The MQTT sensor is named “sensor.dr_entre” and can be one of two states “Open” and “Closed”.

The wall switch is named is controlled with the following:

- alias: ENTRE - NODE 4
  trigger:
    platform: state
    entity_id: switch.logic_soft_zhc5010_wall_switch_switch_2_0_5
  action:
    service: shell_command.wallswitch_entre_4

Any ideas?

Best regards,
Tonkin

Everything to do that should be searchable on this forum. You’ll want the button press to be your trigger. Then use two conditions and “or” them together. Like:

- condition: or
    conditions:
      - condition: state
        entity_id: ...
        state: 'on'
      - condition: state
        entity_id: ...
        state: 'on'

Your first condition would check that the state of the door is open. The second would check that the door has been closed within your 2-5 second window. You should be able to do that with a template condition.