-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdevice_control_timer_full_llm.yaml
More file actions
192 lines (184 loc) · 7.18 KB
/
device_control_timer_full_llm.yaml
File metadata and controls
192 lines (184 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
blueprint:
name: Set Device Timer
author: luuquangvu
source_url: https://github.com/luuquangvu/tutorials/blob/main/device_control_timer_full_llm.yaml
description: |-
# Tool for controlling on/off devices with adjustable delay timing used for Voice Assistant
## Blueprint Setup
### Required
- The `device_control_tool.yaml` blueprint needs to be installed.
- The mentioned file(s) is/are included in the repository.
- A template sensor stores all information about entity aliases needs to be configured in `config/configuration.yaml`; The sensor is required for friendly-name lookup.
```
# File configuration.yaml
shell_command:
get_entity_alias: jq '[.data.entities[] | select(.options.conversation.should_expose == true and (.aliases | length > 0)) | {entity_id, aliases}]' ./.storage/core.entity_registry
template:
- triggers:
- trigger: homeassistant
event: start
- trigger: event
event_type: event_template_reloaded
actions:
- action: shell_command.get_entity_alias
response_variable: response
sensor:
- name: "Assist: Entity IDs and Aliases"
unique_id: entity_ids_and_aliases
icon: mdi:format-list-bulleted
device_class: timestamp
state: "{{ now().isoformat() }}"
attributes:
entities: "{{ response.stdout }}"
```
### Optional
- Adjust the prompts for each field used in the script. The descriptions guide the LLM to provide the correct input.
### Note
- Provide a concise and precise description for the script. This will be utilized by the LLM to understand it should use this script for controlling devices on/off with a specified delay.
- Make sure to expose the entities you want to control to Assist.
- Make sure to expose the script to Assist after the script has been saved.
- Do not alter the default script name.
- Once the script is created, click the three dots in the top right corner, choose "Edit in YAML," and remove the `description: ...` line to restore the default. This step is important because it helps the LLM better understand the script's purpose.
domain: script
homeassistant:
min_version: 2024.12.0
input:
entity_aliases_settings:
name: Settings for Entity Aliases
icon: mdi:format-list-bulleted
description: You can use these settings to configure a template sensor that stores all information about entity aliases.
input:
entity_aliases:
name: Entity Aliases
selector:
entity:
filter:
- domain: sensor
integration: template
controller_settings:
name: Settings for Controller
icon: mdi:controller
description: You can use these settings to select the Device Timer.
input:
controller:
name: Controller
selector:
entity:
filter:
- domain: script
prompt_settings:
name: Prompt settings for the LLM
icon: mdi:robot
description: You can use these settings to finetune the prompts for your specific LLM (model). In most cases the defaults should be fine.
collapsed: true
input:
entities_prompt:
name: Entities Prompt
description: The names of the devices to be controlled, provided by the AI
selector:
text:
multiline: true
default: |-
Device names. Semicolon-separated
control_prompt:
name: Control Prompt
description: The desired state (ON or OFF) for the devices, provided by the AI
selector:
text:
multiline: true
default: |-
Action true (ON), false (OFF). No guessing
timer_prompt:
name: Timer Prompt
description: The delay duration (HH:MM:SS) before the action is executed, provided by the AI
selector:
text:
multiline: true
default: |-
Delay as HH:MM:SS. Calculate from now if target specified
mode: parallel
max_exceeded: silent
description: Turns devices on or off after a specified delay
variables:
version: 20260409
fields:
entities:
name: Entities
description: !input entities_prompt
selector:
text:
required: true
control:
name: Control
description: !input control_prompt
selector:
boolean:
required: true
timer:
name: Timer
description: !input timer_prompt
selector:
time:
required: true
sequence:
- variables:
entity_aliases: !input entity_aliases
entities: "{{ entities | default }}"
control: "{{ control | default(false) }}"
timer: "{{ timer | default('00:00:00') | as_timedelta | default('00:00:00', true) }}"
- alias: Check if variables were set correctly
if:
- condition: template
value_template: >-
{% set validation = namespace(not_exist=false) -%}
{% for entity in entities.split(';') -%}
{% if not ((states | selectattr('attributes.friendly_name', 'eq', entity.strip()) | list) or
(state_attr(entity_aliases, 'entities') | default([]) | selectattr('aliases', 'contains', entity.strip()) | list)) -%}
{% set validation.not_exist = true -%}
{% endif -%}
{% endfor -%}
{{ validation.not_exist }}
then:
- alias: Set variable for error message
variables:
response:
error: Unable to control the device because the device name is invalid.
- alias: Stop the script
stop: Unable to control the device because the device name is invalid.
response_variable: response
- variables:
devices: >-
{% set device = namespace(entities=[]) -%}
{% for entity in entities.split(';') -%}
{% if (states | selectattr('attributes.friendly_name', 'eq', entity.strip()) | list) -%}
{% set device.entities = device.entities + (states | selectattr('attributes.friendly_name', 'eq', entity.strip()) | map(attribute='entity_id') | list) -%}
{% else -%}
{% set device.entities = device.entities + (state_attr(entity_aliases, 'entities') | default([]) | selectattr('aliases', 'contains', entity.strip()) | map(attribute='entity_id') | list) -%}
{% endif -%}
{% endfor -%}
{{ device.entities }}
- variables:
friendly_entities: >-
{% set names = namespace(values=[]) -%}
{% for name in entities.split(';') -%}
{% if name | trim -%}
{% set names.values = names.values + [name.strip()] -%}
{% endif -%}
{% endfor -%}
{{ names.values }}
- action: script.turn_on
target:
entity_id: !input controller
data:
variables:
devices: "{{ devices | join(', ') }}"
control: "{{ control }}"
timer: "{{ timer }}"
- alias: Prepare success response for Assist
variables:
response:
success: >-
Scheduled the {{ 'on' if control else 'off' }} action for {{
friendly_entities | join(', ') }} after {{ timer }}.
- stop: Finish and return response data
response_variable: response