Best way to turn on light after pir triggered, timer script?

I see many use mqtt topic and then launch a timer script that reset mqtt topic.

I find it confusing (doing automation code and script code).

Is it possible to have it only as automation?

Code example?

Thanks

Instead of automation plus script, I would prefer to have 1 automation, or 2. To have the code together

Two automations is probably the best approach. One to turn the light on, then the other to turn it off once there’s been no motion for your chosen period of time. Here’s what I’ve got to turn off a motion triggered light:

alias: 'Table light on'
trigger:
  platform: state
  entity_id: binary_sensor.lounge_motion
  state: 'off'
  for:
    minutes: 2
action:
  service: homeassistant.turn_off
  entity_id: switch.lounge_light_1_0

You could do it in a single automation or script, and simply turn it off a fixed amount of time after the motion sensor goes on. The problem there is that the light will go off regardless of whether there’s still motion.

This is mine and is not working (second automation is not publishing

..    .
    Automation motion hallway
      - alias: "Hallway upstairs turn ON light at night"
        initial_state: 'on'
        trigger:
          - platform: state
            entity_id: binary_sensor.hallway_motion
            from: 'off'
            to: 'on'
        condition:
          - condition: numeric_state
            entity_id: sun.sun
            value_template: '{{ state.attributes.elevation }}'
            below: -7
        action:
          - service: light.turn_on
            entity_id: light.hallway_right
          - delay: '00:01:00'
          - service: light.turn_off
            entity_id: light.hallway_right
          - service: mqtt.publish
            data:
              topic: sensor-pir_hallway
              payload: "off"
              retain: "true"
    #
      - alias: "Hallway upstairs during day"
        initial_state: 'on'
        trigger:
          platform: state
          entity_id: binary_sensor.hallway_motion
          state: 'on'
          for:
            seconds: 30
        condition:
          - condition: numeric_state
            entity_id: sun.sun
            value_template: '{{ state.attributes.elevation }}'
            above: 0
        action:
          service: mqtt.publish
          data:
            topic: 'sensor-pir_hallway'
            payload: 'off'
            retain: 'true'
    #

What if you manually trigger the automation, does it work then?

I do this to check.
Trigger on the topic (with a program)
Then second automation should put the topic to off, correct ?
But it doesn’t

Second automation, yes

If you go to the HA web interface, select the second automation, and then push the Trigger option. Does it work?

The automation itself should only trigger when:

  • There has been motion for the last 30 seconds
  • The sun is above the horizon

I don’t understand your code. The trigger state should not be on instead of off?

Why, I don’t want it turning the light off if there’s movement, I want it turning the light off if there’s no movement. If you simply want the light turned off when there’s movement and it’s daytime, you could just drop the duration check with the for.

I am surely missing something.

My goal is simple.

At night turn on the light if there is motion for x minutes, then light off. If continue motion keep light on
At day do nothing

If during the day you don’t want the light turned on automatically, well, then don’t write an automation to turn it on - which you haven’t done.

For the night time, you do want to automations, as I’ve done. Your current single automation will turn the light off regardless of whether there’s still movement. Setting up one automation to turn it on, then another to turn it off after a period of no motion is far friendlier:

# Automation motion hallway
  - alias: "Hallway upstairs turn ON light at night"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.hallway_motion
        from: 'off'
        to: 'on'
    condition:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: -7
    action:
      - service: light.turn_on
        entity_id: light.hallway_right
      - service: mqtt.publish
        data:
          topic: sensor-pir_hallway
          payload: "on"
          retain: "true"

# Automation motion hallway
  - alias: "Hallway upstairs turn OFF light at night"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.hallway_motion
        state: 'off'
        for: '00:01:00'
    condition:
      - condition: state
        state: 'on'
        entity_id: light.hallway_right
    action:
      - service: light.turn_off
        entity_id: light.hallway_right
      - service: mqtt.publish
        data:
          topic: sensor-pir_hallway
          payload: "off"
          retain: "true"
2 Likes

Thanks will try this

I think it works fine. But why many do automation+script, instead of two automation way simpler IMO …???

Because it works for them? I use scripts in a number of places, and multiple actions in automations in others.

They can be used to achieve the same thing, but they can also be used to achieve slightly different outcomes. You can turn scripts on and off, so you can use them as resettable timers. You can use them to allow you to write a process once, and use many times. Heck, folks may use scripts simply so that their automations themselves are simpler.

Also, you’ve got to remember, every 2 weeks the HA platform changes. Not that long ago scripts may have been the only option.

Ultimately, where there’s more than one way of doing something, there will always be people who prefer one of the options, because that’s the way people are.

I have other automation with problem.

THis time I do have a light sensor involved.

Goal is
At night
if there is movement turn ON light, when no movement after x minutes turn OFF light

At day
If there is movement AND light sensor below Y turn ON light, when no movement after x minutes turn OFF light.

The problem is that at night, the light sensor (even if light is ON) is always at low level .

I hope I am clear.

  - alias: "Light Kitchen ON with motion"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000127acc3
        from: 'off'
        to: 'on'
    condition:
      condition: or
      conditions:
        - condition: numeric_state
          entity_id: sun.sun
          value_template: '{{ state.attributes.elevation }}'
          below: -5
        - condition: numeric_state
          entity_id: sensor.flower_1_light_intensity
          below: 55
    action:
      - service: switch.turn_on
        entity_id: switch.kitchen
      - service: mqtt.publish
        data:
          topic: sensor-pir_kitchen
          payload: "on"
          retain: "true"
#
  - alias: "Light Kitchen OFF without motionn"
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000127acc3
        state: 'off'
        for: '00:03:00'
    action:
      - service: switch.turn_off
        entity_id: switch.kitchen
      - service: mqtt.publish
        data:
          topic: sensor-pir_kitchen
          payload: "off"
          retain: "true"
1 Like

What is the problem? You just say that there is a problem relating to the light level. Is it that the light turns on when it shouldn’t, that it turns off when it shouldn’t…?

I assume you’ve chosen the light level of 55 as the point at which you want the lights on? However, if the sensor doesn’t read that high even with the lights on, it suggests your threshold is too high.

the threashhold is ok during the day, but not at night. At night with artificial lights on (which have enough luminosity) the lux level is 30.

With lux level below 55 during day, is too dark. DOn’t know maybe sensitivity with artificial light or sun light is different

In that case, you need 2 sets of conditions. One for daytime, one for nighttime.

condition: or
conditions:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        below: -5
      - condition: numeric_state
        entity_id: sensor.flower_1_light_intensity
        below: 29
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sun.sun
        value_template: '{{ state.attributes.elevation }}'
        above: '-5'
      - condition: numeric_state
        entity_id: sensor.flower_1_light_intensity
        below: 55

(I haven’t checked the above is formatted correctly, I simply typed it directly here, so you’ll want to double check the formatting)

2 Likes