Seems simple? PIR sensor to turn on a room light

I would like to use a PIR sensor to turn a light on for five minutes and stay on as long as any motion is detected by the PIR, then turn off when there is no motion detected within five minutes.

I have a PIR sensor wired to gPIO14 on an ESP8266-01 running Tasmota. It gives me this in the console when I walk in front of the PIR:

14:57:13 MQT: stat/pir_room/RESULT = {“POWER”:“ON”}
14:57:13 MQT: stat/pir_room/POWER = ON
14:57:16 MQT: stat/pir_room/RESULT = {“POWER”:“OFF”}
14:57:16 MQT: stat/pir_room/POWER = OFF

I have an automation to call a service (switch.turn.on) to turn on the room light. This works fine, but this is where I am drawing a blank. How do I get the light to stay on unless there is no more motion detected?

I suspect that I need to use a timer component, but not sure how to integrate the timer into an automation. Or do I even need an automation?

Any ideas would be appreciated.

here is an example for you. use 2 automations, 1 to turn ON and 1 to turn OFF.

  - alias: Ensuite light auto ON motion
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001ad43db
        to: 'on'
    condition:
      condition: or
      conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-01:00:00'
    action:
      service: light.turn_on
      entity_id: light.ensuite_light


  - alias: Ensuite light OFF after 2mins
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001ad43db
        to: 'off'
        for:
          minutes: 1
    action:
      - service: homeassistant.turn_off
        entity_id: light.ensuite_light

Thanks- it didn’t occur to me to use two automations. My PIR is an MQTT switch, not a sensor, so, would this work?

- alias: Turn on room light when there is movement
  trigger:
    platform: mqtt
    state_topic: "stat/pir_room/POWER"
    to: "ON"
  action:
    service: switch.turn_on
    entity_id: switch.room_light

	
- alias: Turn off room light 1 minute after last movement
  trigger:
    platform: mqtt
    state_topic: "stat/pir_room/POWER"
    to: "OFF"
    for:
      minutes: 1
  action:
    service: switch.turn_off
    entity_id: switch.room_light

Do I understand correctly that the second action only happens if the state_topic is “OFF” for one minute, so more movement would reset the OFF timer?

I think so, I haven’t had a lot of experience with MQTT topics etc.

Correct, as long as there is still movement occurring the timer keeps getting reset. The light will only turn OFF once movement has stopped for 1 minute

No it is a sensor, not a switch.

Thanks for the reminder to be more careful with my terminology. I should know better by now.

Thanks, sparkydave.I am making more progress with your help. I am trying to use the Automations Editor, but I can’t find how to add the for: parameter?

You are far better to just type in the config file.

1 Like

So, nickrout, would you be steering me away from the Automation Editor?

As @nickrout has said, just type the YAML manually. I have never used the Automations Editor because when I first looked at it, it seemed too clunky and plenty of people have said it lacks certain functionality. Typing things out yourself will be far more beneficial in learning to code too

Yes it is buggy and unfinished (IMHO).

Here’s mine; it’s based off a premise that I read in the forum where a guy decided the best way to was to base his automation on LACK of motion. This way the light stays on until the PIR detects no motion and then turns it off. I can’t remember the name of the brilliant user with the idea but I’ve used this for over a year in multiple rooms with 100% WAF approval:

- alias: Motion Home Hallway
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_pir_motion
      from: 'off'
      to: 'on'
  condition:
    - condition: state
      entity_id: light.hallway
      state: 'off'
    - condition: state
      entity_id: group.people
      state: 'on'
    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-1:00:00"
        - condition: sun
          before: sunrise
          before_offset: "1:00:00"
  action:
    - service: scene.turn_on
      data_template:
        entity_id: >
          {% if now().hour >= 20 or now().hour <= 7 %}
            scene.hallway_nightlight
          {% else %}
            {% if now().weekday() >= 0 or now().weekday() <= 4 %}
              scene.hallway_wkday_bright
            {% else %}
              scene.hallway_wkend_bright
            {% endif %}
          {% endif %}

And to turn it off:

- alias: Lights Hallway Off
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_pir_motion
      to: 'off'
      for:
        minutes: 1
  action:
    - service: homeassistant.turn_off
      entity_id: light.hallway

And the scenes:

  - name: hallway_wkend_bright
    entities:
      light.front_hallway:
        state: on
        rgb_color: [255,207,120]
        brightness: 254

  - name: hallway_wkday_bright
    entities:
      light.front_hallway:
        state: on
        rgb_color: [255,207,120]
        brightness: 254
      light.back_hallway:
        state: on
        rgb_color: [255,207,120]
        brightness: 254

  - name: hallway_nightlight
    entities:
      light.front_hallway:
        state: on
        rgb_color: [21,8,0]
        brightness: 1

Duly noted. So, first, how do I delete the automations created by the Automatons Editor? Could I just delete them from 'automations.yaml.?

On adding the for parameter, I get a config error now:

Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation-&gt;trigger-&gt;0-&gt;for. (See /config/configuration.yaml, line 282). Please check the docs at https://home-assistant.io/components/automation/

Here’s the automation:

- id: '1547413472772'
  alias: Room light on upon entry
  trigger:
  - payload: 'ON'
    platform: mqtt
    topic: stat/pir_room/POWER
  condition: []
  action:
  - data:
      entity_id: switch.room_light
    service: switch.turn_on
- id: '1547413570161'
  alias: Room Light Off upon No Motion
  trigger:
  - payload: 'OFF'
    platform: mqtt
    topic: stat/pir_room/POWER
    for:
      minutes: 1
  condition: []
  action:
  - data:
      entity_id: switch.room_light
    service: switch.turn_off

thanks chrisw, you answered my next question before asking it- how to make it only work after sunset

1 Like

You don’t need to delete it, just edit it. If you do want to delete it, just edit the file and delete the lines.

I am going backwards.

I removed the editor-generated automations from the automations.yaml file and put my automations into the automations_old.yaml.

Here’s what I put in:

#Aliases to handle the PIR sensor to control the Room light.
- alias: Turn on room light when there is movement
  trigger:
    platform: mqtt
    state_topic: "stat/pir_room/POWER"
    to: "ON"
  action:
    service: switch.turn_on
    entity_id: switch.room_light


- alias: Turn off room light 1 minute after last movement
  trigger:
    platform: mqtt
    state_topic: "stat/pir_room/POWER"
    to: "OFF"
  action:
    service: switch.turn_off
    entity_id: switch.room_light

But, check config gives me an error. Apparently automations doesn’t like MQTT as a platform.

This is my second attempt: The room light should track the PIR output, either on or off.

#Aliases to handle the PIR sensor to control the Room light.
- alias: Turn on room light when there is movement
  trigger:
    platform: state
    entity_id: switch.room_light
    to: 'ON'
  action:
    service: switch.turn_on
    entity_id: switch.room_light


- alias: Turn off room light when the PIR see's no movement
  trigger:
    platform: state
    entity_id: switch.room_light
    to: "OFF"
  action:
    service: switch.turn_off
    entity_id: switch.room_light

It passes the config check, but the automation doesn’t turn the room light on or off.

Any other ideas would be appreciated.

try indenting further and using the ‘-’ before ‘platform’ as per my example above. Might not be the answer but worth a shot.

I prefer to set up all my automations as if the triggers and actions are going to be a list so it makes adding others in later a bit easier.

your trigger should be the state of your pir ** sensor**, not some switch.

yep, I totally missed that! I’m too busy trying to fix something else right now :tired_face:

According to the docs mqtt is a valid trigger. see here:

but in the example they don’t have quotes around the topic.

without the you posting the error you were getting it’s hard to know what the problem might be.

However, to continue on with the other recommendations to just use the sensor, I assume you created some kind of sensor or binary sensor in HA to represent the PIR sensor? what is the domain.entity_id of that sensor?

UPDATE:
Thanks to your excellent tips- all of them- I got it working.

The mqtt trigger did work, but adding:

for:
  seconds: 30

…generated this config error:

Invalid config for [automation]: [for] is an invalid option for [automation]. Check: automation->trigger->0->for. (See /config/configuration.yaml, line 275). Please check the docs at https://home-assistant.io/components/automation/

It appears that an mqtt trigger doesn’t like the for: ??

So following the tip from finity, I made a sensor by adding this to my sensors.yaml:

- platform: mqtt
  name: Room PIR
  state_topic: "stat/pir_room/POWER"

…and I could see the PIR in ‘States’ where I see ‘ON’ or ‘OFF’

So, here is my final automation (that works as expected):

#Automations to handle the PIR sensor to control the Room light.
- alias: Turn on room light when there is movement
  trigger:
    platform: state
    entity_id: sensor.room_pir
    to: 'ON'
  action:
    service: switch.turn_on
    entity_id: switch.room_light

- alias: Turn off room light when there is no movement
  trigger:
    platform: state
    entity_id: sensor.room_pir
    to: 'OFF'
    for:
      seconds: 30
  action:
    service: switch.turn_off
    entity_id: switch.room_light

Again, thanks all for pointing me in the right direction.

1 Like