I did some changes and fixed some things based on the comments above. I am using the recorder to save the changes of input_number and removed the initial from the window_position.
There is also the fix on automations 411_1 and 412_1 sugested by @foraster.
Also I added underscore 1 on each element in order to make it easier to add multiple covers by replacing the _1.
configuration.yaml
recorder:
timer:
window_up_1:
duration: '00:00:18'
window_down_1:
duration: '00:00:18'
input_number:
window_position_1:
name: "Window 1 Position"
min: 0
max: 18
window_set_position_1:
name: "Window 1 Set Position"
min: 0
max: 100
cover:
- platform: template
covers:
window_cover_1:
friendly_name: "Window 1 Cover"
position_template: "{{ (((states.input_number.window_position_1.state | int) * 100)/18 | int) }}"
open_cover:
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power2'
payload: 'OFF'
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power1'
payload: 'ON'
close_cover:
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power1'
payload: 'OFF'
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power2'
payload: 'ON'
stop_cover:
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power1'
payload: 'OFF'
- service: mqtt.publish
data:
topic: 'cmnd/sonoff_1/power2'
payload: 'OFF'
automations.yaml
- id: '301_1'
alias: Window 1 - MQTT Button1 OFF (up)
trigger:
- payload: 'OFF'
platform: mqtt
topic: stat/sonoff_1/POWER1
action:
- service: timer.cancel
entity_id: timer.window_up_1
- id: '302_1'
alias: Window 1 - MQTT Button2 OFF (down)
trigger:
- payload: 'OFF'
platform: mqtt
topic: stat/sonoff_1/POWER2
action:
- service: timer.cancel
entity_id: timer.window_down_1
- id: '303_1'
alias: Window 1 - Timer stopped
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.window_up_1
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.window_down_1
action:
- service: cover.stop_cover
data:
entity_id: cover.window_cover_1
- id: '311_1'
alias: Window 1 - MQTT Button1 ON (up)
trigger:
- payload: 'ON'
platform: mqtt
topic: stat/sonoff_1/POWER1
condition:
condition: template
value_template: '{{ as_timestamp(now()) - as_timestamp(states.input_number.window_set_position_1.last_updated) > 3 }}'
action:
- service: timer.start
data:
duration: 00:00:18
entity_id: timer.window_up_1
- id: '312_1'
alias: Window 1 - MQTT Button2 ON (down)
trigger:
- payload: 'ON'
platform: mqtt
topic: stat/sonoff_1/POWER2
condition:
condition: template
value_template: '{{ as_timestamp(now()) - as_timestamp(states.input_number.window_set_position_1.last_updated) > 3 }}'
action:
- service: timer.start
data:
duration: 00:00:18
entity_id: timer.window_down_1
- id: '411_1'
alias: Window 1 - Position UP
trigger:
platform: time
seconds: '/1'
condition:
condition: state
entity_id: 'timer.window_up_1'
state: 'active'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.window_position_1
value: "{% if (states.input_number.window_position_1.state | int) < 18 %}\n {{\
\ (states.input_number.window_position_1.state | int) + 1 }}\n{% else %}\n \
\ 18\n{% endif %}"
- id: '412_1'
alias: Window 1 - Position Down
trigger:
platform: time
seconds: '/1'
condition:
condition: state
entity_id: 'timer.window_down_1'
state: 'active'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.window_position_1
value: "{% if (states.input_number.window_position_1.state | int) > 1 %}\n {{\
\ (states.input_number.window_position_1.state | int) - 1 }}\n{% else %}\n \
\ 0\n{% endif %}"
- id: '511_1'
alias: Window 1 - Set Position Down
trigger:
platform: state
entity_id: input_number.window_set_position_1
condition:
condition: template
value_template: "{{ (states.input_number.window_position_1.state | int) > ((((states.input_number.window_set_position_1.state | int ) * 18 )/100) | int) }}"
action:
- service: timer.start
data_template:
entity_id: timer.window_down_1
duration: "{{ '00:00:%02d' | format( ((states.input_number.window_position_1.state | int) - ((((states.input_number.window_set_position_1.state | int ) * 18 )/100) | int) ) | abs ) }}"
- service: cover.close_cover
data:
entity_id: cover.window_cover_1
- id: '512_1'
alias: Window 1 - Set Position UP
trigger:
platform: state
entity_id: input_number.window_set_position_1
condition:
condition: template
value_template: "{{ (states.input_number.window_position_1.state | int) < ((((states.input_number.window_set_position_1.state | int ) * 18 )/100) | int) }}"
action:
- service: timer.start
data_template:
entity_id: timer.window_up_1
duration: "{{ '00:00:%02d' | format( ((states.input_number.window_position_1.state | int) - ((((states.input_number.window_set_position_1.state | int ) * 18 )/100) | int) ) | abs ) }}"
- service: cover.open_cover
data:
entity_id: cover.window_cover_1
groups.yaml
window_cover_1:
name: Window Cover 1
entities:
- cover.window_cover_1
- input_number.window_set_position_1
- input_number.window_position_1
- timer.window_up_1
- timer.window_down_1