Using scripts to turn on or off lights

Hi everyone.
In my house we have apple speakers with build in siri to do our daly stuff, turn on and off lights, play music or radio.
So i created scripts that Apple bridge can connect to our apple home.
so when i say for example “Hey siri, relax” siri will activate script on HA to turn on lights and dim them for the relaxing evening, and if i say “Hey siri” it will run script on HA to turn off all lights, and turn on bedroom stand lights, and turn of the TV etc.
However, sometimes there is one or two lights not turning off, or turning on and it is a little bit frustrating to monitor if it has worked or not.
So here is my question.
In those scripts, is there an easy way to put in the script to verify that the lights in the scrip are on or off and if not then try run the script again or turn off the lights or similar.
In short to validate that all devices have responded to the script ?
Have a fantastic day.

Someone else asked a similar question and the technique is to use a repeat.

If you post the script you’re currently using to control lights, I can show you how to adapt it.


NOTE

The linked example is several years old. Nowadays, instead of using a fixed time delay in the example, it would be preferable to use a wait_template with a timeout.

      - repeat:
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.your_switch
            - wait_template: "{{ is_state('switch.your_switch', 'on') }}"
              timeout:
                seconds: 10
          until: "{{ is_state('switch.your_switch', 'on') or repeat.index >= 5 }}"

Here is my relax script
alias: Relaxing evening
sequence:

  • service: light.turn_on
    target:
    device_id:
    - cb0ef0f1de89a28c3ca2845557cf3569
    data:
    transition: 5
    brightness_pct: 50
  • service: light.turn_on
    metadata: {}
    data:
    brightness: 10
    transition: 5
    target:
    device_id:
    - a8f96eb7709125afee794bee6494083f
    - e045f20274d72cee40dc48c6aae29633
  • service: light.turn_on
    metadata: {}
    data:
    transition: 10
    brightness_pct: 10
    target:
    device_id: ed9830db9360f068cc2d7a8aa99c914c
  • type: turn_on
    device_id: 582a8c34a50476641bbc2f652943e176
    entity_id: 6b2efb5cfd7bd50e14507d7f07d76aa9
    domain: switch
  • service: light.turn_on
    metadata: {}
    data:
    transition: 5
    brightness_pct: 28
    target:
    device_id: 6e3e45337e0340f2d2cbc7ba1549a695
  • type: turn_on
    device_id: 7f595b52e555a9dc107ac7f910537247
    entity_id: 1d8fcd5472df536b19d90739221158c8
    domain: switch
  • type: turn_on
    device_id: 2c028507cdefe64b9a1d028caec913df
    entity_id: afbbc3e254ef1d53364401d73cbd00d5
    domain: switch
    mode: single
    icon: mdi:alarm-light-outline

And here is my good night script
alias: Time to Sleep
sequence:

  • type: turn_off
    device_id: 7f595b52e555a9dc107ac7f910537247
    entity_id: 1d8fcd5472df536b19d90739221158c8
    domain: switch
  • type: turn_off
    device_id: 2c028507cdefe64b9a1d028caec913df
    entity_id: afbbc3e254ef1d53364401d73cbd00d5
    domain: switch
  • type: turn_off
    device_id: 582a8c34a50476641bbc2f652943e176
    entity_id: 6b2efb5cfd7bd50e14507d7f07d76aa9
    domain: switch
  • service: light.turn_off
    metadata: {}
    data:
    transition: 5
    target:
    area_id: livingroom
  • delay:
    hours: 0
    minutes: 0
    seconds: 5
    milliseconds: 0
  • type: turn_off
    device_id: a259fbf44aaff9c189046a88d23bda9b
    entity_id: 8bbd18b3e36b4986cf1c8e9405b5d92a
    domain: remote
  • service: light.turn_off
    metadata: {}
    data:
    transition: 5
    target:
    area_id: kitchen
  • delay:
    hours: 0
    minutes: 0
    seconds: 5
    milliseconds: 0
  • service: light.turn_off
    metadata: {}
    data:
    transition: 5
    target:
    device_id:
    - 8d754b96148008d0c89df54024b50aa0
    - 2361a1f7b406f5a66692b19ca6a247bf
    - 448d3b6c84b953925381e213ac3d5a5b
    - c1c063889bb096147ef49a2ad00386fe
    - cb0ef0f1de89a28c3ca2845557cf3569
  • delay:
    hours: 0
    minutes: 0
    seconds: 2
    milliseconds: 0
  • service: light.turn_on
    metadata: {}
    data:
    transition: 2
    brightness_pct: 10
    target:
    device_id:
    - 3e81805183821c86df21f2446a3604d8
    - 94f5b23370ee7af945f3ba607d143fa6
    mode: single
    icon: mdi:bed-clock

Which “one or two lights” sometimes fail to turn on or off?

it is quite random actually, sometimes one light in the livingroom, and sometimes in the kitchen. that´s why i want to check all the switches to se if they are in that state i selected, and if not then re-run the script again

It sounds like there’s a problem with the reliability of your lighting system’s communications. Whether it’s based on WiFi, Zigbee, or Zwave (or some combination), I recommend you focus your efforts on fixing the root cause of the unreliability. Otherwise, you will forever be writing automations that have to double-check if a light has actually complied with a given command.

1 Like

yeah, i have HA installed on Raspberry pi 4, and usb dongle for Zigbee.
I do reboot the pi once a week so the system i stable, and i have alot of devices :smiley: So the root cause i think is mainly the Raspberry pi though i dont´t know it for sure, and that´s why i wanted to implement the recheck of the scripts :slight_smile:

Formatting code suggestions.

yeah, i never do yaml, i use the visual editor most cases. it is just so much easier for a lazy guy like me :smiley:

The remark about formatting has nothing to do with how you created the automation in HA, but everything with how you post it here. If you do not place it in three backticks as described in the post that the link refers to, then we cannot properly read the automation or see any flaws. As the title of that topic says: help us help you.

It most likely is not. Zigbee problems are more likely:

The morning light script there could be easily stored as a scene. Set the lights in the morning configuration and save the state of all those lights with a scene, then just turn the scene on in the morning. The evening routine with the changes in transition and delays would need be a script of some type. Otherwise yes, random dropouts with Zigbee is usually a weak mesh as Edwin stated.

If you want any other advice on those scripts, I have none. Very hard to read.

First of all i want to thank you all for reaching out.
I manage to do what i wanted and then sum after reading here in the community, watching youtube and spend several hours of coding and testing.

I followed Taras Idea with wait_template and it worked like a charm.

Then i got more ideas of using scenes and helpers on my remotes to put different lights brightness depending on the mood and much more. :slight_smile:

Thanks again everyone. :slight_smile:

1 Like