I’m trying to create various automations to enhance the functionality of my assistant but sometimes it doesn’t understand it 100% correctly (or it might prepend the wake word) and usually the LLM figures it out but in this case with the variables it doesn’t work
alias: Voice Command - Temporary Device Control
description: Handles voice commands like 'turn on X for Y seconds'
variables:
operation: "{% if \"turn on\" in trigger.sentence|lower %}on{% else %}off{% endif %}"
device_name: "{{ trigger.slots.device | lower }}"
duration_seconds: |-
{% if trigger.slots.duration %}
{% if "minute" in trigger.sentence %}
{{ (trigger.slots.duration | int) * 60 }}
{% elif "hour" in trigger.sentence %}
{{ (trigger.slots.duration | int) * 3600 }}
{% else %}
{{ trigger.slots.duration | int }}
{% endif %}
{% else %}
{{ (trigger.slots.duration_hours | default(0) | int) * 3600 +
(trigger.slots.duration_minutes | default(0) | int) * 60 +
(trigger.slots.duration_seconds | default(0) | int) }}
{% endif %}
mode: parallel
triggers:
- command: turn on {device} for {duration} seconds
trigger: conversation
- command: turn off {device} for {duration} seconds
trigger: conversation
- command: turn on {device} for {duration} minutes
trigger: conversation
- command: turn off {device} for {duration} minutes
trigger: conversation
- command: turn on {device} for {duration} hours
trigger: conversation
- command: turn off {device} for {duration} hours
trigger: conversation
- command: turn on {device} for {duration_hours} hours {duration_minutes} minutes
trigger: conversation
- command: turn off {device} for {duration_hours} hours {duration_minutes} minutes
trigger: conversation
- command: turn on {device} for {duration_minutes} minutes {duration_seconds} seconds
trigger: conversation
- command: >-
turn off {device} for {duration_minutes} minutes {duration_seconds}
seconds
trigger: conversation
conditions:
- condition: template
value_template: "{{ duration_seconds > 0 }}"
actions:
- variables:
allowed_domains: "[\"light\", \"switch\", \"fan\", \"media_player\", \"cover\", \"climate\"]"
excluded_entities: >-
["light.cob_strip_light_segment_001",
"light.cob_strip_light_segment_002",
"light.cob_strip_light_segment_003", "light.cob_strip_light_segment_004",
"light.cob_strip_light_segment_005", "light.cob_strip_light_segment_006",
"light.cob_strip_light_segment_007", "light.cob_strip_light_segment_008",
"light.cob_strip_light_segment_009", "light.cob_strip_light_segment_010",
"light.cob_strip_light_segment_011", "light.cob_strip_light_segment_012",
"light.cob_strip_light_segment_013", "light.cob_strip_light_segment_014",
"light.cob_strip_light_segment_015", "light.cob_strip_light_ble",
"sensor.cob_strip_light_status", "button.cob_strip_light_request_platform_api_state",
"switch.cob_strip_light_power_switch", "switch.cob_strip_light_gradient_toggle",
"switch.cob_strip_light_dream_view_toggle"]
entities: >-
{% set found_entities = namespace() %} {% set found_entities.list = []
%} {% set exact_match = namespace() %} {% set exact_match.entity = None
%} {% for entity_id in states | map(attribute='entity_id') %}
{% set domain = entity_id.split('.')[0] %}
{% if domain in allowed_domains | from_json and entity_id not in excluded_entities | from_json %}
{% set entity_name = state_attr(entity_id, 'friendly_name') | lower | default(entity_id.split('.')[-1].replace('_', ' '), true) %}
{% if device_name == entity_name %}
{% set exact_match.entity = entity_id %}
{% break %}
{% elif device_name in entity_name %}
{% set found_entities.list = found_entities.list + [entity_id] %}
{% endif %}
{% endif %}
{% endfor %} {% if exact_match.entity is not none %}
["{{ exact_match.entity }}"]
{% else %}
{{ found_entities.list | tojson }}
{% endif %}
- choose:
- conditions:
- condition: template
value_template: |-
{% if entities is string %}
{{ (entities | from_json) | count == 0 }}
{% else %}
{{ entities | count == 0 }}
{% endif %}
sequence:
- data:
message: Sorry, I couldn't find a device called {{ device_name }}.
entity_id: assist_satellite.esp32_s3_box_3_assist_satellite
action: assist_satellite.announce
- conditions:
- condition: template
value_template: |-
{% if entities is string %}
{{ (entities | from_json) | count == 1 }}
{% else %}
{{ entities | count == 1 }}
{% endif %}
sequence:
- parallel:
- data:
message: >-
Turning {{ operation }} {{ device_name }} for {% if
duration_seconds // 3600 > 0 %}{{ (duration_seconds // 3600)
| int }}{% if duration_seconds // 3600 > 0 %} hours{% endif
%}{% endif %}{% if (duration_seconds % 3600) // 60 > 0 %}{%
if duration_seconds // 3600 > 0 %} and {% endif %}{{
((duration_seconds % 3600) // 60) | int }} minutes{% endif
%}{% if duration_seconds % 60 > 0 %}{% if (duration_seconds
% 3600) // 60 > 0 or duration_seconds // 3600 > 0 %} and {%
endif %}{{ (duration_seconds % 60) | int }} seconds{% endif
%}
entity_id: assist_satellite.esp32_s3_box_3_assist_satellite
action: assist_satellite.announce
- data:
device_entity: |-
{% if entities is string %}
{{ (entities | from_json)[0] }}
{% else %}
{{ entities[0] }}
{% endif %}
operation: "{{ operation }}"
duration: "{{ duration_seconds }}"
action: script.temporary_device_control
- conditions:
- condition: template
value_template: |-
{% if entities is string %}
{{ (entities | from_json) | count > 1 }}
{% else %}
{{ entities | count > 1 }}
{% endif %}
sequence:
- data:
message: >-
I found multiple devices matching {{ device_name }}. Please be
more specific. Options are: {% if entities is string %}{% for
entity_id in entities | from_json %}{{ state_attr(entity_id,
'friendly_name') | default(entity_id.split('.')[-1].replace('_',
' '), true) }}{% if not loop.last %}, {% endif %}{% endfor %}{%
else %}{% for entity_id in entities %}{{ state_attr(entity_id,
'friendly_name') | default(entity_id.split('.')[-1].replace('_',
' '), true) }}{% if not loop.last %}, {% endif %}{% endfor %}{%
endif %}
entity_id: assist_satellite.esp32_s3_box_3_assist_satellite
action: assist_satellite.announce
default: []