I’m looking for feedback on how I can improve my script, or perhaps best practices I’ve not followed or tricks I’ve missed.
Basically how can I improve it.
What it does (or tries to)
- Store a scene of lights that are on
- Turn a set of lights on to a certain colour based on
red
,green
,blue
- Wait a time period based on the
dutycycle
(e.g. 10% of the time) - Turn the same set of lights off
- Wait a time period based on the
dutycycle
(e.g. 90% of the time) - Goto
2
forCount
times - Finally restore the scene stored in
1
A couple of issues/limitations
- Haven’t done anything about white only lights yet
- Can’t work out how to store state for only the
target
selector lights - There is no colour selector
- Running script from the edit script doesn’t obey any defaults - the developer tools run functional would be great here
alias: Flash light
sequence:
- variables:
_red: '{{ red | default(128) }}'
_green: '{{ green | default(128) }}'
_blue: '{{ blue | default(128) }}'
_onFor: |-
{% if (duration | default(0)) > 0 and (dutycycle | default(0)) > 0 %}
{{ (duration * (dutycycle / 100)) | int }}
{% else %}
500
{% endif %}
_offFor: |-
{% if (duration | default(0)) > 0 and (dutycycle | default(0)) > 0 %}
{{ (duration * ((100 - dutycycle) / 100)) | int }}
{% else %}
500
{% endif %}
- service: scene.create
data:
scene_id: temp_flash_light
snapshot_entities: >-
{{ states.light | selectattr('state', 'eq', 'on') |
map(attribute='entity_id') | join(',') }}
- repeat:
count: '{{ count | default(5) }}'
sequence:
- service: light.turn_on
target: '{{ lights }}'
data:
rgb_color:
- '{{ _red }}'
- '{{ _green }}'
- '{{ _blue }}'
brightness: '{{ max(_red, _green, _blue) }}'
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: '{{ _onFor }}'
- service: light.turn_off
target: '{{ lights }}'
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: '{{ _offFor }}'
- service: scene.turn_on
target:
entity_id: scene.temp_flash_light
data: {}
fields:
lights:
description: Lights
required: true
selector:
target:
entity:
domain: light
red:
description: Red
default: 128
example: 255
selector:
number:
min: 0
max: 255
green:
description: Green
default: 128
example: 0
selector:
number:
min: 0
max: 255
blue:
description: Blue
default: 128
example: 0
selector:
number:
min: 0
max: 255
count:
description: Number of cycles
default: 5
example: 3
selector:
number:
min: 1
max: 10
duration:
description: Duration of each cycle in ms
default: 1000
example: 2000
selector:
number:
min: 1
max: 30000
dutycycle:
description: Duty cycle, percentage of time light is on for in each cycle
default: 50
example: 10
selector:
number:
min: 1
max: 99
mode: queued
max: 10