Add a “+” symbol to value in template

I currently got this:

{{state_attr('sensor.glucose_value_change', 'change' )}}

Which spits out, for example: -0.2
Thats great for negative values, but when the value is a positive one, it’ll just show as: 0.2.

I’d like to add a + symbol to that positive value, just like the “ - “ symbol on negative values.

I’m not sure which templating method to use here to look for a “ - “ and ignore, or see no symbol infront of the value, and add a “ + “.

Any help would be greatly appreciated

try this:

{% if state_attr('sensor.glucose_value_change', 'change' ) >0 %}+{% endif %}{{state_attr('sensor.glucose_value_change', 'change' )}}
2 Likes
{{ "{:+.1f}".format(float(state_attr('sensor.glucose_value_change', 'change'))) }}

.1 sets it 1 decimal place, so change or remove as needed. float() can be omitted if the attribute is already a float.

1 Like

Worked like a charm. Thank you

Hi guys I just fell upon this thread and tried your solution like so:

      heat_chart_yearly_compare:
        value_template: >-
          {% if (states('sensor.mariadb_yearly_heat_comparison') | float) >0 %}+{% endif %}
          {{ states('sensor.mariadb_yearly_heat_comparison') }}
      electricity_chart_yearly_compare:
        value_template: >-
          {% if (states('sensor.mariadb_electricity_yearly_comparison') | float) >0 %}+{% endif %}
          {{ states('sensor.mariadb_electricity_yearly_comparison') }}
      water_chart_yearly_compare:
        value_template: >-
          {% if (states('sensor.mariadb_water_yearly_comparison') | float) >0 %}+{% endif %}
          {{ states('sensor.mariadb_water_yearly_comparison') }}

Unfortunately I get a unexpected space character when the plus sign is applied:

Any suggestions?

Change to (as above):

      heat_chart_yearly_compare:
        value_template: >-
          {% if (states('sensor.mariadb_yearly_heat_comparison') | float) >0 %}+{% endif %}{{ states('sensor.mariadb_yearly_heat_comparison') }}

Now my conf says:

      heat_chart_yearly_compare:
        value_template: >-
          {% if (states('sensor.mariadb_yearly_heat_comparison') | float) >0 %}+{% endif %}{{ states('sensor.mariadb_yearly_heat_comparison') }}

Then it looks like this:

Yes you’re right, using a + symbol, the + disappears, using anything else works

Can’t help, sorry

{{ states('sensor.mariadb_yearly_heat_comparison') }}

926


+{{ states('sensor.mariadb_yearly_heat_comparison') }}

926


+ {{ states('sensor.mariadb_yearly_heat_comparison') }}

+ 926


X{{ states('sensor.mariadb_yearly_heat_comparison') }}

X926