How to use !secret in sensor template

HI, finally made it to play local files on my Hassio setup. Or so I thought… they aren’t local in fact, because need the base_url (I am using duckdns.org)

please help me to take out the hardcoded base_url out if this template, Ive tried many options, but can’t find the solution.

sensor:
  - platform: template
    sensors:
      sound_bite:
        friendly_name: Sound bite
        value_template: >
            {% set mapper =
              { 'Alarm clock':'mp3',
                'Allo allo':'m4r',
                'Bike horn':'mp3',
                'Broken glass':'mp3',
                'Classic phone':'mp3',
                'Confusing voice mail':'mp3',
                'Devils laugh':'mp3',
                'Goofy':'mp3',
                'Grolsch beugel':'m4r',
                'Horn of saruman':'m4r',
                'I see you':'m4r',
                'Mario power':'m4r',
                'Mr Bean calling':'mp3',
                'Mr Bean excuse me':'mp3',
                'Mr Bean Nokia':'mp3',
                'Parsifal glocken':'mp3',
                'Police siren':'m4r',
                'Popipopipopi':'mp3',
                'Razor':'mp3',
                'Rooster':'mp3',
                'Siren':'mp3',
                'Truck siren':'mp3' } %}
            {% set state = states('input_select.sound_bite') %}
            {% set url = 'https://mydomain.duckdns.org:8123' %}
            {% set path = '/local/sounds/sound_bites/'%}
            {% set sound_bite = state|lower|replace(' ','_') %}
            {% set ext = '.' + mapper[state] %}
            {{[url,path,sound_bite,ext]|join}}

the template works very nicely, and sounds are playing and now I can use that script in my automation and other sound files :wink: just how do I get rid of the base_url

I presume you want to store ‘/local/sounds/sound_bites/’ in a variable in secrets.yaml?

Unfortunately, you cannot use !secret variables in templates.
There are several ways to work around it though.
I think one of the most elegant is to create a template sensor with

value_template: '/local/sounds/sound_bites/'

so you can write something like

{% set path = states('sensor.my_base_url') %}

thanks for chiming in.

not really, I want to use the base_url, which is in my secrets.yaml, in the template.

this is my current sensor:

      sound_bite:
        friendly_name: Sound bite
        value_template: >
            {% set state = states('input_select.sound_bite') %}
            {% set url = 'https://mydomain.duckdns.org:8123' %}
            {% set path = '/local/sounds/sound_bites/'%}
            {% set sound_bite = state|lower|replace(' ','_') %}
            {% set ext = '.mp3' %}
            {{[url,path,sound_bite,ext]|join}}

I like the idea to replace that with:

  sound_bite:
    friendly_name: Sound bite
    value_template: >
        {% set state = states('input_select.sound_bite') %}
        {% set url = states('sensor.my_base_url') %}
        {% set path = '/local/sounds/sound_bites/'%}
        {% set sound_bite = state|lower|replace(' ','_') %}
        {% set ext = '.mp3' %}
        {{[url,path,sound_bite,ext]|join}}

but not sure how to get to the !secret base_url in the value_template…

Well, when I read your initial post and a comment, it’s still unclear where you want that base_url to be in your template. Maybe it’s a good idea to articulate your goal more precisely? :slight_smile:

To answer your question as I understand it now - you need to create an input_text:

input_text:
  base_url:
    initial: !secret base_url

and then in your template

{% set url = states('input_text.base_url') %}
6 Likes

very nice!
thank you very much.
I can report this to be working fine. I’ve bent my head around this and never thought of the input_select.text to get a !secret into the configuration.

Wouldnt there be a component that hasn’t got an option for the enduser to change the value? Ive set it to mode: password now so the url isn’t revealed in the Front-end. but would prefer it not to be changeable at all.

54
Thought about input_select, with only the !secret set as option, but this component can’t be hidden using mode: password, which is logical, since it is designed for user interfacing…

maybe another component comes to mind?

It’s a long way to go…
Apparently you’re not using Lovelace, are you?
You just need to hide your input_text, no need to use password mode:

customize:
  input_text.base_url:
    hidden: true

as per this

no not using Lovelace as my main interface, and yes, of course I know how to customize…hidden was already set to true, as done with all unused components in my setup. thank you.

forgive me for not having said:

I don’t quite get it - is your goal achieved yet or there’s something you’re not quite happy about?