Need help with automation

I’m trying to do an automation and could use some help.

I want to do the following:

rule 1
trigger: DOOR open received or MOTION active received
event: turn on light, start timer (set countdown timer = 10 minutes)

rule 2
trigger: timer expires
event: turn off light

DOOR and MOTION are both mqtt binary sensors.

So I’d like the light to turn on and the timer to be set to 10 minutes whenever the mqtt on payload is received for either the door or the motion detector. When the timer expires, the light goes off. I don’t want to use the off payload for either the door or the motion detector.

Can anyone point me in the right direction?

Most of what you need is in the documentation here:

The other piece of information you need is that you can have multiple trigger items, and when any one of them is triggered, the automation is activated.

I actually prefer the old automation they had before which used scripts and reset them with different triggers.

Turn on lights with a resettable off timer

This recipe will turn on a light when there is motion and turn off the light when ten minutes has passed without any motion events .

automation:
  alias: Turn on kitchen lights when there is movement 
  trigger:
    - platform: state
      entity_id: sensor.motion_sensor
      to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

script:
  timed_lamp:
    alias: "Turn on lamp and set timer"
    sequence:
      # Cancel ev. old timers 
      - execute_service: script.turn_off
        service_data: 
           entity_id: script.timer_off
      - execute_service: light.turn_on
        service_data:
          entity_id: light.kitchen
      # Set new timer 
      - execute_service: script.turn_on
        service_data:
          entity_id: script.timer_off

  timer_off:
    alias: "Turn off lamp after 10 minutes"
    sequence:
      - delay:
          minutes: 10
      - execute_service: light.turn_off
        service_data: 
          entity_id: light.kitchen

Thanks for the replies. I’d seen the example and tried this:

# BASEMENT HALLWAY LIGHT
- action:
  - alias: ''
    data:
      entity_id: switch.bsmt_hallway_scene
    service: switch.turn_on
  alias: Turn on basement hallway light when there is movement
  id: '1498615122979'
  trigger:
  - entity_id: binary_sensor.status_bsmt_hallway_motion
    platform: state
    to: 'on'
  - entity_id: binary_sensor.w800_device_2
    platform: state
    to: 'on'
# BASEMENT HALLWAY LIGHT
- action:
  - data:
      entity_id: switch.bsmt_hallway_scene
    service: switch.turn_off
  alias: Turn off basement hallway light 10 minutes after last movement
  id: '1498615786618'
  trigger:
  - entity_id: binary_sensor.status_bsmt_hallway_motion
    for:
      minutes: 10
    platform: state
    to: 'off'
  - entity_id: binary_sensor.w800_device_2
    for:
      minutes: 10
    platform: state
    to: 'off'

It doesn’t work the way I want it to. It relies on off states and the timer doesn’t seem to work correctly. I’m just starting and think I’m missing some basic information that I haven’t found on the site.

Is there a way to create a real timer? I think the 2nd rule above turns the light off if either sensor has been in the off state for 10 minutes. I want to turn the light off if the on payload hasn’t bee received for 10 minutes for either sensor.

Is there an alternative for creating automation rules in a conventional programming language?

You need to add 2 conditions for binary_sensor.status_bsmt_hallway_motion and binary_sensor.w800_device_2 to ensure both are off for 10 minutes. Without the condition, it’s functioning as an OR. The condition can make it function like AND. Check out https://home-assistant.io/docs/automation/condition/ for more details.

Thanks for that example, and apologies for pulling up an old thread. I’m just starting out with hass and I’m trying to implement your resettable motion sensor timer example. I get the following message “WARNING (MainThread) [homeassistant.core] Unable to find service script/turn_on” in relation to the script.

I noted that your example uses ‘execute_service’ and ‘service_data’ whereas https://home-assistant.io/docs/scripts/ just uses ‘execute’ and ‘data’. Are they equivalent, or is this example out of date with respect to current syntax, or something else? I don’t see ‘execute_service’ or ‘service_data’ referenced on https://home-assistant.io/docs/scripts/service-calls/ either so I’m a little stumped.