Coupling motion sensor with light

I have an automation that switches the light on when the motion sensor goes from off to on. And if it goes from on to off, another automation switches the light off. Can I do that in ONE automation?
That is:

  trigger:
  - entity_id: binary_sensor.sensor_motion

But how can I make the action dependend on the motion state?
Will this work:

  action:
  - service_template: >- 
        light.turn_{{ states.binary_sensor.sensor_motion.state  }}
    data:
      entity_id: light.light1

Yes, but typically it’s done using the trigger variable. And, as a shortcut, a service call allows you to not have to put entity_id under data:. So:

  action:
  - service_template: "light.turn_{{ trigger.to_state.state }}"
    entity_id: light.light1
1 Like

Great, thanks!

What if a want to control the brightness also, “brightness: 50”, then I need “data”, or not?

You can’t easily combine the automations if you are setting the brightness as the light.turn_off service does not accept this data and will generate an error and halt execution of the actions.

The complexity required to overcome this limitation is not worth the effort. Use two automations.

Ok thank you!