Hmm, I thought I could use condition shorthand in an action sequence.
Also, the variables below assume you’re on 0.117 or higher (as does the template shorthand). If you’re not at that version, it would be easiest to move those variables to outside the script and pass them in as parameters rather than have to calculate them multiple times in the same script.
For example, you would call cover_set_position like this instead of just passing in position:
- service: script.cover_set_position
data:
position: "{{position}}"
desired_state: >-
{%- set rounded = 50 * ((( position | float)/50) | round) -%}
{%- if rounded == 0 -%} 'chiuso'
{%- elif rounded == 50 -%} 'tapparella'
{%- else -%} 'aperto'
{%- endif -%}
current_assumed_state: "{{states('input_select.posizione_tapparella_camera') }}"
cover: cover.bfdc1128af1eb05af1w4i6
If you do it that way, get rid of the -variables
section. I’m wondering though why that section didn’t raise an error for you if you were on something before 0.117 though.
cover_set_position:
# Input is a number from 0-100. Round it to the closest 0, 50, 100.
# If 0 or 100, just use the open/close script above. Easy.
# If 50, need to send a close command from closed position.
sequence:
# Need to know the current state of the cover to know how to handle it.
- variables:
# Capture the state we think the blinds are
current_assumed_state: "{{states('input_select.posizione_tapparella_camera') }}"
desired_state: >-
{%- set rounded = 50 * ((( position | float)/50) | round) -%}
{%- if rounded == 0 -%} 'chiuso'
{%- elif rounded == 50 -%} 'tapparella'
{%- else -%} 'aperto'
{%- endif -%}
# Template condition shorthand. If the desired state matches the current state, don't do anything.
# This is likely to happen using the slider as it might change value technically, but the rounded
# value would be the same.
- condition: template
value_template: "{{ current_assumed_state != desired_state }}"
# This is a new state. Try to set it.
- choose:
# If this is an open/close command, just call that script above.
- conditions:
- condition: template
value_template: "{{ desired_state != 'tapparella' }}"
sequence:
- service: script.cover_open_close
data:
action: "{{desired_state}}"
cover: {{cover}}
# If it wasn't open or close position, do this instead
default:
# To set this state, we have to close the cover first, then send another close command.
# If the cover isn't fully closed, send a closed command first.
- choose:
- conditions:
# Use our only sensor to know if the cover is closed. We could use the
# last input select state, but this should be safer. If this sensor is not on,
# send a close command.
- "{{ is_state('binary_sensor.cover_closed', 'on') }}
sequence:
- service: cover.close_cover
data:
entity_id: "{{ cover }}"
# We either sent a closed command above, or it's already closed.
# In either case, don't continue until the cover says its closed.
# If it's already closed, this wont wait at all and will just move on.
- wait_template: "{{ is_state('binary_sensor.cover_closed', 'on') }}"
timeout: "00:01:00"
continue_on_timeout: false
# If we got here, the cover closed. Send another close command.
- service: cover.close_cover
data:
entity_id: "{{ cover }}"
# Finally, update the input select
- service: input_select.select_option
entity_id: input_select.posizione_tapparella_camera
data:
option: tapparella
As for that last error, the variable name should be ‘cover’, not ‘entity_id’
data:
action: "{{desired_state}}"
cover: {{cover}}