Adding offset to sensor data

Hello,

I already used the search function and also found the thread https://community.home-assistant.io/t/add-temp-offset-with-sensor-template/16114.
But I don’ t get it working.
How can I add a offset to my sensor data?
How can I hide the sensor DHT22 and only show the sensor value from the template?
From where do I get these entity_id, I found them in the database, is it the right way to get the names?

sensor:
  - platform: dht
    sensor: DHT22
    pin: 4
    monitored_conditions:
      - temperature
      - humidity
  - platform: template
    sensors:
      dht_sensor_humidity:
        value_template: '{{states.sensor.dht_sensor_humidity.state | float + 4}}'
        friendly_name: 'Test'

You can get entity_id’s by clicking the “States” button under Developer Tools in the Home Assistant sidebar.

Find the entity_id for your DHT22 Humidity. It should show the current humidity level under the “State” column.

In your example, replace

value_template: '{{states.sensor.dht_sensor_humidity.state | float + 4}}'

with the name of the DHT22 Hudity Sensor’s entity_id.

value_template: '{{states.sensor.XXXXXXXXX.state | float + 4}}'

Thank you for your reply, the entity_id is right. But it seems not to work. The following should work, but it does not.
value_template: '{{states.sensor.dht_sensor_humidity.state | float + 4}}'
If I enable the template, it seems like it generates too much data or something wents wrong as the raspberry gets really slow if I add the template.

I’ ll try something similar tomorrow with a dummy sensor.

You are adding 4 to itself.

  - platform: template
    sensors:
      dht_sensor_humidity:
        value_template: '{{states.sensor.dht_sensor_humidity.state | float + 4}}'
        friendly_name: 'Test'

With this code, you have created a template sensor, and you are taking the value from the template sensor that you just defined and adding 4 to it.

What you want to add 4 to is the DHT sensor which is defined previously. You should be able to find the entity id of that sensor in the developer tools.

Thank you. Now I found a solution. Hopefully the following information will also help others.

To hide the original sensor data (which is defined in sensors) use customize. Syntax for customize is sensor.enitity_id:

homeassistant:
  customize:
    # Add an entry for each entity that you want to overwrite.
    sensor.dht_sensor_humidity:
      hidden: true

Sensor section: Define the sensor (in my case dht).
Create a new entitiy_id with platform: template and the name sensor.dht_sensor_humidity_offset which includes the offset (in this example 4).

sensor:
  - platform: dht
    sensor: DHT22
    pin: 4
    monitored_conditions:
      - temperature
      - humidity
  - platform: template
    sensors:
      dht_sensor_humidity_offset:
        value_template: '{{states.sensor.dht_sensor_humidity.state | float + 4}}'
        friendly_name: 'Test'
3 Likes

Guys, sorry for digging this very old post but need your help: I’ve got a humidity sensor which is definitely not properly calibrated and wanted to assign to its output a -8 (%) offset.
I’ve learned from your posts and basically added following lines to my configuration.yaml.

   - platform: template
     sensors:
       sonoff_th16_01_am2301_humidity:
         value_template: '{{states.sensor.sonoff_th16_01_am2301_humidity.state | float - 8}}'
 friendly_name: 'Test'

The strange thing is that, when I try to graph the new sensor values, it is shown as an history chart instead…

CaptureHUMIDITY
Light blue line (“camera bambine”) is the value I would like to correct and the history on top is the corrected value (offset -8).
Any idea?
Thanks, cheers.

Add

           value_template: .......
           unit_of_measurement: '°'

I use the following code. it uses the state() rather than states.senso…

which means less errors on start up.

I also define the device class to set the icons and etc, the same as all the other temp sensors.

I haven’t calculated the offset yet, in the below example. but you just adjust the 0.0 as required.

  - platform: template
    sensors:
      family_room_temperature:
        friendly_name: "Family Room - Temperature"
        value_template: "{{ states('sensor.motion_family_temperature_raw') | float + 0.0}}"
        device_class: temperature
        unit_of_measurement: '°C'
2 Likes

It would be nice to have a feature that applies the offset only under certain conditions.

I have all lights energy consumption calculated according to some measurements but then I gather power/energy data from plug and the digestor has lights and ventilations, thus the lights are included through the plug, which I’d like to deduce from the digestor power consumption as I calculate them indirectly based on data/brigthness etc as all the flat, not directly as through meteres in plugs.

If you go to developer tools, then template and “reset to demo template”. There’s some example there with IF statements that you could use to accomplish this.

Adding to this old thread as it’s the top result when searching for “temperature offset” for home assistant.

All the above helped point me in the right direction, but I found it overly complicated with talk of editing yaml files and using templates in dev tools. The following was simple and worked for me so I’ll add it here for future folk like me :slight_smile:

Settings–>Devices & services–>Helpers–>Create Helper–>Template–>Template a sensor

Add the following line in the State Template box:

{{states(‘sensor.name_of_sensor’) | float -1.0 }}

Adjusting name_of_sensor as you need and the -1.0 for the value you want to adjust by.

Fill in remaining info unit of measure, etc. Save the template with a suitable name (I use the same name as the sensor with “_calibrated” added on the end.