Passing a secret into template logic

Is there a way to pass info in the secret.yaml into the code used for a template sensor?

This doesn’t work, but is an example of what I’m trying to do:
{% if is_state('binary_sensor.myphone_wifi_state','on') and is_state('sensor.myphone_wifi_connection',"!secret ATrustedWifiSSID") %}

I’m not aware of any option like that. Maybe somebody else has knowledge of one.

In any case, maybe a useful workaround is to create an input_boolean and set/clear that one using an automation that triggers on the sensor.myphone_wifi_connection state. I think you can use a secret inside automation variables which you can then use in the action to compare with the current state to set the input_boolean.

Something like this (didn’t test the automation):

  - platform: state
    entity_id: "sensor.myphone_wifi_connection"
    to: null  # = any state
   secret: !secret "ATrustedWifiSSID"
   - service: "{{ 'input_boolean.turn_' + ('on' if is_state('sensor.myphone_wifi_connection', secret) else 'off') }}"
     entity_id: input_boolean.myphone_wifi_state

The only way to do it is to put the entire template in secrets.yaml

value_template: !secret my_template
1 Like

You can use a template sensor to store your secret then use its state in the template:

  - sensor:
      - name: "My Secret"
        state: !secret my_secret

Then use it like this:

1 Like


The !secret gainloss (used as value_template: !secret gainloss in an external template) showed value if input_boolean.hide = on and showed “X” if it = off.

In secrets.yaml only this

gainloss: >-  
    (states('sensor.fondo1')|float - 1000) | round(2)
    if states('input_boolean.hide') == 'on' 
    else "❌"  

This works good until core update (2023.5.1 from 2022.12.9).
I do not find specific “breaking changes”… How to fix this, keeping the template in secrets.yaml?

I try with modern template but in secrets.yaml does not work (the entity is not created).

EDIT: I solved. if the unit_of_measurement: of sensor is i.e. “€” it does not validate the string "x"