# Create template sensor for a 13% temperature compensation - what to do when positive, 0 and then negative temps?

Good day all,

I’m trying to create a template sensor based on an outdoor temperature sensor (sensor.temp) with a compensation value of 13%. The 13% is to be subtracted from the reported value and ONLY when the temperature of sensor.temp temperature is below +5 oC

for example,

if the sensor.temp value is +8.4 oC, the new value should be +8.4oC

if the sensor.temp value is +3.4oC, the new value should be 3.4*0.13=0.443 = 2.96 oC

if the sensor.temp value is -30.4oC, the new value should be 3.4*0.13= -3.95 = -34.35 oC

I’m a little confused how to deal with the the positive, to 0, to negative values if something is being multiplied by it.

I’m pretty sure I have the syntax wrong below, but these are the scenarios I’d like to capture

``````template:
- sensor:
- name: compensated_outdoor_temperature
friendly_name: "Compensated Outdoor Temperature"
state_class: measurement
unit_of_measurement: °C
device_class: temperature
state: >
{% set temperature = states(‘sensor.temp’) | float(0) %}
{% if temperature > 5 %}
value = state
{% if temperature > 5 < 0 %}
value = state - (state*0.13)
{% if temperature = 0 %}
value = 0.13
{% if temperature  < 0 %}
value = state - (state*0.13)
``````

I’m sure there’s a more compact way to do this but here’s a first draft…

``````template:
- sensor:
- name: compensated_outdoor_temperature
friendly_name: "Compensated Outdoor Temperature"
state_class: measurement
unit_of_measurement: °C
device_class: temperature
state: >
{% set temperature = states(‘sensor.temp’) | float(0) %}
{% set negative = temperature < 0 %}
{% if temperature > 5 %}
{{ temperature }}
{% elif temperature == 0 %}
0.13
{% elif not negative %}
{{ temperature * 0.87 }}
{% elif negative %}
{{ (temperature | abs * 1.13) * -1 }}
{% endif %}
``````
1 Like

Or use the compensation integration with as many points as you can provide.

You can use a spreadsheet and trendline to pick the best degree of the polynomial fit.

Thanks @Didgeridrew, this should work!

and thanks @tom_l very interesting! I never thought of using this. I’m actually assuming a linear fit for my sensor using two points, i mean basically, the temperature is 4oC warmer when its -30oC outside because the weather station has a built-in heater. so, I just calculated the slope of this, which is about 13%.

If it is linear you only need two points for the compensation integration.

Are you sure that the weatherstation itself isn’t compensating for this?

thanks, @tom_l
maybe i’ll give this a try and see how it compares with the above code by Drew.

Here’s how I set it up below. Basically, when its +5 (or warmer), there should be NO compensation, but when its +5oC or colder, compensation should occur. For example, at -30oC, compensation should be an additional +4oC, so the temperature is really -34oC.

``````compensation:
outdoor_temperature_compensation:
source: sensor.temp
unit_of_measurement: °C
precision: 1
degree: 1
unique_id: 0e1a1a97-2244-4ff9-abb6-580c373bc71c
data_points:
- [+5.0, +5.0]
- [-30.0, -34.0]
``````

How will I know that the above won’t compensate when the temperature is at +25oC?

@sunqan unfortunately no, it is not automatically compensating for this. There is an option to add an offset, but its a fixed offset (i.e.x oC as opposed to x %). I have another weather station beside this one, and this one is always warmer because of its in-built heater to prevent frost/snow build up on an ultrasonic wind sensor.

1 Like

That is not a linear relationship. You will need a lot more than two points.

Ok, i’ll have to do a bit more homework on this approach, and collect data.