Advertisement
xangin

2 step light switch control

Nov 19th, 2021 (edited)
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 6.49 KB | None | 0 0
  1. ###########################################################
  2. ##
  3. ##       Counter
  4. ##
  5. ###########################################################
  6. counter:
  7.   click_times:
  8.     initial: 0
  9.     step: 1
  10.  
  11. ###########################################################
  12. ##
  13. ##       Timer
  14. ##
  15. ##  if switch over how long times no action it will reset
  16. ##  to default state is 0  
  17. ###########################################################
  18. timer:
  19.   click_times_timer:
  20.     duration: '00:05:00'
  21.  
  22. ###########################################################
  23. ##
  24. ##       Switch
  25. ##
  26. ###########################################################
  27. switch:
  28.   - platform: template
  29.     switches:
  30.      ########### if need click once
  31.       light_odd: #entity name that you want
  32.         unique_id: "light_odd" #same as entity name
  33.         value_template: >
  34.          {{  ((states("counter.click_times")| int % 2) == 1) and is_state('switch.AAAAAAAA','on') }}
  35.         turn_on:
  36.           - alias: "Set variables"
  37.             variables:
  38.               entities:
  39.                - switch.AAAAAAAA
  40.               delay_times: 500
  41.           - choose:
  42.              # if click times only click once
  43.               - conditions: >
  44.                  {{ (states("counter.click_times")| int % 2) == 1 }}
  45.                 sequence:
  46.                  # off -> on once    
  47.                   - service: switch.turn_off
  48.                     target:
  49.                       entity_id: "{{ entities }}"
  50.                   - delay:
  51.                       milliseconds: "{{ delay_times }}"          
  52.                   - service: switch.turn_on
  53.                     target:
  54.                       entity_id: "{{ entities }}"
  55.                   - delay:
  56.                       milliseconds: "{{ delay_times }}"  
  57.                   # off -> on twice
  58.                   - service: switch.turn_off
  59.                     target:
  60.                       entity_id: "{{ entities }}"
  61.                   - delay:
  62.                       milliseconds: "{{ delay_times }}"  
  63.                   - service: switch.turn_on
  64.                     target:
  65.                       entity_id: "{{ entities }}"
  66.                      
  67.               # if click times already click twice
  68.               - conditions: >
  69.                  {{ (states("counter.click_times")| int % 2) == 0 }}
  70.                 sequence:
  71.                  # reset counter
  72.                   - service: counter.reset
  73.                     target:
  74.                       entity_id: counter.click_times
  75.                   # off -> on once    
  76.                   - service: switch.turn_off
  77.                     target:
  78.                       entity_id: "{{ entities }}"          
  79.                   - delay:
  80.                       milliseconds: "{{ delay_times }}"            
  81.                   - service: switch.turn_on
  82.                     target:
  83.                       entity_id: "{{ entities }}"
  84.          
  85.         turn_off:
  86.           service: switch.turn_off
  87.           target:
  88.             entity_id: switch.AAAAAAAA
  89.  
  90.   ########### if need click twice
  91.   - platform: template
  92.     switches:
  93.       light_even: #entity name that you want
  94.         unique_id: "light_even" #same as entity name      
  95.         value_template: >
  96.          {{  ((states("counter.click_times")| int % 2) == 0) and is_state('switch.AAAAAAAA','on') }}
  97.  
  98.         turn_on:
  99.           - alias: "Set variables"
  100.             variables:
  101.               entities:
  102.                - switch.AAAAAAAA  
  103.               delay_times: 500
  104.           - choose:
  105.              # if click times already click twice
  106.               - conditions: >
  107.                  {{ (states("counter.click_times")| int % 2) == 0 }}
  108.                 sequence:
  109.                  # reset counter
  110.                   - service: counter.reset
  111.                     target:
  112.                       entity_id: counter.click_times
  113.                   # off -> on once    
  114.                   - service: switch.turn_off
  115.                     target:
  116.                       entity_id: "{{ entities }}"          
  117.                   - delay:
  118.                       milliseconds: "{{ delay_times }}"            
  119.                   - service: switch.turn_on
  120.                     target:
  121.                       entity_id: "{{ entities }}"
  122.                   - delay:
  123.                       milliseconds: "{{ delay_times }}"  
  124.                   # off -> on twice
  125.                   - service: switch.turn_off
  126.                     target:
  127.                       entity_id: "{{ entities }}"
  128.                   - delay:
  129.                       milliseconds: "{{ delay_times }}"  
  130.                   - service: switch.turn_on
  131.                     target:
  132.                       entity_id: "{{ entities }}"
  133.                      
  134.               # if click times only click once
  135.               - conditions: >
  136.                  {{ (states("counter.click_times")| int % 2) == 1 }}
  137.                 sequence:
  138.                   # off -> on once    
  139.                   - service: switch.turn_off
  140.                     target:
  141.                       entity_id: "{{ entities }}"          
  142.                   - delay:
  143.                       milliseconds: "{{ delay_times }}"            
  144.                   - service: switch.turn_on
  145.                     target:
  146.                       entity_id: "{{ entities }}"
  147.  
  148.         turn_off:
  149.           service: switch.turn_off
  150.           target:
  151.             entity_id: switch.AAAAAAAA
  152.  
  153.  
  154. ###########################################################
  155. ##
  156. ##       Automation
  157. ##
  158. ###########################################################            
  159. automation:
  160.   - id: click_times_count
  161.     alias: click_times_count
  162.     trigger:
  163.       platform: state
  164.       entity_id: switch.AAAAAAAA
  165.       to: 'on'
  166.  
  167.     action:
  168.       service: counter.increment
  169.       target:
  170.         entity_id: counter.click_times  
  171.  
  172.    
  173.   - id: click_times_timer_start
  174.     alias: click_times_timer_start
  175.     trigger:
  176.       platform: state
  177.       entity_id: switch.AAAAAAAA  
  178.     action:
  179.       - service: timer.start
  180.         entity_id: timer.click_times_timer
  181.  
  182.        
  183.   - id: click_times_reset
  184.     alias: click_times_reset
  185.     initial_state: true
  186.     trigger:
  187.       - platform: event
  188.         event_type: timer.finished
  189.         event_data:
  190.           entity_id: timer.click_times_timer                    
  191.     action:        
  192.       - service: counter.reset
  193.         target:
  194.           entity_id: counter.click_times    
  195.       - service: timer.cancel
  196.         entity_id: timer.click_times_timer        
  197.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement