Thought I’d post my final scripts here. Not the prettiest, but they work and accomplish everything in all scenarios that I could think of.
Idea: Control multiple bathroom fans, to all turn on/off at set times. Monitor the switch presses for on/off and depending when in ventilation or outside of ventilation timing act accordingly.
-
If Ventilation OFF
– Turns fan on, with 30 minute delay, then turns off. When pushed while already on restarts so delay another 30 minutes before turning off
-
If Ventilation ON
– switch OFF – delay 10 seconds turn back on
– switch ON – checks against a input.boolean to see if fan is in last 25 minutes of ventilation. If not, button push is ignored. If it is, the individual fan status is set to ON and delay of 30 minutes before turning off set. Additional presses of on restart delay
When ventilation turning off, it checks each individual fan to see if the status is ON. If they are on it doesn’t turn them off. If fan status is OFF it turns them off at end of ventilation.
Original automation to turn fans on/off during ventilation times:
- id: '1630215220100'
alias: 001 - 1 BATH FANS ON/OFF FOR VENTILATION
description: Run bathroom fans based on start and stop timing - includes coding
to check if action missed due to HA restarting
trigger:
- platform: time
at:
- 07:00:00
- '10:00:00'
- '14:00:00'
- '17:00:00'
- '21:00:00'
- '23:59:00'
- platform: homeassistant
event: start
condition: []
action:
- variables:
hmin: '{{ now().strftime(''%H:%M'') }}'
- choose:
- conditions:
- condition: template
value_template: '{{ ''07:00'' <= hmin < ''10:00'' or ''14:00'' <= hmin < ''17:00''
or ''21:00'' <= hmin < ''23:59'' }}'
- condition: template
value_template: '{{ is_state(''group.bathroom_switch'', ''off'') }}'
sequence:
- service: homeassistant.turn_on
target:
entity_id: group.bathroom_switch
- service: input_boolean.turn_on
target:
entity_id: input_boolean.001_01_ventilation_status_bath_fans
- service: input_boolean.turn_off
target:
entity_id:
- input_boolean.main_bath_fan_status
- input_boolean.ensuite_bath_fan_status
- input_boolean.upstairs_bath_fan_status
- input_boolean.tenant_bath_fan_status
default:
- service: input_boolean.turn_off
target:
entity_id:
- input_boolean.001_01_ventilation_status_bath_fans
- input_boolean.001_01_ventilation_status_bath_fans_under_30_min
- service: script.ensuite_bathroom_fan_check_status_on_turn_off
- service: script.main_bathroom_fan_check_status_on_turn_off
- service: script.upstairs_bathroom_fan_check_status_on_turn_off
- service: script.tenant_bathroom_fan_check_status_on_turn_off
mode: single
input.boolean is set 25 minutes prior to ventilation off for use elsewhere:
- id: '1630224742314'
alias: 001 - 1 BATH FANS VENTILATION Final 25 Minutes
description: Sets input.boolean for other automations to check if in final 25 minutes
of ventilation run.
trigger:
- platform: time
at:
- 09:35:00
- '16:35:00'
- '23:35:00'
condition: []
action:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.001_01_ventilation_status_bath_fans_under_30_min
mode: single
Each fan has it switch push OFF automation:
- id: '1630222032781'
alias: 001 - 1c Upstairs Bath Fan check if Switched OFF during Ventilation
description: Detects when switch for bathroom fan turned off, then checks if the
bath fan is supposed to be on ventilation mode
trigger:
- platform: device
type: turned_off
device_id: b800352ff2c40b63df11c132882fa876
entity_id: switch.scene_capable_switch_on_off_4
domain: switch
condition: []
action:
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- choose:
- conditions:
- condition: state
entity_id: input_boolean.001_01_ventilation_status_bath_fans
state: 'on'
sequence:
- service: homeassistant.turn_on
target:
entity_id: switch.scene_capable_switch_on_off_4
default:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.upstairs_bath_fan_status
mode: single
Each fan has its switch push ON automation:
- id: '1630224708508'
alias: 001 - 1c Upstairs Bath Fan check Switch ON
description: Detects when switch for bathroom fan turned ON
trigger:
- platform: device
device_id: 31dd04856b40eced6b6b56888fcff826
domain: zwave_js
type: zwave_js.value_updated.value
command_class: 37
to: 'on'
property: currentValue
from: ''
endpoint: ''
property_key: ''
condition: []
action:
- choose:
- conditions:
- condition: state
entity_id: input_boolean.001_01_ventilation_status_bath_fans
state: 'off'
sequence:
- service: script.upstairs_bathroom_fan_delay
- conditions:
- condition: state
entity_id: input_boolean.001_01_ventilation_status_bath_fans_under_30_min
state: 'on'
sequence:
- service: script.upstairs_bathroom_fan_delay
default: []
mode: restart
Each switch as a scripted called from switch OFF automation:
alias: 001 - 1c Upstairs Bath Fan Ventilation OFF check
sequence:
- condition: state
entity_id: input_boolean.upstairs_bath_fan_status
state: 'off'
- type: turn_off
device_id: 31dd04856b40eced6b6b56888fcff826
entity_id: switch.scene_capable_switch_on_off_4
domain: switch
mode: single
Each switch has a script called when it is turned on to do the delay:
alias: 001 - 1c Upstairs Bath Fan Delay on ON
sequence:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.upstairs_bath_fan_status
- delay:
hours: 0
minutes: 30
seconds: 0
milliseconds: 0
- type: turn_off
device_id: 31dd04856b40eced6b6b56888fcff826
entity_id: switch.scene_capable_switch_on_off_4
domain: switch
- service: input_boolean.turn_off
target:
entity_id: input_boolean.upstairs_bath_fan_status
mode: restart
This is all done using Leviton DS15Z on/off switches. They do not have multiple button press actions but by using conditions and input.booleans set I was able to make them act like it did. It rethinking the logic and conditions it would be possible using this to actually turn these switches into multiple action based on amount of times the switch is hit on to trigger difference scenarios. ie if you walked into the room, you could have different items turned on depending on how many button presses. Just would take more effort then having the state change available like certain other switches with that capability have built in!