JLindvig

Frederik v. 0.5

Feb 23rd, 2021 (edited)
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 3.92 KB | None | 0 0
  1. blueprint:
  2.   name: Light and sensors
  3.   description: Kommer
  4.   domain: automation
  5.  
  6.   input:
  7.     sensor:
  8.       name: Sensor
  9.       description: The sensor which shall trigger the automation
  10.       selector:
  11.         entity:
  12.           domain: binary_sensor
  13.  
  14.     light:
  15.       name: Light(s)
  16.       description: Light(s) to operate
  17.       selector:
  18.         target:
  19.     time_after:
  20.       name: Time Start
  21.       description: The earliest time to trigger
  22.       default: "07:00:00"
  23.       selector:
  24.         time:
  25.     time_before:
  26.       name: Time End
  27.       description: The latest time to trigger
  28.       default: "23:30:00"
  29.       selector:
  30.         time:
  31.     time_duration:
  32.       name: Duration of sensor
  33.       description: How long time should pass before trigger
  34.       default: "00:10:00"
  35.       selector:
  36.         time:
  37.     delay_min:
  38.       name: Delay minimum
  39.       description: How short delay
  40.       default: 60
  41.       selector:
  42.         number:
  43.           min: 0
  44.           max: 300
  45.           mode: slider
  46.           step: 10
  47.           unit_of_measurement: "seconds"
  48.  
  49.     delay_max:
  50.       name: Delay maximum
  51.       description: How long delay
  52.       default: 300
  53.       selector:
  54.         number:
  55.           min: 10
  56.           max: 600
  57.           mode: slider
  58.           step: 10
  59.           unit_of_measurement: "seconds"
  60.  
  61. mode: single
  62. max_exceeded: silent
  63. variables:
  64.   sensor: !input sensor
  65.   time_after: !input time_after
  66.   time_before: !input time_before
  67.   delay_min: !input delay_min
  68.   delay_max: !input delay_max
  69.  
  70. trigger:
  71.   - platform: state
  72.     entity_id: !input sensor
  73.     for: !input time_duration
  74.  
  75.   - platform: time
  76.     at:
  77.      - !input time_after
  78.       - !input time_before
  79.  
  80. action:
  81.   # - service: persistent_notification.create
  82.   #   data:
  83.   #     title: Debug
  84.   #     notification_id: debug
  85.   #     message: |
  86.  #       {{ trigger.platform }}
  87.   #       delay: {{ delay }}
  88.  
  89.   # If trigger is SENSOR or TIME
  90.   - choose:
  91.       # It is a sensor that triggered the automation AND time is between the specified time window
  92.       - conditions:
  93.           - "{{ trigger.platform == 'state' }}"
  94.           - condition: time
  95.             after: !input time_after
  96.             before: !input time_before
  97.         sequence:
  98.           # Wait (DELAY) to see if the sensor changes state, if not after timeout continue
  99.           - wait_template: "{{ is_state(sensor, 'trigger.from_state.state') }}"
  100.             timeout:
  101.               seconds: "{{ range(delay_min | int, delay_max | int) | random }}"
  102.             continue_on_timeout: true
  103.             # Only perform action if the wait timed out
  104.           - choose:
  105.               - conditions: "{{ not wait.completed }}"
  106.                 sequence:
  107.                   - service: "homeassistant.turn_{{ trigger.to_state.state }}"
  108.                     target: !input light
  109.  
  110.     # Time was the trigger
  111.     default:
  112.       - choose:
  113.           # It is morning AND the sensor is ON
  114.           - conditions:
  115.               - "{{ (as_timestamp(trigger.now) | timestamp_custom('%H:%M:%S')) == time_after }}"
  116.               - "{{ is_state(sensor, 'on') }}"
  117.             sequence:
  118.               # Wait (DELAY) to see if the sensor turns off, if not after timeout continue
  119.               - wait_template: "{{ is_state(sensor, 'off') }}"
  120.                 timeout:
  121.                   seconds: "{{ range(delay_min | int, delay_max | int) | random }}"
  122.                 continue_on_timeout: true
  123.               # Only perform action if the wait timed out
  124.               - choose:
  125.                   - conditions: "{{ not wait.completed }}"
  126.                     sequence:
  127.                       - service: homeassistant.turn_on
  128.                         target: !input light
  129.  
  130.         # It is NOT morning, delay and turn OFF
  131.         default:
  132.           - delay: "{{ range(delay_min | int, delay_max | int) | random }}"
  133.           - service: homeassistant.turn_off
  134.             target: !input light
Add Comment
Please, Sign In to add comment