dannytsang / homeassistant-config

🏡 My Home Assistant Configs.
https://dannytsang.com
31 stars 0 forks source link

Create open/closed sensor for curtains/blinds #59

Open dannytsang opened 1 year ago

dannytsang commented 1 year ago

There are multiple conditions to when a window covering should open or close. They exist as separate automations which has multiple issues:

  1. Automatons ignoring conditions that was previously open/closed.
  2. Overlapping triggers causing opposite states
  3. Debugging why it open or closed means looking at multiple automations.

Background The problem was very obvious when #48 was introduced and people in the house were confused why the blind/curtain was open or closed. Before, it used to be based on time and window state.

dannytsang commented 1 year ago

Using a template sensor to achieve this. Most flexibility for conditions and visibility of each condition. The down side is any changes require a restart to take effect.

Example:

  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.bedroom_tv_powered_on
          - binary_sensor.bedroom_window
          - input_number.close_blinds_brightness_threshold
          - input_number.forecast_high_temperature
          - sensor.bedroom_tv_power_meter
          - sensor.front_garden_motion_light_level
          - sun.sun
          - weather.home_hourly
    binary_sensor:
      - name: Bedroom Blind Anticipated State
        unique_id: 978fb6f0-42db-4c71-ab13-9037e82da73a
        device_class: opening
        state: >-
          {{ not(states('sensor.front_garden_motion_light_level')|float(0) < states('input_number.close_blinds_brightness_threshold')|float(10000)
          and not(states('sun.sun') == 'above_horizon')
          and states('binary_sensor.bedroom_window') == 'off')
          and state_attr('weather.home_hourly', 'temperature')|float(0) > states('input_number.forecast_high_temperature')|float(24)
          and states('binary_sensor.bedroom_tv_powered_on') == 'on'
          and not(states('weather.home_hourly') in ['sunny','partlycloudy']) }}
        attributes:
          brightness: "{{ (states('sensor.front_garden_motion_light_level')|float(0)) < (states('input_number.close_blinds_brightness_threshold')|float(10000)) }}"
          sun: "{{ states('sun.sun') == 'above_horizon' }}"
          temperature_hour_forecast: "{{ states('weather.home_hourly')|float(0) > states('input_number.forecast_high_temperature')|float(24) }}"
          tv_running: "{{ states('binary_sensor.bedroom_tv_powered_on')|default('off') == 'on' }}"
          weather: "{{ states('home_hourly')|default('') in ['sunny','partlycloudy'] }}"
          window: "{{ states('binary_sensor.bedroom_window') == 'off' }}"