Advertisement
yurghie

Home Assistant dim outdoor lights according to sun

Mar 8th, 2024
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.72 KB | None | 0 0
  1. template:
  2.   - binary_sensor:
  3.     - name: sun_above_horizon
  4.       device_class: light
  5.       state: "{%if state_attr('sun.sun', 'elevation')|float(0) > 2 %}True{%else%}False{%endif%}"
  6.  
  7. input_number:
  8.   outdoor_lights_brightness:
  9.     name: Outdoor light brightness
  10.     min: -1
  11.     max: 101
  12.     step: 1
  13.  
  14. automation:
  15.   - alias: dim outdoor lights according to daylight
  16.     trigger:
  17.       - platform: state
  18.         entity_id: binary_sensor.sun_above_horizon
  19.         to: ['on', 'off']
  20.         from: ['on', 'off']
  21.     variables:
  22.       brightness_pct: "{{ 0 if states('binary_sensor.sun_above_horizon') == 'on' else 100 }}"
  23.       direction: "{{ 1 if states('binary_sensor.sun_above_horizon') == 'on' else -1 }}"
  24.     action:
  25.      # store the starting value
  26.       - service: input_number.set_value
  27.         data:
  28.           entity_id: input_number.outdoor_lights_brightness
  29.           value: "{{brightness_pct}}"
  30.       - repeat:
  31.          # repeat until value is outside of range
  32.           until: "{{states('input_number.outdoor_lights_brightness')|int(-1) < 0 or states('input_number.outdoor_lights_brightness')|int(101) > 100}}"
  33.           sequence:
  34.              #first calculate the new number
  35.             - service: input_number.set_value
  36.               data:
  37.                 entity_id: input_number.outdoor_lights_brightness
  38.                 value: "{{states('input_number.outdoor_lights_brightness')|int(0)+direction}}"
  39.               # then apply new number to the lights
  40.             - service: light.turn_on
  41.               data:
  42.                 entity_id: light.outdoor_lights
  43.                 brightness_pct: "{{states('input_number.outdoor_lights_brightness')|int(0)}}"
  44.             - delay: '00:00:02'
  45.     mode: restart
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement