Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Created August 4, 2020 21:22
Show Gist options
  • Save bessarabov/b23c1a3b327f5ccf268ecba02fe5cc62 to your computer and use it in GitHub Desktop.
Save bessarabov/b23c1a3b327f5ccf268ecba02fe5cc62 to your computer and use it in GitHub Desktop.
input_boolean:
uf_lamp:
input_number:
on_seconds:
min: 0
max: 10000000000
step: 1
mode: box
off_seconds:
min: 0
max: 10000000000
step: 1
mode: box
timestamp:
min: 0
max: 10000000000
step: 1
mode: box
switch:
- platform: template
switches:
uf_lamp:
value_template: "{{ is_state('input_boolean.uf_lamp', 'on') }}"
turn_on:
service: input_boolean.turn_on
entity_id: input_boolean.uf_lamp
turn_off:
service: input_boolean.turn_off
entity_id: input_boolean.uf_lamp
sensor:
- platform: history_stats
name: Switch on
entity_id: switch.uf_lamp
state: 'on'
type: time
start: '{{ 0 }}'
end: '{{ now() }}'
- platform: history_stats
name: Switch off
entity_id: switch.uf_lamp
state: 'off'
type: time
start: '{{ 0 }}'
end: '{{ now() }}'
automation:
- trigger:
platform: state
entity_id: switch.uf_lamp
to: 'on'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.off_seconds
value: "{{ states('input_number.off_seconds') | float + ((as_timestamp(now()) - (states('input_number.timestamp')) | float)) }}"
- service: input_number.set_value
data_template:
entity_id: input_number.timestamp
value: "{{ as_timestamp(now()) }}"
- trigger:
platform: state
entity_id: switch.uf_lamp
to: 'off'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.on_seconds
value: "{{ states('input_number.on_seconds') | float + ((as_timestamp(now()) - (states('input_number.timestamp')) | float)) }}"
- service: input_number.set_value
data_template:
entity_id: input_number.timestamp
value: "{{ as_timestamp(now()) }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment