Justbob
(Bob)
December 12, 2023, 3:39pm
1
I’m trying to create a template to grab a Text selection from a dropdown and pass it to a script data item as an integer. I’m able to get the text value of the selection and interpret it as a number in developer tools but the script wont’ accept the template value as an integer even when casting to int. Anybody have any thoughts?
alias: Delete Camera Captures
sequence:
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
- service: delete.files_in_folder
data:
folder: /config/www/tmp
time: {{ states('input_select.test_dropdown') | int }}
scan_subfolders: true
mode: single
icon: mdi:delete-clock
The current dropdown has:
0
3600
86400
604800
as its options
Your template needs to be in quotes:
time: "{{ states('input_select.test_dropdown') | int }}"
1 Like
Justbob
(Bob)
December 12, 2023, 3:59pm
3
Thanks! I did get it to work after a bit of a struggle!
Justbob
(Bob)
December 12, 2023, 4:18pm
4
This is what I ended up with.
- service: delete.files_in_folder
data:
folder: /config/www/tmp
time: >-
{% if states('input_select.file_age') == 'All' %} {{ 0 | int }} {% elif
states('input_select.file_age') == '1 Hour' %} {{ 3600 | int }} {% elif
states('input_select.file_age') == '24 Hours' %} {{ 86400 | int }} {%
elif states('input_select.file_age') == '1 Week' %} {{ 604800 | int }}
{% endif %}
scan_subfolders: true
This takes text input from the dropdown and converts it to a numeric value for data:[time]. For anyone struggling with text to int conversions.
Another option is to use a dictionary to map the state values to their corresponding integers
- service: delete.files_in_folder
data:
folder: /config/www/tmp
time: >-
{% set select_map = {
"All": 0, "1 Hour": 3600, "24 Hours": 86400, "1 Week": 604800} %}
{{ select_map.get(states('input_select.file_age')) }}
scan_subfolders: true
3 Likes
Justbob
(Bob)
December 17, 2023, 3:28am
6
Thanks! That’s a lot cleaner!
Is there a way to do this in a card?
type: custom:gallery-card
entities:
- sensor.snapshots
menu_alignment: right
maximum_files: '{{ states(''input_number.files_to_display'') }}'
show_reload: true
This doesn’t seem to work in a card. Is that by design or is it a syntax issue?
Most cards do not accept templating, but some do. Since this is a custom card you will need to check its repository… This is not a card I’m familiar with, but from a cursory read I don’t see anything to suggest that gallery-card allows templating.
Justbob
(Bob)
December 17, 2023, 12:54pm
8
Thanks Drew!
I’ll dig in a little deeper and maybe do a feature request.