SOLVED - Some help needed with first script

Hello and looking for some guidance on my first attempt at scripting a few actions on my system. It was working as anticipated for a few days but has stopped unexpectedly. What I am attempting to do is have a button on my dashboard that when selected it will perform the following actions:

  1. shut off any light that is in the “on” state on my first floor
  2. lower the TV volume to a level of “4”
  3. Turn “off” TV
  4. Turn on another light so I can “find” my way upstairs
  5. turn off the same light from step 4 after 30secs has passed

I have detailed my process below for reference and let me also say I am relatively new so sure there is a more efficient way of accomplishing this.

  1. I created a scene with all the entities that I wanted to have controlled with their desired states (off, volume 4, and TV off)

  2. created a script to turn on the light from step 4 above

alias: Bedtime script
sequence:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.bedtime
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: light.kitchen_island_lights
description: >-
  full script that will run bedtime scene plus turn on island lights for
  30seconds
  1. Created an automation to turn the light off after 30 seconds:
alias: bedtime light delay automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - script.bedtime_script
    for:
      hours: 0
      minutes: 0
      seconds: 30
conditions: []
actions:
  - type: turn_off
    device_id: 99d8b91e262d6790de6956b0ad4c2b74
    entity_id: 439f636417deb67e6df78a9f1ae39ffa
    domain: light
mode: single
  1. created a button on the dashboard that will trigger the entire process:
  - type: custom:mushroom-template-card
    primary: BEDTIME
    secondary: ""
    entity: script.bedtime_script
    tap_action:
      action: perform-action
      perform_action: script.turn_on
      target: {}
    fill_container: false
    icon: mdi:bed-clock
    icon_color: red

any guidance and/or suggestions would be greatly appreciated!!

Hi there,

There is a few things that I don’t really understand here.
First : why using a script to trigger an automation when the script can do the job ?

You should put all the steps directly in the script sequence and link your button to the action

action: script.turn_on
target:
  entity_id: script.bedtime_script

Your script should do all the step mentioned :

  1. shut off any light that is in the “on” state on my first floor
  2. lower the TV volume to a level of “4”
  3. Turn “off” TV
  4. Turn on another light so I can “find” my way upstairs
  5. turn off the same light from step 4 after 30secs has passed

Since you seems not very comfortable with the yaml config, you should probably use the GUI editor to create your sequence (and maybe check out the result in YAML after you completed what you want with the GUI if you’re interested in it)

As far as I know, this won’t work, for example :

actions:
  - type: turn_off
    device_id: 99d8b91e262d6790de6956b0ad4c2b74
    entity_id: 439f636417deb67e6df78a9f1ae39ffa
    domain: light

But I didn’t tested it my self.
What you should do is to call several actions :

  • light.turn_off / switch.turn_off targeting the entity you want to turn off
  • another action would be light.turn_on → turning on the stairs
  • then wait action for 30 seconds
  • then light.turn_off → turning off the stairs.

I don’t have enough informations on your devices to write down the whole script for you so the best I can do is giving some directions.
And I guess your goal here is to understand how to achieve this yourself instead of c/c something on the internet. :slight_smile:

I hope this help, and I’d be glad to help you more on this if needed
Cheers.

I agree with @nok… from what you have posted there doesn’t seem to be a reason to split off the part in the automation.

However, if you decide to use it anyway, you should edit the trigger. Your post describes it as:

… but that is not exactly what your trigger is doing. As constructed, the trigger will fire when the state object is stable for 30 seconds. While this will most often be when the script’s state changes from “on” to “off” and stays that way, you may get other, seemingly random, triggers from other changes. I would make the trigger specific to the on-off transition event.

triggers:
  - trigger: state
    entity_id:
      - script.bedtime_script
    to: "off"
    from: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 30

thank you for the guidance @Didgeridrew and @nok! I will attempt to put into a single script and see how I do. You are correct still learning so appreciate the “bread crumbs” !! better to teach to fish…

I will let you know how I make out.

Thank you again and success!!! Here is the final script and yes much easier to have just one file. Really appreciate all your help @Didgeridrew and @nok !

alias: Bedtime first floor
sequence:
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.bedtime
  - action: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id:
        - light.kitchen_island_lights
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - action: light.turn_off
    target:
      entity_id:
        - light.kitchen_island_lights
    data: {}
description: ""
icon: mdi:bed
2 Likes