I’ve been working on my “Good Night” button and here’s what I’ve come up with. I’m using the custom:button-card for the visual aspect. Here’s my coding for that:
- template: standard-button
type: 'custom:button-card'
aspect_ratio: 1/1.1
double_tap_action:
action: call-service
service: script.good_night_quick
tap_action:
action: call-service
service: script.good_night
name: |
[[[
if (states["timer.gn_01"].state == "active") return "lights off";
if (states["timer.gn_01"].state == "paused") return "lights...";
if (states["timer.gn_02"].state == "active") return "in bed on";
if (states["timer.gn_02"].state == "paused") return "in bed...";
return "Good Night";
]]]
icon: |
[[[
if (states["timer.gn_01"].state == "active") return "mdi:lightbulb-group";
if (states["timer.gn_01"].state == "paused") return "mdi:question";
if (states["timer.gn_02"].state == "active") return "mdi:bed";
if (states["timer.gn_02"].state == "paused") return "mdi:question";
return "mdi:power";
]]]
You’ll notice that both the label and the icon are based on the state of two timers, timer.gn_01 and timer.gn_02. The button simply calls a “fast” script (without the timers) on double_tap and a timed script on single tap. Since the visuals of the button are based on the timers, while they’re running, the button shows what’s going on.
Here are the scripts:
good_night:
alias: "timed shut down of main floor"
sequence:
- service: timer.start
target:
entity_id: timer.gn_01
data:
duration: '30'
good_night_quick:
alias: "quick shut down of main floor"
sequence:
- service: homeassistant.turn_off
target:
entity_id:
- group.downstairs_lights
- fan.lr_sonoff
- service: homeassistant.turn_on
target:
entity_id:
- input_boolean.in_bed
Clearly there’s something else going on with that “good_night” script because all it does is start the timer. The following automations do the rest of the operations:
- alias: 'good night timer 01 finishes'
trigger:
platform: event
event_type: timer.finished
event_data:
entity_id: timer.gn_01
action:
- service: homeassistant.turn_off
target:
entity_id:
- group.downstairs_lights
- fan.lr_sonoff
- service: timer.start
target:
entity_id:
- timer.gn_02
- alias: 'good night timer 02 finishes'
trigger:
platform: event
event_type: timer.finished
event_data:
entity_id: timer.gn_02
action:
- service: homeassistant.turn_on
target:
entity_id:
- input_boolean.in_bed
This all works. The only thing I don’t like about it is that I’m editing up to three separate files (and possibly changing the timer definitions) in order to change the functionality. Can any of you folks more versed in HA figure a way to eliminate the scripts? I haven’t figured out any way to put a sequence into a tap_action if there is one. I also haven’t figured out how I can specify a duration in the start.timer service call in the automation. (I tried using the same syntax as the script and it threw me an error message.)
Alternatively, I do know how to do the whole thing in a script, but I’m not sure how to change the button label and icon according to where execution is in the script.