I would like to have an automation that toggles a light continuously while a binary sensor is True. I thought that I would use a script that blinks a light and have the automation call this script based on a trigger event. However, I tried using a Repeat While loop but cannot get it to work (mostly due to yaml errors).
Post the script and we’ll help you fix it.
Here’s an example of a repeat loop to get you started. (I use this because my phone is nearly 100% always on vibrate only, plus we always have music playing in the house)
automation:
- alias: Daves S20 Ringing announce at home
trigger:
- platform: state
entity_id: sensor.daves_s20_phone_state
to: ringing
condition:
- condition: state
entity_id: person.dave
state: home
- condition: time
after: '08:00:00'
- condition: time
before: '18:30:00'
action:
repeat:
while:
- condition: state
entity_id: sensor.daves_s20_phone_state
state: ringing
sequence:
- service: rest_command.assistant_broadcast
data:
command: Daves phone is ringing
- delay: '00:00:10'
@sparkydave Thanks! I tried using your script and modifying it to do what I am looking for but I am getting YAML errors.
alias: Flash light when gate is open
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: on
action:
repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: on
sequence:
- service: light.toggle
data:
entity_id:: light.gate_snd_lte
- delay: '00:00:01'
If you are using the Automation Editor, pasting this into YAML mode should work:
alias: Flash light when gate is open
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'on'
sequence:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
- delay: '00:00:01'
- service: light.turn_off
target:
entity_id: light.gate_snd_lte
If you are pasting it directly into automations.yaml
then it requires a subtle modification:
- alias: Flash light when gate is open
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'on'
sequence:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
- delay: '00:00:01'
- service: light.turn_off
target:
entity_id: light.gate_snd_lte
NOTE
I added a light.turn_off
service at the very end to ensure that when the input_boolean is turned off, the light is not left on
.
BTW, are you aware that the Light integration offers a flash
option?
flash
Tell light to flash, can be either valueshort
orlong
.
It only works with lights that have a native ability to flash. The easiest way to determine if your lights support it is to go to Developer Tools > Services, select Light: Turn on
, select your light, enable Flash
, set it to long
, and finally click Call Service.
EDIT
Correction. Replaced doubled colons with a single colon for entity_id:
@123 Thank you. I tried pasting that in YAML mode in the Automation Editor and got:
Message malformed: extra keys not allowed @ data['action'][0]['repeat']['sequence'][0]['target']['entity_id:']
I remember getting that error when I used an older version of HA
Try changing
target:
to
data:
Sorry, that was my mistake.
The example included this (two colons) which is wrong:
target:
entity_id:: light.gate_snd_lte
^
|
WRONG
I corrected it in the example shown in my previous post and it now looks like this:
target:
entity_id: light.gate_snd_lte
NOTE
I pasted the corrected example into the Automation Editor, clicked Save, and it was accepted with no errors reported.
OK, the automation now looks like this:
alias: Flash light when gate is open
description: ''
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
condition: []
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'on'
sequence:
- service: light.toggle
data:
'entity_id:': light.gate_snd_lte
- delay: '00:00:01'
- service: light.turn_off
data:
entity_id: light.gate_snd_lte
mode: single
saved with no errors. Problem now is that the action is not flashing the light.
This is still incorrect:
It should look like this:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
alias: Flash light when gate is open
description: ''
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
condition: []
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'on'
sequence:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
- delay: '00:00:01'
- service: light.turn_off
data:
entity_id: light.gate_snd_lte
No errors but no flashing light.
- Go to Developer Tools > Services
- Select ‘Light: Toggle’
- Select
light.gate_snd_lte
- Click ‘Call Service’
The light should toggle.
- It it does, click Call Service as fast as you can (at least once per second) and see if the light still toggles properly.
- If it fails to toggle, now you know that there’s a fundamental problem.
Which integration is being used to support this light? Is it based on Zigbee, Z-Wave, or what?
@123 No problem toggling it in Developer tools although probably shouldn’t go faster than 2 sec delay. Part of the problem may have been the delay but I also did a Run Actions with the sensor state=off (rather than on). WIth the sensor on (or the condition changed for testing), and a 3 second delay, it appears to be working. Thanks for your help.
That’s why I asked which integration you are using for the light. Some lighting technologies are unable to effectively control a device when commands are sent rapidly.
The other reason why I asked is that some lighting technologies have a native ability to flash a light (without having to receive a stream of on/off/on/off commands).
You’re welcome!
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link under your first post that leads to the solution post. All of this helps users find answers to similar questions.
@123 I am finding that sometimes the light is left in the on state even after the trigger is off. I thought that the while loop would be active toggling the light when the sensor was on and would turn off the light when the sensor was off.
alias: Flash light when gate is open
description: ''
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
condition: []
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'on'
sequence:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
- service: light.turn_off
target:
entity_id: light.gate_snd_lte
mode: single
In theory, that should not happen. The repeat
iterates only while the gate_sensor is on
and ceases when the gate_sensor changes to off
. The next action after repeat
is a service call to turn off the light.
I recommend you add an id
to the automation (and then Reload Automations). That will permit Home Assistant to create a trace each time the automation is triggered. Whenever the automation fails to turn off the light, quickly examine the last trace (there will be several) to examine what it did.
alias: Flash light when gate is open
id: flash_light_abc123
description: ''
trigger:
As an alternative, you can try this version which effectively does the same thing as the original version except it employs a repeat - until
. It iterates until the gate_sensor changes to off
and then turns off the light. I doubt this will work any better than the original version so my recommendation remains that you examine the trace to gain insight into why the final service call isn’t turning off the light.
alias: Flash light when gate is open
description: ''
trigger:
- platform: state
entity_id: binary_sensor.gate_sensor
to: 'on'
condition: []
action:
- repeat:
sequence:
- service: light.toggle
target:
entity_id: light.gate_snd_lte
- delay:
hours: 0
minutes: 0
seconds: 2
milliseconds: 0
until:
- condition: state
entity_id: binary_sensor.gate_sensor
state: 'off'
- service: light.turn_off
target:
entity_id: light.gate_snd_lte
mode: single
If this also oc
The light just turned on although the automation was not triggered, so I presume something else is turning this light on although I have no idea what. It is controlled by an X10 switch and the x-10 command is sent from an Omnipro which is integrated with HA via the Omnipro Bridge.
Ouch! Toggling an X10 device every 2 seconds? Small wonder it was unreliable when the delay
was 1 second.
FWIW, I switched from X10 to UPB many years ago to get better reliability and functionality. UPB supports blinking a light natively. In other words, I can send a single command to start blinking a light and the device takes care of the rest.
Although I replaced the majority by either UPB or WiFi devices, I still have a few stalwarts:
- Two outlets mounted outdoors (had three but that one failed 2 winters ago).
- Two light switches in a 3-way circuit (slated to be replaced soon).
- One heavy-duty plug that controls a 220VAC pool pump (over 12 summers of service so far).
I have a couple of UPB modules and the interface module. They are connected to the Omnipro but I can’t seem to get them to work when I send commands from the Omnipro. The Omnipro bridge has added them to HA but, obviously they cannot be controlled from there either. I thought perhaps I wolud take them off the Omni and put them on HA directly to facilitate troubleshooting but it has been a long time since I installed them and I don’t recall how to do it.
Hello,
I’m having some erros with the code, some missing comma errors!
[- alias: Piscar Luz
trigger:
- platform: state
entity_id: binary_sensor.sensor_fundos_movimento
to: 'on'
action:
- repeat:
while:
- condition: state
entity_id: binary_sensor.sensor_fundos_movimento
state: 'on'
sequence:
- service: light.toggle
target:
entity_id: light.l_escritorio_switch_1
- delay: '00:00:01'
- service: light.turn_off
target:
entity_id: light.l_escritorio_switch_1]
Error:
missed comma between flow collection entries (1:2)
1 | [- alias: Piscar Luz
------^
Yeah, I’m new on this and need some help, maybe you have an easy solution for me.