2025/10/26

욕실 전등, 환기팬 자동화2


가장 의도치 않은 상황이 많이 발생해 예외조건을 많이 추가해야 되는 욕실 자동화.
트리거는 도어센서, 모션센서, 존재감지센서고 트리거 후 예외조건을 덕지덕지 붙여 액션으로 가게 되면 주/야간에 따라 주조명등 또는 다운라이트만 켜는 자동화.

예외조건은 모션센서와 존재감지센서에서 고스트현상이 꽤 자주 발생해 모션센서가 트리거일 때는 존재감지센서 에너지 값을 확인하게 하고 존재감지센서가 트리거일 때는 모션센서의 last_seen값을 참고해 10분내에 감지된 이력이 있을때만 조건을 패스.
욕실등이 켜져있거나 HA재시작후 2분이 지난 후부터 작동하게 하는 것이 1,2번째 조건.

욕실on 자동화 시작시 환풍기 켜지고 이전 환풍기 타이머 시간이 남아있다면 리셋. 욕실off 자동화시 환풍기off 5분 타이머 시작하고 수동으로 전등을 켤때는 전등 트리거에 전등on과 환풍기 타이머 리셋을 같이 넣어 전등이 켜져있는 상태에서 환풍기가 꺼지는 걸 방지.


** light.bathroom은 light도메인에서 주조명과 다운라이트를 묶은 그룹조명.
** light.bathroom_button1은 주조명이고 switch.bathroom_button2은 환풍기 스위치.
** 욕실 전등, 환기팬 자동화1: https://ntxlds.blogspot.com/2023/10/blog-post.html

automation.yaml
- id: 움직임-욕실on
  alias: 움직임-욕실on
  mode: restart
  triggers:
    - trigger: state
      entity_id: binary_sensor.ba_door_contact
      from: 'off'
      to: 'on'
    - trigger: state
      entity_id: binary_sensor.ba_motion_occupancy
      from: 'off'
      to: 'on'
    - trigger: numeric_state
      entity_id: sensor.eld24_ba_still_energy
      above: 50
  conditions:
    - condition: state
      entity_id: light.bathroom
      state: 'off'
    - condition: numeric_state
      entity_id: sensor.core_uptime
      above: 120
    - condition: template
      value_template: >
        {% if trigger.entity_id == 'binary_sensor.ba_motion_occupancy' %}
          {{ states('sensor.eld24_ba_still_energy') | float < 20 }}
        {% else %}
          true
        {% endif %}
    - condition: template
      value_template: >
        {% if trigger.entity_id == 'sensor.eld24_ba_still_energy' %}
        {{ (now() - (states('sensor.ba_motion_last_seen') | as_datetime)).total_seconds() < 600 }}
        {% else %}
          true
        {% endif %}
  actions:
    - choose:
        - conditions:
            - condition: time
              after: '22:00'
              before: '07:00'
          sequence:
            - action: homeassistant.turn_on
              entity_id: 
                - light.bathroom_downlight
                  switch.bathroom_button2
            - action: timer.cancel
              entity_id: timer.bathroom_button2
      default:
        - action: homeassistant.turn_on
          entity_id:
            - light.bathroom_button1
              switch.bathroom_button2
        - action: timer.cancel
          entity_id: timer.bathroom_button2

- id: 움직임-욕실off
  alias: 움직임-욕실off
  triggers:
    - trigger: numeric_state
      entity_id: sensor.eld24_ba_still_energy
      below: 10
      for: '00:00:10'
    - trigger: state
      entity_id: binary_sensor.ba_motion_occupancy
      from: 'on'
      to: 'off'
  conditions:
    - condition: state
      entity_id: light.bathroom
      state: 'on'
    - condition: numeric_state
      entity_id: sensor.eld24_ba_still_energy
      below: 10
  actions:
    - action: light.turn_off
      entity_id: light.bathroom
    - action: timer.start
      entity_id: timer.bathroom_button2