On another note… in an attempt to fix my issues with states and position with my curtains, i did clean up the code above and explain what’s happening with that — It was confusing, ill admit.
I adjusted it for my sliding curtains; but its should be the same for shades or blinds etc.
You’ll need 2 timers setup in config yaml
timer:
curtains_open:
name: "Curtains Open"
curtains_closed:
name: "Curtains Closed"
Also, you’ll need 2 input_number’s setup (use helper) set max time to the time it takes for your curtains to fully open or close. In my case, 15s
input_number.curtains_set_position
input_number.curtains_position
Heres the very cleaned up, and properly formatted automation.yaml code from above— with some explaining.
#Force state update from Tuya App Every 2 sec.
#In my case this did nothing. Tuya didn’t update — but it should’ve
- id: update_status_from_tuya
alias: 'Update Status from Tuya '
trigger:
- platform: time_pattern
seconds: /2
action:
- service: tuya.force_update
data: {}
#Cancel curtains_open Timer upon Unknown state
- id: open_to_unknown
alias: 'Open to unknown'
trigger:
- entity_id: cover.balcony_curtains
from: open
platform: state
to: unknown
action:
- service: timer.cancel
entity_id: timer.curtains_open
#Cancel curtains_closed Timer upon Unknown state
- id: closed_to_unknown
alias: 'Closed to unknown'
trigger:
- entity_id: cover.balcony_curtains
from: closed
platform: state
to: unknown
action:
- service: timer.cancel
entity_id: timer.curtains_closed
#Stop the Curtains when either Timer is Finished
- id: curtains_timer_finished
alias: 'Curtains - Timer Finished'
trigger:
- platform: event
event_data:
entity_id: timer.curtains_closed
event_type: timer.finished
- platform: event
event_data:
entity_id: timer.curtains_open
event_type: timer.finished
condition: []
action:
- service: cover.stop_cover
data:
entity_id: cover.balcony_curtains
#Start curtains_open Timner on OPEN state
#Adjust Duration to the time it takes to open. In my case, it takes 15sec to fully Open.
- id: open_button
alias: 'Open Button'
trigger:
- platform: state
entity_id: cover.balcony_curtains
to: open
condition:
- condition: template
value_template: '{{ as_timestamp(now()) - as_timestamp(states.input_number.curtains_set_position.last_updated)>3 }}'
action:
- service: timer.start
data:
duration: 00:00:15
entity_id: timer.curtains_open
#Start curtains_closed Timner on CLOSED state
#Adjust Duration to the time it takes to Close. In my case, it takes 15sec to fully Close.
- id: close_button
alias: 'Close Button'
trigger:
- platform: state
entity_id: cover.balcony_curtains
to: closed
condition:
- condition: template
value_template: '{{ as_timestamp(now()) - as_timestamp(states.input_number.curtains_set_position.last_updated)> 3 }}'
action:
- service: timer.start
data:
duration: 00:00:15
entity_id: timer.curtains_closed
#When curtains_open Timer Starts, Set the Curtains Position value
- id: position_open
alias: 'Position Open'
trigger:
platform: time_pattern
seconds: /1
condition:
- condition: state
entity_id: timer.curtains_open
state: active
action:
- service: input_number.set_value
data_template:
entity_id: input_number.curtains_position
value: '{{ (states.input_number.curtains_position.state | int) + 1 }}'
#When curtains_closed Timer Starts, Set the Curtains Position value
- id: position_closed
alias: 'Position Closed'
trigger:
platform: time_pattern
seconds: /1
condition:
- condition: state
entity_id: timer.curtains_closed
state: active
action:
- service: input_number.set_value
data_template:
entity_id: input_number.curtains_position
value: '{{ (states.input_number.curtains_position.state | int) - 1 }}'
#Set the Curtains Position value towards CLOSED until the 15sec Close time is reached.
#Be Sure to change my 15s value to yours in the value_template and Duration below.
#Make sure it matches your close_button second values above.
- id: set_position_closed
alias: 'Set Position Closed'
trigger:
- platform: state
entity_id: input_number.curtains_set_position
condition:
- condition: template
value_template: '{{ (states.input_number.curtains_position.state | int) > ((((states.input_number.curtains_set_position.state | int ) * 15 )/100) | int) }}' # <-- Change 15 to your Close time in seconds
action:
- service: timer.start
data_template:
entity_id: timer.curtains_closed
duration: "{{ '00:00:%02d' | format( ((states.input_number.curtains_position.state | int) - ((((states.input_number.curtains_set_position.state | int ) * 15 )/100) | int) ) | abs ) }}" # <-- Change 15 to your Close time in seconds
- service: cover.close_cover
data:
entity_id: cover.balcony_curtains
#Set the Curtains Position value towards OPEN until the 15sec Open time is reached.
#Be Sure to change my 15s value to yours in the value_template and Duration below.
#Make sure it matches your open_button second values above.
- id: set_position_open
alias: 'Set Position Open'
trigger:
- platform: state
entity_id: input_number.curtains_set_position
condition:
- condition: template
value_template: '{{ (states.input_number.curtains_position.state | int) < ((((states.input_number.curtains_set_position.state | int ) * 15 )/100) | int) }}' # <-- Change 15 to your Open time in seconds
action:
- service: timer.start
data_template:
entity_id: timer.curtains_open
duration: "{{ '00:00:%02d' | format( ((states.input_number.curtains_position.state | int) - ((((states.input_number.curtains_set_position.state | int ) * 15 )/100) | int) ) | abs ) }}" # <-- Change 15 to your Open time in seconds
- service: cover.open_cover
data:
entity_id: cover.balcony_curtains
Ok, keep in mind this is not my code; i simply cleaned up and attempted to adapt the above posted code to my needs with my curtains. Unfortunately, this seems to be an ongoing issue with Tuya not updating their state when requested. I’m not sure anything from our end can be done about this. But maybe someone will have better luck then i did with this code.
In theory, with this code, you’d be able to setup a slider in the frontend (with input_number.curtains_position) and choose the position you want.
Ah well… the search continues for a solution for these migraine-inducing Tuya devices…