Switch off lights after a certain time

Hi Everyone,

I’m trying to work out the best way of implementing a parameter that will allow me to specify how long after a motion sensor turns lights on before they should turn off. I’m not sure I’ve coded this up in the most efficient way, so please let me know if this needs a redesign.

The trigger is the motion sensor detecting some motion, with some conditions around time, illuminance and lights current state, before choosing some Philips Hue light scenes based on some other conditons. What I’d like to is have the lights turn off after a period of time.

I looked at using a timer, but this seemed like I might need several automations to implement which didn’t seem efficient.

Do you clever people have any suggestions.

Here’s the automation as it stands:

alias: 'Bedroom: Motion Sensor'
description: Programming for Bedroom Lights
trigger:
  - type: motion
    platform: device
    device_id: f0ec9128644cdcd5b4ad1fcc07ccba98
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    domain: binary_sensor
condition:
  - type: is_illuminance
    condition: device
    device_id: f0ec9128644cdcd5b4ad1fcc07ccba98
    entity_id: sensor.hue_motion_sensor_1_light_level
    domain: sensor
    below: 150
  - condition: state
    entity_id: light.master_bedroom
    state: 'off'
action:
  - choose:
      - conditions:
          - condition: time
            after: '00:00:00'
            before: '09:30:00'
        sequence:
          - service: hue.hue_activate_scene
            data:
              group_name: Master Bedroom Lights
              scene_name: Night
      - conditions:
          - condition: or
            conditions:
              - type: is_plugged_in
                condition: device
                device_id: ea068aadbe01500374dc3cadb309e713
                entity_id: binary_sensor.oneplus_a6013_is_charging
                domain: binary_sensor
              - type: is_plugged_in
                condition: device
                device_id: ba96ffd4a109f09daf521ecf48135566
                entity_id: binary_sensor.frances_phone_is_charging
                domain: binary_sensor
        sequence:
          - service: hue.hue_activate_scene
            data:
              group_name: Master Bedroom Lights
              scene_name: Night
    default:
      - service: hue.hue_activate_scene
        data:
          group_name: Master Bedroom Lights
          scene_name: Bright
mode: single

A simple way is to use a State Trigger with the for option. The following example turns off the light if it’s been on for ten minutes.

alias: example 687
trigger:
- platform: state
  entity_id: light.master_bedroom
  to: 'on'
  for: '00:10:00'
action:
- service: light.turn_off
  target:
   entity_id: light.master_bedroom

It’s simplistic and will require enhancement to meet all of your “turn off” requirements which you will need to tell us in order to help you.

1 Like

Is it possible to set the “for” duration using a helper number, instead of a hardcoded 10 minutes?

For example to have for: {{ states(‘input_number.light_downstairs_duration’) }}

Thank you,

Yes.

  for:
    minutes: "{{ states('input_number.light_downstairs_duration') }}"

Documentation reference.

1 Like

Thank you very much!
It did work!
It did work for 2nd attempt, when I used it with a state trigger

alias: ManualBoost Heater Downstairs Switch off
description: To switch off the relay after x minutes have passed
trigger:
  - platform: state
    entity_id:
      - switch.esphome_heater_relay2
    to: "on"
    for:
      hours: 0
      minutes: "{{ (states('input_number.manualboost_downstairs_duration')) }}"
      seconds: 0
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.esphome_heater_relay2
mode: single

However, exactly the same construct failed when I used a GUI editing with “device” trigger.
(I do not really like the “device” approach, because it is impossible to understand in yaml what is this device 66afb3be1f52c0ae3bbfed016495d5c5), but I wanted to try the “lazy” way of automation creation in the wizard.)

alias: ManualBoost Heater Downstairs Switch off
description: To switch off the relay after x minutes have passed
trigger:
  - platform: device
    type: turned_on
    device_id: 66afb3be1f52c0ae3bbfed016495d5c5
    entity_id: switch.esphome_heater_relay2
    domain: switch
    for:
      hours: 0
      minutes: "{{ states('input_number.manualboost_downstairs_duration')|float }}"
      seconds: 0
condition: []
action:
  - type: turn_off
    device_id: 66afb3be1f52c0ae3bbfed016495d5c5
    entity_id: switch.esphome_heater_relay2
    domain: switch
mode: single

Message malformed: expected float for dictionary value @ data['for']['minutes']

And I did tried both with or without conversion |float or |int.

Was I doing something wrong or is it a bug in HA?

The example I posted was for use with a State Trigger. Device Triggers don’t support templates (nor do Device Conditions and Device Actions).

1 Like