nixos-configs/configurations/homie/modules/home-assistant/timer/default.nix

881 lines
30 KiB
Nix
Raw Normal View History

# TODO: rewrite this in NetDaemon
# From https://github.com/don86nl/ha_intents/blob/main/config/packages/assist_timers.yaml
{lib, ...}: let
inherit (lib) concatStrings concatStringsSep;
2024-09-17 14:58:21 -04:00
mkTimer = id: {
"assist_timer${toString id}" = {
2024-10-23 14:00:41 -04:00
icon = "mdi:timer-outline";
2024-09-17 14:58:21 -04:00
name = "Assist - Timer ${toString id}";
restore = true;
};
};
mkLocation = id: {
"assist_timer${toString id}_location" = {
2024-10-23 14:00:41 -04:00
icon = "mdi:map-marker";
2024-09-17 14:58:21 -04:00
name = "Assist - Timer ${toString id} Location";
max = 255;
};
};
entityIdList = concatStringsSep " " [
''{{ states.timer ''
''| rejectattr('state','eq','idle')''
''| selectattr('entity_id','match','timer.assist_timer*')''
''| map(attribute='entity_id')''
];
2024-10-04 21:27:54 -04:00
settings = rec {
timer_target = timer_target_default;
timer_target_default = "media_player.music_player_daemon";
2024-09-17 14:58:21 -04:00
timer_tts = true;
timer_tts_service = "tts.speak";
timer_tts_target = "tts.piper";
timer_volume = 0.4;
2024-09-17 14:58:21 -04:00
timer_tts_message = "A set timer has finished.";
timer_media_location = "/path/to/file.mp3";
};
in {
services.home-assistant = {
customSentences."assist_timers" = import ./sentences.nix;
config = {
2024-09-17 14:58:21 -04:00
homeassistant.customize."script.assist_timerstart" = {inherit settings;};
# Make timers
timer = (mkTimer 1) // (mkTimer 2) // (mkTimer 3);
# Makes location of a timer customizable from the UI
input_text = (mkLocation 1) // (mkLocation 2) // (mkLocation 3);
intent_script = {
TimerStart = {
async_action = "false";
action = [
{
service = "script.assist_timerstart";
data.duration = "{{hours | int(default=0)}}:{{ minutes | int(default=0) }}:{{ seconds | int(default=0) }}";
}
];
};
TimerStop = {
async_action = true;
action = [
{
service = "script.assist_timerstop";
data.entity_id = "{{ entity_id }}";
}
];
};
TimerPause = {
async_action = true;
action = [
{
service = "script.assist_timerpause";
data = {
entity_id = "{{ entity_id }}";
timer_action = "{{ timer_action }}";
};
}
];
};
TimerDuration = {
async_action = true;
action = [{stop = "";}];
};
};
2024-09-17 14:58:21 -04:00
# Automate some logic
automation = [
{
2024-09-17 14:58:21 -04:00
alias = "Assist - TimerReached";
description = "Assist automation when set timer time is reached.";
id = "assist_timerreached";
mode = "single";
condition = [
{
alias = "Finished timer is an assist timer";
condition = "template";
value_template = ''{{ trigger.event.data.entity_id[:18] == 'timer.assist_timer' }}'';
}
];
trigger = [
{
alias = "Any timer reached";
id = "timer_finished";
event_type = "timer.finished";
platform = "event";
}
];
variables = {inherit settings;};
action = [
{
alias = "Get generic variables from script";
2024-09-17 14:58:21 -04:00
variables = {
2024-09-17 14:58:21 -04:00
timer_target = concatStrings [
''{%- if settings.get('timer_target')[:13] == "" %}''
''{{- settings.get('timer_target_default') }}''
''{%- elif settings.get('timer_target')[:13] == "media_player." %}''
''{{- settings.get('timer_target') }}''
''{%- elif (${concatStringsSep " " [
''settings.get('timer_target')[:7] == "sensor."''
''or''
''settings.get('timer_target')[:11] == "input_text.")''
''and''
''(states(settings.get('timer_target'))[:13] == "media_player.")''
]}%}''
''{{- states(settings.get('timer_target')) }}''
''{%- elif (${concatStringsSep " " [
''settings.get('timer_target')[:7] == "sensor."''
''or''
''settings.get('timer_target')[:11] == "input_text.")''
''and''
''(states(settings.get('timer_target')) == "")''
]}%}''
''{{- settings.get('timer_target_default') }}''
''{%- else %}''
''{%- set media_player_list = states.media_player | map(attribute='entity_id') | list %}''
''{%- if "sensor." in settings.get('timer_target') or "input_text." in target_area %}''
''{%- set target_area = states(settings.get('timer_target')) %}''
''{%- else %}''
''{%- set target_area = settings.get('timer_target') %}''
''{%- endif %}''
''{%- for entity_id in media_player_list %}''
''{%- if area_name(entity_id) | lower == target_area | lower %}''
''{{ entity_id }}''
''{%- endif %}''
''{%- endfor %}''
''{%- endif %} ''
];
timer_tts = ''{{ settings.get('timer_tts') }}'';
timer_tts_service = ''{{ settings.get('timer_tts_service') }}'';
timer_tts_target = ''{{ settings.get('timer_tts_target') }}'';
timer_volume = ''{{ settings.get('timer_volume') }}'';
timer_tts_message = ''{{ settings.get('timer_tts_message') }}'';
timer_media_location = ''{{ settings.get('timer_media_location') }}'';
};
}
2024-09-17 14:58:21 -04:00
{
alias = "Media file or TTS";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "Media file";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "Timer is a media file";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_tts == false }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Play media";
2024-09-17 14:58:21 -04:00
service = "media_player.play_media";
target.entity_id = ''{{ timer_target }}'';
enabled = true;
data = {
announce = true;
2024-09-17 14:58:21 -04:00
media_content_id = ''{{ timer_media_location }}'';
media_content_type = "music";
};
}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
2024-09-17 14:58:21 -04:00
{delay.seconds = 1;}
{
alias = "Choose TTS service";
choose = [
{
conditions = [
{
alias = "tts.cloud_say";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_tts_service != 'tts.speak' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
2024-09-17 14:58:21 -04:00
service = ''{{ timer_tts_service }}'';
data = {
cache = true;
2024-09-17 14:58:21 -04:00
entity_id = ''{{ timer_target }}'';
message = concatStrings [
''{% if timer_tts_message[:7] == "sensor." or timer_tts_message[:11] == "input_text." %}''
''{{ states(timer_tts_message) }}''
''{% else %}''
''{{ timer_tts_message }}''
''{% endif %}''
];
};
}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
2024-09-17 14:58:21 -04:00
service = "tts.speak";
target.entity_id = ''{{ timer_tts_target }}'';
data = {
cache = true;
2024-09-17 14:58:21 -04:00
media_player_entity_id = ''{{ timer_target }}'';
message = concatStrings [
''{% if timer_tts_message[:7] == "sensor." or timer_tts_message[:11] == "input_text." %}''
''{{ states(timer_tts_message) }}''
''{% else %}''
''{{ timer_tts_message }}''
''{% endif %}''
];
};
}
];
}
];
}
];
}
2024-09-17 14:58:21 -04:00
{
alias = "Assist - TimerFinished";
2024-09-17 14:58:21 -04:00
id = "assist_timerfinished";
description = "Assist automation when set timer time is finished.";
mode = "parallel";
condition = [
{
alias = "Timer was active or paused";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ trigger.from_state != trigger.to_state }}'';
}
];
2024-09-17 14:58:21 -04:00
trigger = [
{
alias = "Assist timer finished or cancelled";
entity_id = ["timer.assist_timer1" "timer.assist_timer2" "timer.assist_timer3"];
platform = "state";
to = "idle";
}
];
2024-09-17 14:58:21 -04:00
action = [
{
2024-09-17 14:58:21 -04:00
alias = "Delay for Timer Reached automation";
delay.seconds = 3;
}
2024-09-17 14:58:21 -04:00
{
2024-09-17 14:58:21 -04:00
alias = "Reset timer location";
service = "input_text.set_value";
target.entity_id = ''{{ 'input_text.' + trigger.entity_id[6:] + '_location' }}'';
data = {
value = "";
};
}
];
2024-09-17 14:58:21 -04:00
}
];
# Scripts to start, pause and stop timers
script = let
entity_id = ''{% if entity_id is set or entity_id != "" %} {{ entity_id }} {% else %} null {% endif %}'';
in {
assist_timerpause = {
alias = "Assist - TimerPause";
description = "Script for pausing a timer using HA Assist.";
icon = "mdi:assistant";
mode = "single";
2024-09-17 14:58:21 -04:00
variables = {
inherit entity_id;
timer_action = ''{% if timer_action is set or timer_action != "" %} {{ timer_action }} {% else %} resume {% endif %}'';
};
sequence = [
{
choose = [
{
conditions = [
{
alias = "Single Timer";
condition = "template";
value_template = ''{{ entity_id[0][:18] == 'timer.assist_timer' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Single timer: Idle or active";
2024-09-17 14:58:21 -04:00
choose = [
{
conditions = [
{
alias = "Timer not active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ states(entity_id) == 'idle' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "Timer is not active";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Pause or resume";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "Pause";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "Action = pause";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_action == 'pause' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Pause timer";
service = "timer.pause";
2024-09-17 14:58:21 -04:00
target.entity_id = ''{{ entity_id }}'';
}
{stop = "Pause timer";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Resume timer";
service = "timer.start";
2024-09-17 14:58:21 -04:00
target.entity_id = ''{{ entity_id }}'';
}
{stop = "Resume timer";}
];
}
];
}
];
}
2024-09-17 14:58:21 -04:00
{
alias = "No specific timer";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "No specific Timer";
condition = "template";
value_template = ''{{ entity_id == 'null' or entity_id | list | length == 0 }}'';
}
2024-09-17 14:58:21 -04:00
{
alias = "Timer(s) are active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length > 0 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "No specific timer: # active?";
2024-09-17 14:58:21 -04:00
choose = [
{
conditions = [
{
alias = "No specific timer asked";
condition = "template";
value_template = ''{{ entity_id == 'null' or entity_id | list | length == 0 }}'';
}
2024-09-17 14:58:21 -04:00
{
alias = "Multiple timers active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length > 1 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "Multiple timers active, none specified";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Pause or resume";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "Pause";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "Action = pause";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_action == 'pause' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Pause timer";
service = "timer.pause";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
{stop = "Pause timer";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Resume timer";
service = "timer.start";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
{stop = "Resume timer";}
];
}
];
}
];
}
2024-09-17 14:58:21 -04:00
{
alias = "All timers";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "All timers";
condition = "template";
value_template = ''{{ entity_id[0] == 'all' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Timers active?";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "No timers active";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "No timers active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length == 0 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "No timers active";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Pause or resume";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "Pause";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "Action = pause";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_action == 'pause' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Pause timer";
service = "timer.pause";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
{stop = "Pause timer";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Resume timer";
service = "timer.start";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
{stop = "Resume timer";}
];
}
];
}
];
}
];
}
];
};
2024-09-17 14:58:21 -04:00
assist_timerstart = {
alias = "Assist - TimerStart";
description = "Script for starting a timer using HA Assist.";
icon = "mdi:assistant";
mode = "single";
2024-09-17 14:58:21 -04:00
variables = {inherit settings;};
sequence = [
{
alias = "Set variables";
2024-09-17 14:58:21 -04:00
variables = {
timer_location = concatStrings [
''{%- if settings.get('timer_target')[:13] == "media_player." %}''
''{{ area_name(settings.get('timer_target')) | lower }}''
''{% elif (settings.get('timer_target')[:7] == "sensor." or settings.get('timer_target')[:11] == "input_text.") and states(settings.get('timer_target'))[:13] == "media_player." %}''
''{{- states(settings.get('timer_target')) }}''
''{%- elif settings.get('timer_target')[:13] != "media_player." and settings.get('timer_target')[:7] != "sensor." and settings.get('timer_target')[:11] != "input_text." %}''
''{{- settings.get('timer_target') }}''
''{%- elif (settings.get('timer_target')[:7] == "sensor." or settings.get('timer_target')[:11] == "input_text.") and (states(settings.get('timer_target')) != "") and (states(settings.get('timer_target'))[:13] == "media_player.") %}''
''{{ area_name(settings.get('timer_target_default')) }} ''
''{%- elif (settings.get('timer_target')[:7] == "sensor." or settings.get('timer_target')[:11] == "input_text.") %}''
''{% if states(settings.get('timer_target')) != "" and states(settings.get('timer_target')) != "not_home" and states(settings.get('timer_target')) != 0 %}''
''{{ states(settings.get('timer_target')) }}''
''{% else %}''
''{{- area_name(settings.get('timer_target_default')) | lower }}''
''{%- endif %}''
''{%- else %}''
''{{- area_name(settings.get('timer_target')) | lower }}''
''{%- endif %}''
];
};
}
2024-09-17 14:58:21 -04:00
{
alias = "Set timer location";
service = "input_text.set_value";
2024-09-17 14:58:21 -04:00
target.entity_id = concatStrings [
''{% if states('timer.assist_timer1') != 'active' and states('timer.assist_timer1') != 'paused' %}''
''input_text.assist_timer1_location''
''{% elif states('timer.assist_timer2') != 'active' and states('timer.assist_timer2') != 'paused' %}''
''input_text.assist_timer2_location''
''{% else %}''
''input_text.assist_timer3_location''
''{% endif%}''
];
data = {
value = ''{{ timer_location }}'';
};
}
2024-09-17 14:58:21 -04:00
{
alias = "Start timer";
service = "timer.start";
2024-09-17 14:58:21 -04:00
target.entity_id = concatStrings [
''{% if states('timer.assist_timer1') != 'active' and states('timer.assist_timer1') != 'paused' %}''
''timer.assist_timer1''
''{% elif states('timer.assist_timer2') != 'active' and states('timer.assist_timer2') != 'paused' %}''
''timer.assist_timer2''
''{% else %}''
''timer.assist_timer3''
''{% endif%}''
];
data_template = {
duration = ''{{ duration }}'';
};
}
];
};
2024-09-17 14:58:21 -04:00
assist_timerstop = {
alias = "Assist - TimerStop";
description = "Script for stopping a timer using HA Assist.";
icon = "mdi:assistant";
mode = "single";
2024-09-17 14:58:21 -04:00
variables = {inherit entity_id;};
sequence = [
{
alias = "Set variables";
2024-09-17 14:58:21 -04:00
variables = {inherit entity_id;};
}
2024-09-17 14:58:21 -04:00
{
choose = [
{
alias = "Stop Timer music";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "Timer is a media file";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ timer_tts == false }}'';
}
2024-09-17 14:58:21 -04:00
{
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''
{% set mediaplayer = namespace(entity=[]) %}
{% for player in states.media_player %}
{%- if ((state_attr(player.entity_id, 'media_content_id') |lower != 'none' and state_attr(player.entity_id, 'media_content_id')[:47][38:] == 'timer.mp3') or state_attr(player.entity_id, 'media_title') | lower == 'timer') and states(player.entity_id) == 'playing' -%}
{%- set mediaplayer.entity = player.entity_id -%}
{% endif -%}
{% endfor %}
{{ mediaplayer.entity[:12] == 'media_player' }}
'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Stop timer music";
service = "media_player.media_stop";
2024-09-17 14:58:21 -04:00
target.entity_id = ''
{% set mediaplayer = namespace(entity=[]) %}
{% for player in states.media_player %}
{% if ((state_attr(player.entity_id, 'media_content_id') |lower != 'none' and state_attr(player.entity_id, 'media_content_id')[:47][38:] == 'timer.mp3') or state_attr(player.entity_id, 'media_title') | lower == 'timer') and states(player.entity_id) == 'playing' %}
{% set mediaplayer.entity = player.entity_id %}
{% endif %}
{% endfor %} {{ mediaplayer.entity }}'';
}
];
}
2024-09-17 14:58:21 -04:00
{
conditions = [
{
alias = "Single Timer";
condition = "template";
value_template = ''{{ entity_id[0][:18] == 'timer.assist_timer' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Single timer: Idle or active";
2024-09-17 14:58:21 -04:00
choose = [
{
conditions = [
{
alias = "Timer not active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''{{ states(entity_id) == 'idle' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "Timer is not active";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Reset timer location value";
service = "input_text.set_value";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join('_location, ') | replace('timer.', 'input_text.') }}'';
data = {
value = "0";
};
}
{
alias = "Cancel single timer";
service = "timer.cancel";
target.entity_id = ''{{ entity_id }}'';
}
{stop = "Timer cancelled";}
];
}
];
}
2024-09-17 14:58:21 -04:00
{
alias = "No specific timer";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "No specific Timer";
condition = "template";
value_template = ''{{ entity_id == 'null' or entity_id | list | length == 0 }}'';
}
2024-09-17 14:58:21 -04:00
{
alias = "Timer(s) are active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length > 0 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "No specific timer: # active?";
2024-09-17 14:58:21 -04:00
choose = [
{
conditions = [
{
alias = "No specific timer asked";
condition = "template";
value_template = ''{{ entity_id == 'null' or entity_id | list | length == 0 }}'';
}
2024-09-17 14:58:21 -04:00
{
alias = "Multiple timers active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length > 1 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "Multiple timers active, none specified";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Cancel single timer";
service = "timer.cancel";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
2024-09-17 14:58:21 -04:00
{
alias = "Reset timer location value";
service = "input_text.set_value";
2024-09-17 14:58:21 -04:00
metadata = {};
target.entity_id = ''${entityIdList} | join('_location, ') | replace('timer.', 'input_text.') }}'';
data = {value = "0";};
}
2024-09-17 14:58:21 -04:00
{stop = "Timer cancelled";}
];
}
];
}
2024-09-17 14:58:21 -04:00
{
alias = "All timers";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "All timers";
condition = "template";
value_template = ''{{ entity_id[0] == 'all' }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{
alias = "Timers active?";
2024-09-17 14:58:21 -04:00
choose = [
{
alias = "No timers active";
2024-09-17 14:58:21 -04:00
conditions = [
{
alias = "No timers active";
condition = "template";
2024-09-17 14:58:21 -04:00
value_template = ''${entityIdList} | list | length == 0 }}'';
}
];
2024-09-17 14:58:21 -04:00
sequence = [
{stop = "No timers active";}
];
}
];
2024-09-17 14:58:21 -04:00
default = [
{
alias = "Cancel all timers";
service = "timer.cancel";
2024-09-17 14:58:21 -04:00
target.entity_id = ''${entityIdList} | join(', ') }}'';
}
2024-09-17 14:58:21 -04:00
{stop = "Cancel all timers";}
];
}
];
}
];
}
];
};
};
};
};
}