Automation works for Motion Light on/off but not for Motion Switch on/off (Code enclosed)

I have an automation running that turns on a light when motion is detected between certain hours and then off after 1 minute of no motion.

I need to do the same thing but with a switch

This works

alias: Family Room Late Night
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.family_room_motion
    to: 'on'
  - platform: state
    entity_id: binary_sensor.family_room_motion
    to: 'off'
    for: '0:01:00'
condition:
  - condition: time
    after: '23:00:00'
    before: '06:00:00'
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    entity_id: light.family_room_right
mode: single

This does not work

alias: Master Bedroom Motion
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'on'
  - platform: state
    entity_id: automation.master_bedroom_motion
    to: 'off'
    for: '00:02:00'
condition:
  - condition: time
    after: '09:30:00'
    before: '22:00:00'
action:
  - type: turn_on
    device_id: 3462fefb095ea75ce9446f63a27e1f4c
    entity_id: switch.bedroom_dresser_on_off
    domain: switch
mode: single

The second trigger in the second one is not the motion sensor, and your action is only turn on.

ah… Good catch @anon43302295 , I missed that
This will turn the switch on but not off
I suspect the issue is the Action Service which I modified from the other automation but, I’m not sure how to fix it.
Unless there is a better way to do this

alias: Master Bedroom Motion
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'off'
    for: '00:02:00'
condition:
  - condition: time
    after: '09:30:00'
    before: '22:00:00'
action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    entity_id: switch.bedroom_dresser_on_off
mode: single

Yeah, that should work fine now.

I actually used Toggle and it works as expected

alias: Master Bedroom Motion
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'off'
    for: '00:02:00'
condition:
  - condition: time
    after: '09:30:00'
    before: '22:00:00'
action:
  - type: toggle
    device_id: 3462fefb095ea75ce9446f63a27e1f4c
    entity_id: switch.bedroom_dresser_on_off
    domain: switch
mode: single

Imagine switch.bedroom_dresser_on_off is currently on.

Now the motion sensor turns to on.

What will the automation do?

It will toggle the switch to off.

Probably not the behavior you want (entering a lit room and causing the light to turn off).

I use a very similar automation and I suggest you consider changing the action to this:

action:
  - service: "switch.turn_{{ trigger.to_state.state }}"
    entity_id: switch.bedroom_dresser_on_off

When the first trigger occurs, the light is turned on.
When the second trigger occurs, the light is turned off.


EDIT

Corrected typo. Used light where I should have used switch.

Nope… Toggle is not a good solution… If I forget something and walk back into the room while the switch is on, it will turn off.

This is working and what I am staying with… Thank you

alias: Master Bedroom Motion
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_ias_zone
    to: 'off'
    for: '00:02:00'
condition:
  - condition: time
    after: '09:30:00'
    before: '22:00:00'
action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    entity_id: switch.bedroom_dresser_on_off
mode: single