Files
esphome-config/shelly1_02.yaml
T
2022-05-27 18:50:07 -07:00

224 lines
6.2 KiB
YAML

---
# Shelly 2 Power Module
# Location: Upstairs bathroom
#
substitutions:
project: Upstairs bathroom exhaust
id: switchexhaust
hostname: switch-exhaust
default_humidity_delta_threshold: "1.0" # Difference between house humidity and bathroom humidity.
timeout_duration: "600" # 600 seconds = 10 minutes
<<: !include common/substitutions/gpio/shelly1.yaml
<<: !include common/esphome/esp8266.yaml
<<: !include common/common.yaml
script:
- id: timeout_restart
then:
#- if:
# condition:
# - lambda: return id(remaining_time) < $timeout_duration;
# then:
- globals.set:
id: remaining_time
value: !lambda return $timeout_duration;
- sensor.template.publish:
id: timeout
state: !lambda return id(remaining_time);
binary_sensor:
# Physical Toggle Switch. Monitor for switch position changes internally. Do not expose to Home Assistant.
- platform: gpio
pin:
number: $button_gpio
id: button
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_state:
- switch.toggle: relay
- if:
condition:
- lambda: return id(diagnostic_last_trigger).state != "PHYSICAL";
then:
- text_sensor.template.publish:
id: diagnostic_last_trigger
state: PHYSICAL
# Use motion detection to keep fan running.
- platform: homeassistant
id: motion
entity_id: binary_sensor.upstairs_bathroom_presence
globals:
- id: remaining_time
type: int
restore_value: no
initial_value: "0"
# - id: delta_trigger
# type: float
# restore_value: no
# initial_value: $default_humidity_delta_threshold
# Update the timeout timers every 5 seconds.
interval:
- interval: 5s
then:
#- lambda: ESP_LOGD("interval", "diff %.1f > trigger %.1f ", id(humidity_delta).state, id(delta_trigger).state);
- if: # Humidity threshold trigger.
condition:
and:
# If the difference is greater than the trigger.
- lambda: return id(humidity_delta).state > id(delta_trigger).state;
# and the fan is off.
- switch.is_off: relay
then:
- switch.turn_on: relay
- if:
condition:
- lambda: return id(diagnostic_last_trigger).state != "THRESHOLD";
then:
- text_sensor.template.publish:
id: diagnostic_last_trigger
state: THRESHOLD
- if: # Restart the timeout if motion is active.
condition:
- binary_sensor.is_on: motion
then:
- script.execute: timeout_restart
- if: # Decrement the timeout.
condition:
and:
- lambda: return id(remaining_time) > 0;
- lambda: return id(humidity_delta).state <= id(delta_trigger).state;
- switch.is_on: relay
then:
- globals.set:
id: remaining_time
value: !lambda return id(remaining_time) -= 5;
- sensor.template.publish:
id: timeout
state: !lambda return id(remaining_time);
# Time to turn off the relay.
else:
- if:
condition:
- switch.is_on: relay
then:
- if:
condition:
# If the difference is greater than the trigger.
- lambda: return id(humidity_delta).state > id(delta_trigger).state;
then:
- script.execute: timeout_restart
else:
# Only turn the fan off if the humidity difference is below the trigger.
- switch.turn_off: relay
number:
- platform: template
name: "$project humidity delta trigger"
#id: delta_trigger_ui
id: delta_trigger
icon: "mdi:rocket-launch"
entity_category: config
unit_of_measurement: "%"
optimistic: true
initial_value: $default_humidity_delta_threshold
restore_value: true
min_value: 0.5
max_value: 15.0
step: 0.5
# set_action:
# then:
# - globals.set:
# id: delta_trigger
# value: !lambda return id(delta_trigger_ui).state;
sensor:
- platform: template
id: timeout
name: "$project timeout"
accuracy_decimals: 0
icon: "mdi:sort-clock-ascending"
entity_category: diagnostic
unit_of_measurement: "seconds"
- platform: homeassistant
id: humidity_24h
internal: true
accuracy_decimals: 1
entity_id: sensor.upstairs_bathroom_humidity_average_last_24_hours
unit_of_measurement: "%"
- platform: homeassistant
id: humidity_now
internal: true
accuracy_decimals: 1
entity_id: sensor.upstairs_bathroom_humidity
unit_of_measurement: "%"
on_value:
then:
- sensor.template.publish:
id: humidity_delta
state: !lambda return id(humidity_now).state - id(humidity_24h).state;
- platform: template
id: humidity_delta
name: "$project humidity delta"
accuracy_decimals: 1
icon: "mdi:delta"
entity_category: diagnostic
unit_of_measurement: "%"
switch:
- platform: gpio
id: relay
pin: $relay_gpio
on_turn_on:
# Start the timeout timer.
- script.execute: timeout_restart
on_turn_off:
- sensor.template.publish:
id: timeout
state: 0
- platform: template
id: virtual_switch
name: $project
optimistic: no
lambda: |-
return (id(relay).state);
turn_on_action:
- switch.turn_on: relay
- if:
condition:
- lambda: return id(diagnostic_last_trigger).state != "VIRTUAL";
then:
- text_sensor.template.publish:
id: diagnostic_last_trigger
state: VIRTUAL
turn_off_action:
- switch.turn_off: relay
text_sensor:
# Trigger states: PHYSICAL, VIRTUAL (UI or Alexa/Google), THRESHOLD
- platform: template
id: diagnostic_last_trigger
name: "$project last trigger"
disabled_by_default: true
entity_category: diagnostic