Light up a LED depending on sensor data

i want to light up a LED depending on sensor data
I use this code, in conf.yaml

automation :
 alias: turn on light if distance is less than 5cm.
 trigger:
  platform: state
  entity_id: sensor.distance
  below: 5
  for:
     seconds: 3
 action:
  service: switch.turn_on
  entity_id: switch.control

and no thing happened, what is the wrong in it ?

It could just be due to the indenting. You’ve only indented by one space when it should be two like this:

automation:
- alias: 'turn on light if distance is less than 5cm.'
  trigger:
    platform: state
    entity_id: sensor.distance
    below: 5
    for:
      seconds: 3
  action:
    service: switch.turn_on
    entity_id: switch.control

Oh and you had a space between automation and : on the first line.

1 Like

Thanks, it works. Now, what if I want to replace Light switch ,with controlling a servo motor, exactly the SG90