Add delay to scene

Hi

How I can add delay to scene? sequence ?

I want my lights/switches will turn off after 2 minutes scene activated.
How i’m adding delay??

scene:
  - name: "Multimedia Off"
    entities:
      switch.amplifier:
        state: 'off'
      switch.tv_lights:
        state: 'off'      
      switch.tv:
        state: 'off'

You can’t. Use a script.

Or automation

and how can I assign it to be triggered from the scene?

Can’t you delay within the scene using ‘transition’?

Why would you need to trigger it from a scene? Scripts are triggerable from the UI. And you can call scripts from automations

action:
  - service: script.entity_id

I want the scene to appear in my homekit (thru homebridge). Otherwise I don’t see scripts in home app

So, what happened when you gave it a try? Were you able to trigger a script from a scene?

Homebridge can see booleans, too, so you could always kick off the script from an automation that watches a boolean. You could even have that script reset the boolean when it completes.

Did you figure out how to add a delay to a scene?

I want my scene to turn off all the lights - and then turn a couple of them back on - but without a delay, when I activate the scene, some of the lights receive the turn on before they receive the turn off and end up in the wrong state. Adding a 2 second delay after ‘Turn off all lights’ would solve my problem.

The only alternative I can think of is to turn off each light individually - which would require constant maintenance as I add new lights to the home.

Is this a situation where you can use a script? Scripts can include delays. Pair that with groups (you can sent an on/off to a whole group) and it should be pretty straightforward.

Back those days I set a script with delays and added a scene that was running this script.
Nowadays homekit supports scripts as well…

Thanks for the replies.
As suggested - I’ve used scripts where I need to insert delays.

@tenly got an example?

@Alec

bedroomlightsdelayed:
  alias: Bedroom Lights Delayed
  sequence:
  - service: light.turn_off
    data:
      entity_id: light.bedroom
      transition: 20
  - delay:
      seconds: 40
  - service: switch.turn_off
    data:
      entity_id: switch.bedroom_lamp
  - delay:
      seconds: 90
  - service: light.turn_off
    data:
      entity_id: light.bed_lights
1 Like

I created two scripts and added them to HomeKit config. But they show as two switches. Can I somehow show them like scenes? Or combine them in one switch?

Homekit config looks like this:

homekit:
  filter:
    include_domains:
      - light
      - climate
      - fan
    include_entities:
      - script.mediasys_turnon
      - script.mediasys_turnoff

Scripts look like this:

  mediasys_turnon:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.media_outlet
      - delay:
          seconds: 4
      - service: switch.turn_on
        data:
          entity_id: switch.epson_projector_switch
  mediasys_turnoff:
    sequence:
      - service: switch.turn_off
        data:
          entity_id: switch.epson_projector_switch    
      - wait_template: "{{ is_state('binary_sensor.epson_projector', 'off') }}"
        timeout: '00:01:00'
        continue_on_timeout: 'false'
      - service: switch.turn_off
        data:
          entity_id: switch.media_outlet
1 Like

I have the same for multimedia on/off with different scripts.
So I finished with creating a dummy binary switch and mapping the scripts to it.

I ended up making a switch with the delay this way. When I turn it off I make sure that the projector is off so I don’t damage it.

Scripts:

  mediasys_turnon:
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.shenzhen_neo_electronics_co_ltd_power_plug_12a_switch
      - delay:
        # Supports milliseconds, seconds, minutes, hours, days
          seconds: 4
      - service: broadlink.send
        data:
          host: 192.168.1.30
          packet: 'JQFRAUERQRFDUVEBUQFTQVNRU1FTQVNRUQFTQVNRUQFQANBQAAAAAAAAAA'
  mediasys_turnoff:
    sequence:
      - service: media_player.turn_off
        data:
          entity_id: media_player.epson_projector_lan
      - wait_template: "{{ is_state('binary_sensor.projector_working', 'off') }}"
        timeout: '00:01:00'
        continue_on_timeout: 'false'
      - service: switch.turn_off
        data:
          entity_id: switch.shenzhen_neo_electronics_co_ltd_power_plug_12a_switch

Config:

binary_sensor:
  - platform: ping
    host: 192.168.1.161
    count: 2
    name: epson_projector
    scan_interval: 10
  - platform: template
    sensors:
      projector_working:
        friendly_name: "Проектор"
        device_class: plug
        icon_template: mdi:projector
        value_template: "{{ is_state('binary_sensor.epson_projector', 'on') }}"

switch:
  - platform: template
    switches:
      # включение/выключение проектора через сеть и ИК для HomeKit
      epson_projector_switch:
        friendly_name: "Проектор"
        value_template: "{{ is_state('binary_sensor.projector_working', 'on') }}"
        turn_on:
          service: script.turn_on
          entity_id: script.mediasys_turnon
        turn_off:
          service: script.turn_on
          entity_id: script.mediasys_turnoff

You (today) can use sequences in an automation, like below:

      - conditions:
          - condition: template
            value_template: "{{ states('binary_sensor.tradfri_motion_sensor') == 'off' }}"
            
          - condition: template
            value_template: "{{ state_attr('sun.sun', 'elevation') < -4 }}"

        sequence:
          - service: scene.turn_on
            data_template:
              transition: 5
              entity_id: scene.soft_gang

          - delay:
              seconds: 10

          - service: scene.turn_on
            data_template:
              transition: 20
              entity_id: scene.uit_gang

and in the complete automation.yaml

- id: '1683859223825749'
  alias: PIRS
  description: 'all PIR automations'

  trigger:
  - event: start
    platform: homeassistant

  - event: sunset
    platform: sun
    # Optional time offset. This example will trigger 45 minutes before sunset.
    offset: "01:00:00"

  - event: sunrise
    platform: sun
    # Optional time offset. This example will trigger 45 minutes before sunset.
    offset: "01:00:00"

  - platform: state
    entity_id: binary_sensor.tradfri_motion_sensor
    to: 'on'

  - platform: state
    entity_id: binary_sensor.tradfri_motion_sensor
    to: 'off'
    for: '00:00:30'

  action:
  - data_template:
      message: "GANG: {{now()}} {{ states('binary_sensor.tradfri_motion_sensor') }} {{state_attr('sun.sun', 'elevation')}}"
    service: persistent_notification.create

  - choose:
      - conditions:
          - condition: template
            value_template: "{{ states('binary_sensor.tradfri_motion_sensor') == 'on' }}"
          - condition: template
            value_template: "{{ state_attr('sun.sun', 'elevation') < 4 }}"
          - condition: template
            value_template: "{{ state_attr('sun.sun', 'elevation') > -4 }}"
        sequence:
          service: scene.turn_on
          data_template:
            transition: 1
            entity_id: scene.fel_gang

      - conditions:
          - condition: template
            value_template: "{{ states('binary_sensor.tradfri_motion_sensor') == 'on' }}"
          - condition: template
            value_template: "{{ state_attr('sun.sun', 'elevation') < -4 }}"
        sequence:
          service: scene.turn_on
          data_template:
            transition: 2.5
            entity_id: scene.fel_nacht_gang

      - conditions:
          - condition: template
            value_template: "{{ states('binary_sensor.tradfri_motion_sensor') == 'off' }}"
            
          - condition: template
            value_template: "{{ state_attr('sun.sun', 'elevation') < -4 }}"

        sequence:
          - service: scene.turn_on
            data_template:
              transition: 5
              entity_id: scene.soft_gang

          - delay:
              seconds: 10

          - service: scene.turn_on
            data_template:
              transition: 20
              entity_id: scene.uit_gang

from app, watches and car the HA app has some direct access to the scenes, not to scripts… so it would be quite nice to have a way to include this script into a scene, even if it is just for this…

Hon, this is more than necrophilia after four (4) years… :wink:

HA has changed so much, I’m not even sure, if any of the above code is even valid. If you have a question or a problem, please open up a new topic or find a topic, that was written in in the last six months. Anything else is likely outdated!