My current set up consists of multiple scripts, input sliders and automations. First automation is to determine what is playing on Plex. If it detects that it is a TV show then the automation determines the state of 4 lights, appends that information to multiple sliders and turns on a scene. The second automation is when it detects the TV show paused it looks at the slider values and returns those lights to their previous state. The code was pieced together with a whole bunch of examples I found on the forums. I was wondering if there is a simpler way to do the same without so many scripts. Currently there is one script which appends all the brightness values to input sliders. Then for each light there are two scripts to determine whether the light is at a specific brightness and then either return to previous state or keep them off.
Any advice would be helpful. I really want to reduce the amount of coding in the yaml file.
automations
- alias: TV Resume
trigger:
- platform: state
entity_id: media_player.apple_tv
to: βplayingβ
condition:
condition: and
conditions:
- condition: template
value_template: β{{ states.media_player.apple_tv.attributes.media_content_type == βtvshowβ }}β- condition: state
entity_id: input_boolean.ambiance
state: βonβ
action:- service: script.kitchen_slider
- service: scene.turn_on
entity_id: scene.ambiance
- alias: TV Paused
trigger:
- platform: state
entity_id: media_player.apple_tv
to: βpausedβ
condition:
condition: and
conditions:
- condition: template
value_template: β{{ states.media_player.apple_tv.attributes.media_content_type == βtvshowβ }}β- condition: state
entity_id: input_boolean.ambiance
state: βonβ
action:- service: script.kit_prev_state
- service: script.kit_prev_state_off
- service: script.cab_prev_state
- service: script.cab_prev_state_off
- service: script.dining_prev_state
- service: script.dining_prev_state_off
- service: script.dining_hall_prev_state
- service: script.dining_hall_prev_state_off
Scene
- name: Ambiance
entities:
light.kitchen_:
state: off
light.under_cabinet:
state: on
brightness: 100
light.dining_room:
state: off
light.dining_hall:
state: off
switch.family_room:
state: off
scripts
Append to slider
kitchen_slider:
alias: Record Kitchen Brightness Level
sequence:
- service: input_slider.select_value
data_template:
entity_id: input_slider.kitchen_brightness
value: β{% if states.light.kitchen_.state == βoffβ %}0{% else %}{{states.light.kitchen_.attributes.brightness}}{% endif %}β
- service: input_slider.select_value
data_template:
entity_id: input_slider.cabinet_brightness
value: β{% if states.light.under_cabinet.state == βoffβ %}0{% else %}{{states.light.under_cabinet.attributes.brightness}}{% endif %}β
- service: input_slider.select_value
data_template:
entity_id: input_slider.dining_room_brightness
value: β{% if states.light.dining_room.state == βoffβ %}0{% else %}{{states.light.dining_room.attributes.brightness}}{% endif %}β
- service: input_slider.select_value
data_template:
entity_id: input_slider.dining_hall_brightness
value: β{% if states.light.dining_hall.state == βoffβ %}0{% else %}{{states.light.dining_hall.attributes.brightness}}{% endif %}β
Return previous state
kit_prev_state:
alias: Return Kitchen Light Back To Previous state
sequence:
- condition: template
value_template: β{{ states.input_slider.kitchen_brightness.state| int > 5 }}β
- service: light.turn_on
entity_id: light.kitchen_
data_template:
brightness: β{{states.input_slider.kitchen_brightness.state | int}}β
kit_prev_state_off:
alias: Return Kitchen Light Off
sequence:
- condition: template
value_template: β{{ states.input_slider.kitchen_brightness.state| int <= 5 }}β
- service: light.turn_off
entity_id: light.kitchen_
cab_prev_state:
alias: Return Cabinet Light Back To Previous state
sequence:
- condition: template
value_template: β{{ states.input_slider.cabinet_brightness.state| int > 5 }}β
- service: light.turn_on
entity_id: light.under_cabinet
data_template:
brightness: β{{states.input_slider.cabinet_brightness.state | int}}β
cab_prev_state_off:
alias: Return Cabinet Light Off
sequence:
- condition: template
value_template: β{{ states.input_slider.cabinet_brightness.state| int <= 5 }}β
- service: light.turn_off
entity_id: light.under_cabinet
dining_prev_state:
alias: Return Dining Room Light Back To Previous state
sequence:
- condition: template
value_template: β{{ states.input_slider.dining_room_brightness.state| int > 5 }}β
- service: light.turn_on
entity_id: light.dining_room
data_template:
brightness: β{{states.input_slider.dining_room_brightness.state | int}}β
dining_prev_state_off:
alias: Return Dining Light Off
sequence:
- condition: template
value_template: β{{ states.input_slider.dining_room_brightness.state| int <= 5 }}β
- service: light.turn_off
entity_id: light.dining_room
dining_hall_prev_state:
alias: Return Dining Hall Light Back To Previous state
sequence:
- condition: template
value_template: β{{ states.input_slider.dining_hall_brightness.state| int > 5 }}β
- service: light.turn_on
entity_id: light.dining_hall
data_template:
brightness: β{{states.input_slider.dining_hall_brightness.state | int}}β
dining_hall_prev_state_off:
alias: Return Dining Hall Light Off
sequence:
- condition: template
value_template: β{{ states.input_slider.dining_hall_brightness.state| int <= 5 }}β
- service: light.turn_off
entity_id: light.dining_hall