Help with demystifying template sensors

Hi all, I’m really struggling with creating a template sensor to convert some of my soil moisture float sensor data into a plain text value. Long term, I’d like to turn this into a blueprint so I can easily apply it to each sensor, but with how much trouble I’m having now that has been pushed back lol.

Expected Behavior

I’d like to ingest the raw float value from my soil moisture sensor, and then turn create another plain text sensor that outputs a string value depending on how moist the soil is. I’d then like this outputted string to be accessible as a sensor value, so that I can use it in my Lovelace dashboards.

What’s happening

The code works within the Template section of the Developers tab, but it’s not accessible as a sensor once I create the automation. I only see the automation which runs it

What I think the problem is

I think this is an issue with how I’m declaring the sensor/template within automations.yaml. But no matter how many different ways I construct it I get the same result. I’m hoping someone can help clear up how I take a functioning value template from the Developer’s Tools and put it into production in a way where I can access it’s result as sensor.plain_text_snow_peas for example

Technical Information And Scripts

Hass.io OS 5.13

supervisor-2021.04.3

core-2021.5.5

Value Template

{% set sat_level = states('sensor.soil_moisture_level_3') | float %}
    
              {% if 0 < sat_level <= 20 %}
                Bone Dry
              {% elif 20.001 < sat_level <= 40 %}
                Water Soon
              {% elif 40.00 < sat_level <= 60 %}
                Moderately Moist
              {% elif 60.00 < sat_level <= 80 %}
                Recently Watered
              {% elif 80.00 < sat_level %}
                Absolutely Soaked
              {% else %}
                Saturation Unknown
              {% endif %}

automations.yaml


    - id: '1618931435538'
      alias: Printer Power Down
      description: ''
      trigger:
      - platform: state
        entity_id: \binary_sensor.octoprint_printing
        from: 'on'
        to: 'off'
        for: 00:10:00
      action:
      - type: turn_off
        device_id: 0ca104bde9ea4742dd0315ca19c1bdba
        entity_id: light.printing_overhead
        domain: light
      - type: turn_off
        device_id: 9a1a35ba729ed97241114d6e1cd0ba10
        entity_id: switch.closet_outlet_on_off
        domain: switch
      mode: single
    - id: '1618932266889'
      alias: Bar Lights Power
      description: ''
      use_blueprint:
        path: EPMatt/ikea_e1743.yaml
        input:
          integration: ZHA
          controller_device: 07b966807cd931790a7d548161e495dc
          action_button_up_short:
          - type: turn_on
            device_id: bd4e9c7e7d4b87b5c8c9971d74c0f572
            entity_id: light.breakfast_bar_light_1
            domain: light
            brightness_pct: 100
          - type: turn_on
            device_id: e88ced2964111c9b6c6ba2ee7f3ffb26
            entity_id: light.breakfast_bar_light_2
            domain: light
            brightness_pct: 100
          action_button_down_short:
          - type: turn_off
            device_id: bd4e9c7e7d4b87b5c8c9971d74c0f572
            entity_id: light.breakfast_bar_light_1
            domain: light
          - type: turn_off
            device_id: e88ced2964111c9b6c6ba2ee7f3ffb26
            entity_id: light.breakfast_bar_light_2
            domain: light
    - id: '1619218813557'
      alias: Grow Lights On
      description: ''
      trigger:
      - platform: time
        at: '6:00'
      condition: []
      action:
      - type: turn_on
        device_id: 9243036911c8b618e6a5cc2e638679f2
        entity_id: switch.space_heater
        domain: switch
      mode: single
    - id: '1619218860127'
      alias: Grow Lights Off
      description: ''
      trigger:
      - platform: time
        at: '21:00'
      condition: []
      action:
      - type: turn_off
        device_id: 9243036911c8b618e6a5cc2e638679f2
        entity_id: switch.space_heater
        domain: switch
      - type: turn_off
        device_id: 0ca104bde9ea4742dd0315ca19c1bdba
        entity_id: light.printing_overhead
        domain: light
      mode: single
    - id: '1620237100047'
      alias: Don't kill the seeds!!
      description: Get a notification if the seedlings get too hot
      trigger:
      - platform: numeric_state
        entity_id: sensor.greenhouse_ambient_temperature
        above: '87'
      condition: []
      action:
      - device_id: 37b3fd255de7adbfff583974f229d271
        domain: mobile_app
        type: notify
        title: Seedling Warning
        message: Your seedling nursery has reached temperatures over 88 degrees!
      mode: single
    - id: '1620774165350'
      alias: Water the plants downstairs
      description: ''
      trigger:
      - platform: numeric_state
        entity_id: sensor.soil_moisture_level
        below: '40'
      condition: []
      action:
      - device_id: 37b3fd255de7adbfff583974f229d271
        domain: mobile_app
        type: notify
        title: Water The Peas!
        message: Your snow peas' soil just dropped below 40% saturation, it's probably
          time to go check on the plants!
      mode: single
    - id: '1620785218157'
      alias: Bar Light Switch
      description: Control the bar lights without your voice
      use_blueprint:
        path: niro1987/zha_ikea_tradfri_2button_remote_brightness.yaml
        input:
          remote_ieee: 5c:02:72:ff:fe:94:91:71
          light_entity: light.kitchen_lights
    - id: '1621184064105'
      alias: Keep Home Assistant Fresh
      description: ''
      trigger:
      - platform: time
        at: 01:00:00
      condition: []
      action:
      - service: homeassistant.restart
      mode: single
    - id: '1622391156482'
      alias: Snow Pea Soil State
      description: Convert the raw snow pea soil state into a plain text description based
        on 5 predefined buckets
      trigger:
      - platform: template
        value_template: "{% set sat_level = states('sensor.soil_moisture_level_3') | float\
          \ %}\n\n          {% if 0 < sat_level <= 20 %}\n            Bone Dry\n     \
          \     {% elif 20.001 < sat_level <= 40 %}\n            Water Soon\n        \
          \  {% elif 40.00 < sat_level <= 60 %}\n            Moderately Moist\n      \
          \    {% elif 60.00 < sat_level <= 80 %}\n            Recently Watered\n    \
          \      {% elif 80.00 < sat_level %}\n            Absolutely Soaked\n       \
          \   {% else %}\n            Saturation Unknown\n          {% endif %}"
      action:
      mode: single

What you are doing is set the value to a variable called sat_level, but never assigned it to a entity.

I think the rigth way is to put that in a template sensor and not in a automation.

template:
  - sensor:
      - name: "Pea Soil State"
        state: >-
          {% set sat_level = states('sensor.soil_moisture_level_3') | float %}
          {% if 0 < sat_level <= 20 %}
          Bone Dry
          {% elif 20.001 < sat_level <= 40 %}
          Water Soon
          {% elif 40.00 < sat_level <= 60 %}
          Moderately Moist
          {% elif 60.00 < sat_level <= 80 %}
          Recently Watered
          {% elif 80.00 < sat_level %}
          Absolutely Soaked
          {% else %}
          Saturation Unknown
          {% endif %}

If you really want an automation or a blueprint, you need to create an entity to be used as output before, for example a helper input_text. Then in your automation use the service to set the desired state to the output entity. For example, for a imput text is input_text.set_value.

action:
  - service: input_text.set_value
      target:
        entity_id: input_text.pea_soil_state
      data:
        value: "desired value"

I was able to get a solution to my problem over in /r/homeassistant. If anyone else ends up with the problem the solution is here: https://www.reddit.com/r/homeassistant/comments/noegpq/help_demystify_my_journey_with_template_sensors/?utm_source=share&utm_medium=web2x&context=3

TL,DR: I needed to move my value template out of automations.yaml, change the formatting slightly, and place it in configuration.yaml. Here’s how I formatted it in the end:

template:
  - sensor:
      - name: Snow Pea Saturation Level
        icon: mdi-watering-can
        state: >
          {% set sat_level = states('sensor.soil_moisture_level_3') | float %}
          {% if 0 < sat_level <= 20 %}
            Bone Dry
          {% elif 20.001 < sat_level <= 40 %}
            Water Soon
          {% elif 40.00 < sat_level <= 60 %}
            Moderately Moist
          {% elif 60.00 < sat_level <= 80 %}
            Recently Watered
          {% elif 80.00 < sat_level %}
            Absolutely Soaked
          {% else %}
            Saturation Unknown
          {% endif %}

This is good to know, going to mess around with this method as I feel like it might be the key to creating a blueprint. Thanks you!!

Here a complete automation example:

alias: 'soil moisture'
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.soil_moisture_level_3
variables:
    soil_state: >-
      {% set sat_level = states('sensor.soil_moisture_level_3') | float %}
      {% if 0 < sat_level <= 20 %}
        Bone Dry
      {% elif 20.001 < sat_level <= 40 %}
        Water Soon
      {% elif 40.00 < sat_level <= 60 %}
        Moderately Moist
      {% elif 60.00 < sat_level <= 80 %}
        Recently Watered
      {% elif 80.00 < sat_level %}
        Absolutely Soaked
      {% else %}
        Saturation Unknown
      {% endif %}
condition: []
action:
  - service: input_text.set_value
      target:
        entity_id: input_text.pea_soil_state
      data:
        value: soil_state

Note, that you need to create first the entity input_text.pea_soil_state to make it work.

I leave for you to convert this in a blueprint