p33j4y

HA: timer on button

May 31st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 6.31 KB | None | 0 0
  1. #######################################################################
  2. ### wymagania:
  3. ### 1. variable -> https://github.com/rogro82/hass-variables
  4. ### 2. button-card -> https://github.com/custom-cards/button-card
  5. #######################################################################
  6.  
  7. #######################################################################
  8. ### lowelas:
  9. #######################################################################
  10. - type: custom:button-card
  11.   name: >
  12.    [[[
  13.       var temp_name = 'Testowy';
  14.       if (states['timer.test_on'].state == 'active')
  15.         var temp_name = states['sensor.test'].state;
  16.       if (states['timer.test_delay'].state == 'active')
  17.         var temp_name = states['sensor.test'].state;
  18.       return temp_name;
  19.     ]]]
  20.   icon: >
  21.    [[[
  22.       var temp_delay = states['timer.test_delay'].state;
  23.       var temp_icon = 'mdi:toggle-switch';
  24.       if (temp_delay == 'active')
  25.         var temp_icon = 'mdi:toggle-switch-off-outline';
  26.       return temp_icon;
  27.     ]]]
  28.   entity: switch.test
  29.   lock:
  30.     enabled: '[[[ return states["timer.test_delay"].state === "active" ]]]'
  31.     exemptions: []
  32.   styles:
  33.     lock:
  34.       - align-items: center
  35.       - opacity: 0.3
  36.       - color: var(--label-badge-red)
  37.  
  38. #######################################################################
  39. ### konfig:
  40. #######################################################################
  41. variable:
  42.   var_testowa:
  43.     value: 'False'
  44.  
  45. sensor:
  46. - platform: template
  47.   sensors:
  48.     test:
  49.       entity_id:
  50.      - timer.test_on
  51.       - timer.test_delay
  52.       value_template: >-
  53.         {% if is_state('timer.test_on', 'idle') and is_state('timer.test_delay', 'idle') %}
  54.           0
  55.         {% elif is_state('timer.test_delay', 'active') %}
  56.           {% set temp_delay = (state_attr('timer.test_delay', 'duration').split(':')[0]|int * 60 * 60)+(state_attr('timer.test_delay', 'duration').split(':')[1]|int * 60)+(state_attr('timer.test_delay', 'duration').split(':')[2]|int) %}
  57.           {% set temp_now1 = (as_timestamp(now())|int - (states('variable.var_testowa')|int))|int|timestamp_custom('%M:%S') %}
  58.           {% set temp_now2 = (temp_now1.split(':')[0]|int * 60)+(temp_now1.split(':')[1]|int) %}
  59.           {{ (temp_delay - temp_now2)|int|timestamp_custom('%M:%S') }}
  60.         {% else %}
  61.           {% set temp_delay = (state_attr('timer.test_on', 'duration').split(':')[0]|int * 60 * 60)+(state_attr('timer.test_on', 'duration').split(':')[1]|int * 60)+(state_attr('timer.test_on', 'duration').split(':')[2]|int) %}
  62.           {% set temp_now1 = (as_timestamp(now())|int - (states('variable.var_testowa')|int))|int|timestamp_custom('%M:%S') %}
  63.           {% set temp_now2 = (temp_now1.split(':')[0]|int * 60)+(temp_now1.split(':')[1]|int) %}
  64.           {{ (temp_delay - temp_now2)|int|timestamp_custom('%M:%S') }}
  65.         {% endif %}
  66.  
  67. timer:
  68.   test_on:
  69.     name: test_on
  70.     duration: '00:05:00'
  71.   test_delay:
  72.     name: test_delay
  73.     duration: '00:45:00'
  74.  
  75. automation:
  76.  #################### odśwież licznik na przyciku
  77.   - id: test_refresh
  78.     alias: test_refresh
  79.     trigger:
  80.     - platform: time_pattern
  81.       seconds: "/2"
  82.     action:
  83.     - condition: template
  84.       value_template: "{{ 'False' if (is_state('timer.test_on', 'idle') and is_state('timer.test_delay', 'idle')) else 'True' }}"
  85.     - service: homeassistant.update_entity
  86.       entity_id:
  87.        - sensor.test
  88.     - condition: template
  89.       value_template: "{{ 'True' if (is_state('timer.test_on', 'idle') and is_state('timer.test_delay', 'idle')) else 'False' }}"
  90.     - service: homeassistant.turn_off
  91.       entity_id:
  92.        - automation.test_refresh
  93.  
  94.   #################### włącz odświeżanie licznika na przycisku
  95.   - id: test_refresh_on
  96.     alias: test_refresh_on
  97.     trigger:
  98.     - platform: event
  99.       event_type: timer.started
  100.       event_data:
  101.         entity_id: timer.test_on
  102.     - platform: event
  103.       event_type: timer.started
  104.       event_data:
  105.         entity_id: timer.test_delay
  106.     - platform: event
  107.       event_type: timer.restarted
  108.       event_data:
  109.         entity_id: timer.test_on
  110.     - platform: event
  111.       event_type: timer.restarted
  112.       event_data:
  113.         entity_id: timer.test_delay
  114.     action:
  115.     - condition: template
  116.       value_template: "{{ 'False' if (is_state('timer.test_on', 'idle') and is_state('timer.test_delay', 'idle')) else 'True' }}"
  117.     - service: homeassistant.turn_on
  118.       entity_id:
  119.        - automation.test_refresh
  120.  
  121.   #################### wyłącz odświeżanie licznika na przycisku
  122.   - id: test_refresh_off
  123.     alias: test_refresh_off
  124.     trigger:
  125.     - platform: event
  126.       event_type: timer.finished
  127.       event_data:
  128.         entity_id: timer.test_on
  129.     - platform: event
  130.       event_type: timer.finished
  131.       event_data:
  132.         entity_id: timer.test_delay
  133.     action:
  134.     - condition: template
  135.       value_template: "{{ 'True' if (is_state('timer.test_on', 'idle') and is_state('timer.test_delay', 'idle')) else 'False' }}"
  136.     - service: homeassistant.turn_off
  137.       entity_id:
  138.        - automation.test_refresh
  139.  
  140.   #################### włącz
  141.   - id: test_on
  142.     alias: test_on
  143.     trigger:
  144.    ## jakiś trigger
  145.     action:
  146.     - condition: template
  147.       value_template: "{{ 'True' if (is_state('timer.test_delay', 'idle') and trigger.to_state.state == 'on') else 'False' }}"
  148.     - service: timer.start
  149.       entity_id: timer.test_on
  150.     - service: variable.set_variable
  151.       data:
  152.         variable: var_testowa
  153.         value_template: "{{ as_timestamp(now())|int }}"
  154.     - condition: state
  155.       entity_id: switch.test
  156.       state: 'off'
  157.     - service: switch.turn_on
  158.       entity_id: switch.test
  159.  
  160.   #################### wyłącz
  161.   - id: test_off
  162.     alias: test_off
  163.     trigger:
  164.     - platform: event
  165.       event_type: timer.finished
  166.       event_data:
  167.         entity_id: timer.test_on
  168.     - platform: event
  169.       event_type: timer.cancelled
  170.       event_data:
  171.         entity_id: timer.test_on
  172.     action:
  173.     - condition: state
  174.       entity_id: switch.test
  175.       state: 'on'
  176.     - service: switch.turn_off
  177.       entity_id: switch.test
  178.     - service: timer.start
  179.       entity_id: timer.test_delay
  180.     - service: variable.set_variable
  181.       data:
  182.         variable: var_testowa
  183.         value_template: "{{ as_timestamp(now())|int }}"
Add Comment
Please, Sign In to add comment