Return to previous state (Lights)

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

1 Like

Anyone have any ideas?

Does anyone have a better way to implement the above with less code?

wow almost a year since i started this thread and no comments at all.

Well part of the issue is that the format is unreadable. You should edit the post and use the </> button above around your code so people can read the yaml. Also, it would help if you simplified the issue. That first post is huge, going to scare people away. If you can make the code readable, I’ll give it a whirl.

example of good spacing and format:

emulated_hue:
  host_ip: 192.168.1.xxx
  listen_port: 8300
  off_maps_to_on_domains:
    - script
    - scene
  expose_by_default: true
  exposed_domains:
    - light
    - switch
    - script
    - input_boolean
2 Likes