Hi, I am using the input slider control to control the blackout shades in our home theater. I have the slider set up at four stops: 0, 25, 75, 100 and I have been able to make it work to a point, but I am having trouble with the conditional logic that allows me to choose which direction the blinds should go, depending if they should be opened or closed to a certain position.
I have seen this code that would probably be ideal, but I can’t seem to make it work. I’m thinking that I am missing the larger context of the program structure and I’m wondering if anyone would know what the full extent of the code should be:
I am hoping that the all important “trigger.to_state.state” and “trigger.from_state.state” variables will be passed to the code decision block as this is what determines the direction for the 12v DC blind motor. This will then trigger the BroadLink RF/IR blaster hub to send the appropriate radio signal to the motor. This system should also work to send the BroadLink RF radio control signals my 12v window opener.
So after many, many trials and errors I finally figured out how to do it. This script is the full meal deal and involves passing of variables and conditional branching to two different scripts, depending on blind motor open/close direction.
I used sensor displays to confirm that the various calculations for excursion time and direction were correct. I would expect there are better ways to do this, and I’m going to try it with 2 alternative methods, appDaemon and Node-red.
homeassistant:
# Name of the location where Home Assistant is running
name: Home
# Location required to calculate the time the sun rises and sets
latitude: 49.3193
longitude: -124.3136
# Impacts weather/sunrise data (altitude above sea level in meters)
elevation: 66
# metric for Metric, imperial for Imperial
unit_system: imperial
# Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
time_zone: UTC
# Enables the frontend
frontend:
http:
# Uncomment this to add a password (recommended!)
# api_password: PASSWORD
# Uncomment this if you are using SSL or running in Docker etc
# base_url: example.duckdns.org:8123
# Checks for available updates
updater:
# Discover some devices automatically
# discovery:
# Allows you to issue voice commands from the frontend in enabled browsers
# conversation:
# Enables support for tracking state changes over time.
history:
# View all events in a logbook
logbook:
mqtt:
broker: 192.168.1.71
port: 1883
client_id: home-assistant-1
#protocol: 3.1.1
#username: mosquitto
#password: raspberry
#publish.single('setblinds', 'good', hostname=broker)
input_slider:
theater_blackout_blinds:
name: 'Theater Blackout Blinds'
icon: mdi:window-closed
min: 0
max: 100
step: 25
initial: 0
switch 10:
platform: broadlink
host: 192.168.1.249
mac: 'b4:43:0d:ee:e7:bf'
timeout: 15
switches:
blinds:
friendly_name: "Open Blackout Blinds"
command_off: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
command_on: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
switch 20:
platform: broadlink
host: 192.168.1.249
mac: 'b4:43:0d:ee:e7:bf'
timeout: 15
switches:
blinds:
friendly_name: "Close Blackout Blinds"
command_off: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
command_on: 'JgAoAAABJ5ISRxJIEkcSSBJHEkgSRxJIEkcSSBJHEkcTRxJHEkgSRxIADQU='
sensor 1:
- platform: mqtt
name: "Slider Start"
state_topic: "setStart/#"
sensor 2:
- platform: mqtt
name: "Slider Stop"
state_topic: "setStop/#"
sensor 3:
- platform: mqtt
name: "Percent Travel"
state_topic: "setPctTravel/#"
sensor 4:
- platform: mqtt
name: "Direction Travel"
state_topic: "setDirection/#"
sensor 5:
- platform: mqtt
name: "Travel Time"
state_topic: "setTravelTime/#"
automation:
- alias: 'Test Slider'
trigger:
platform: state
entity_id: input_slider.theater_blackout_blinds
action:
service: script.turn_on
#service_template: >
#{% if (trigger.to_state.state | int) - (trigger.from_state.state | int) < 0 %}
#entity_id: script.slider_moved
#{% else %}
#entity_id: script.slider_moved
#{% endif %}
data_template:
entity_id: >
{% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
script.slider_moved_close
{% else %}
script.slider_moved_open
{% endif %}
variables:
set_start: '{{ trigger.from_state.state | int }}'
set_stop: '{{ trigger.to_state.state | int }}'
full_excursion: '30' #seconds
travel_pct: >
{% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
{{ trigger.from_state.state | int - trigger.to_state.state | int }}
{% else %}
{{ trigger.to_state.state | int - trigger.from_state.state | int }}
{% endif %}
direction_travel: >
{% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
CLOSE
{% else %}
OPEN
{% endif %}
travel_time: >
{% if (trigger.to_state.state | int - trigger.from_state.state | int < 0) %}
{{ (( trigger.from_state.state | int - trigger.to_state.state | int)/100*30) | round(0) }}
{% else %}
{{ ((trigger.to_state.state | int - trigger.from_state.state | int)/100*30) | round(0) }}
{% endif %}
script:
slider_moved_open:
sequence:
- service: mqtt.publish
data_template:
topic: "setStart"
retain: true
payload: '{{ set_start }}'
- service: mqtt.publish
data_template:
topic: "setStop"
retain: true
payload: '{{ set_stop }}'
- service: mqtt.publish
data_template:
topic: "setPctTravel"
retain: true
payload: '{{ travel_pct }}'
- service: mqtt.publish
data_template:
topic: "setDirection"
retain: true
payload: '{{ direction_travel }}'
- service: mqtt.publish
data_template:
topic: "setTravelTime"
retain: true
payload: '{{ travel_time }}'
- delay: 00:00:04
- service: switch.turn_on
entity_id: switch.open_blackout_blinds
- delay: 00:00:0{{ travel_time }}
#- delay: 00:00:04
- service: switch.turn_off
entity_id: switch.open_blackout_blinds
- delay: 00:00:02
slider_moved_close:
sequence:
- service: mqtt.publish
data_template:
topic: "setStart"
retain: true
payload: '{{ set_start }}'
- service: mqtt.publish
data_template:
topic: "setStop"
retain: true
payload: '{{ set_stop }}'
- service: mqtt.publish
data_template:
topic: "setPctTravel"
retain: true
payload: '{{ travel_pct }}'
- service: mqtt.publish
data_template:
topic: "setDirection"
retain: true
payload: '{{ direction_travel }}'
- service: mqtt.publish
data_template:
topic: "setTravelTime"
retain: true
payload: '{{ travel_time }}'
- delay: 00:00:04
- service: switch.turn_on
entity_id: switch.close_blackout_blinds
#- delay: '00:00:0{{ ((trigger.to_state.state | int - trigger.from_state.state | int)/25) | round(0) }}'
- delay: 00:00:04
- service: switch.turn_off
entity_id: switch.close_blackout_blinds
- delay: 00:00:02
#-------------------------------------------------