Changing template sensor icon based on state [UI Helper]

Hi,

I am having trouble changing the state icon of a sensor using the Template Sensor helper in the UI.

How do I apapt the YAML from a manual config to the UI?

Example from Template Sensor

sensor:
  - platform: template
    sensors:
      day_night:
        friendly_name: "Day/Night"
        value_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            Day
          {% else %}
            Night
          {% endif %}
        icon_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

Edit: I have created a Template Sensor helper in the Helpers section.

You are going to have to explain that a bit better.

Most core dashboard cards do not support templates.

I have edited the original post

You can’t combine the state and icon logic in the UI. The state template in the UI would be like this.

You are better off adding the template to the configuration file as suggested

Adding it to your config file can be done via the File Editor addon (other methods too)

Example configuration.yaml file

default_config:

frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml

script: !include scripts.yaml

scene: !include scenes.yaml

sensor:
  - platform: template
    sensors:
      day_night:
        friendly_name: "Day/Night"
        unique_id: day_night
        value_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            Day
          {% else %}
            Night
          {% endif %}
        icon_template: >-
          {% if is_state('sun.sun', 'above_horizon') %}
            mdi:weather-sunny
          {% else %}
            mdi:weather-night
          {% endif %}

A more compact method to execute the same template code.

  - platform: template
    sensors:
      day_night:
        friendly_name: "Day/Night"
        unique_id: day_night
        value_template: >-
          {{ 'Day' if is_state('sun.sun', 'above_horizon') else 'Night' }}
          
        icon_template: >-
          {{ 'mdi:weather-sunny' if is_state('sun.sun', 'above_horizon') else 'mdi:weather-night' }}

Ok, thanks. I was trying to avoid using manual config as I have had trouble with splitting the config, due to not wanting to a large configuration.yaml file. And being unsure if some of the methods in older forum posts or even in the documentation are actually still valid.

Templating the state via the UI is available, but the icon is the kicker. From my understanding that has always been manual process. If you have a older forum post describing that method, it may add clarity.