Create template sensor rule to add a set number / value

   sensors:
     current_cost_gas_integration_2:
       friendly_name: Current Cost Gas Integration 2
       value_template: "{{ (states('sensor.current_cost_gas_integration') | float) | round (3) }}"     
       unit_of_measurement: 'kWh' 

After doing a lot of reading & trial & error configuration I still can't achieve what I'm after and I'm sure its a simple task

How do I set a rule so if a sensors output is 0 then + 0.1125?

I have an old style gas boiler that when in idle state consumes 112.5w of gas but by the pilot light
my configuration shows 0 at this point so what I want to do is
ONLY add 112.5 if output is 0

I hope this makes sense

You could use the max value of the two:

"{{ [(states('sensor.current_cost_gas_integration') | float) | round (3), 0.1125] | max }}"     

When it’s below 0.1125 then the max value is 0.1125.
If the sensor is 5, then 5 is the max value.

1 Like

On a very similar matter do you know how I can set a rule so if a value is for example 2 then change it output to 0

On 1 of my Solar PV systems I often get a phantom reading of a couple of watts at night so I want the template to set lets say anything 3 or below to 0 (ZERO)

value_template: “{{ (states(‘sensor.shed_solar’) | float) | round (0) }}”

value_template: >-
             {% if (states(‘sensor.shed_solar’) | float) | round (0) > 3 %} 
                  {{ (states(‘sensor.shed_solar’) | float) | round (0) }}
             {% else %}
                  0
             [% endif % }

WOW, fantastic, thanks for the speedy reply. I’ll give that a try now

I get this when I do a check config

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 14) for dictionary value @ data[‘sensors’][‘solar_total_2’][‘value_template’]. Got ‘{% if (states(‘sensor.solar_total’) | float) | round (0) > 3 %} \n {{ (states(‘sensor.solar_total’) | float) | round (0) }}\n{% else %}\n 0\n[% endif % }’. (See ?, line ?).

I just copied & pasted your suggestion into my configuration.yaml

I just copied your code.
It seems since you did not use a code block there is incorrect characters in the code.

{% if (states(‘sensor.shed_solar’) | float) | round (0) > 3 %} 
              ^                 ^
are not correct. and the same on the line below.

Cheers, I’ll give that a try later thanks. Just finished a night shift so going for some kip
Need to give it my full concentration

I’ve been trying for an hour and getting nowhere.
My yaml skills are very poor I’m afraid
This is the actual entry I wish to ammend which is copied directly from my working config file

currentcost_shed_solar:
        entity_id: sensor.current_cost
        unit_of_measurement: 'W'
        value_template: "{{ (state_attr('sensor.current_cost', 'Appliance 3') |  float) | round (0) }}"
        friendly_name: CurrentCost Shed Solar

Apologies if I’ve caused confusion
The above is just 1 entry I wish to do it on so hopefully once I get 1 correct I can work out how to do others

My latest attempt makes me feel I’m getting somewhere near!
Any suggestion greatly appreciated

currentcost_shed_solar_3:
        unit_of_measurement: 'W'  
        value_template: "{% if is state_attr("sensor.currentcost_shed_solar") | float) | round (0) > 3 %} {{ (state-attr("sensor.currentcost_shed_solar") | float) | round (0) }} {% else %} 0 {% endif %}"
        friendly_name: CurrentCost Shed Solar 3
Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/configuration.yaml", line 126, column 9
expected <block end>, but found '<scalar>'
in "/config/configuration.yaml", line 127, column 47

So it didn’t work with just replacing the with '?

The error you have in the one above is that you use " all the time.
Look at the text color and you see that it stops the string/code where the second " comes.

What you should do is example: "{{ states('something') }}"

That way the " " enclose the full string.

But not sure it will work since you have a if else.
But what I posted before, and replacing the faulty characters should work.

1 Like

I’m at work at the moment so can’t give this a try but a few questions

value_template: >-
             {% if (states('sensor.shed_solar') | float) | round (0) > 3 %} 
                  {{ (states('sensor.shed_solar') | float) | round (0) }}
             {% else %}
                  0
            {% endif %}

Do I enter your suggestion exactly as its laid it out above?
I thought I needed to use " in all the places I have

Yes.

Only when it’s inline with value_template.
If you use >- and write one line below then it will all be read as a yaml-string.

1 Like

This version passes the config check but I get zero as an output all the time

currentcost_shed_solar_3:
        unit_of_measurement: 'W'
        value_template: >-
             {% if (states('sensor.shed_solar') | float) | round (0) > 3 %} 
                  {{ (states('sensor.shed_solar') | float) | round (0) }}
             {% else %}
                  0
             {% endif %}
        friendly_name: CurrentCost Shed Solar 3

What happens if you paste this in developer tools template:

             {% if (states('sensor.shed_solar') | float) | round (0) > 3 %} 
                  {{ (states('sensor.shed_solar') | float) | round (0) }}
             {% else %}
                  0
             {% endif %}
{{ (states('sensor.shed_solar') | float) | round (0) }}
{{ states('sensor.shed_solar') | float  }}
{{ states('sensor.shed_solar') }}

Result type: string
0

0
0
unknown

In that case there is no sensor called sensor.shed_solar
You need to look at what the name of the entity is.

Ah!!
My stupid mistake, the sensor is “sensor.currentcost_shed_solar”

Also I was not aware of the developer tools template so 2 lessons learned today

Thanks again for your help & patience Hellis81