Sensor Template and Adjustment

I have a MQTT sensor reporting the temperature to home assistant. The sensor is off by a few degrees so I would like to adjust it in HA. I can’t seem to be able to figure out the template that would do that. I want to subtract x number of degrees from the sensor value before displaying it. How could I make this work? It seems like I can’t do a subtract or add in a template, only a multiply. I tried that but it gave a very strange number. or a different error.

Current Configuration:

 mqtt:
  broker: 127.0.0.1
  port: 1883
  client_id: home-assistant-1
  username: pi
  password: raspberry

sensor:
  - platform: mqtt
    state_topic: 'sensor/mbc/1/it/nasa_library/temperature'
    name: 'Library NASA Temp'
    unit_of_measurement: '°F'
  - platform: mqtt
    state_topic: 'sensor/mbc/1/it/nasa_library/humidity'
    name: 'Library NASA Humidity'
    unit_of_measurement: '%'
  - platform: mqtt
    state_topic: 'sensor/mbc/1/it/nasa_library/barometer/inchHg'
    name: 'Library NASA Pressure'
    unit_of_measurement: 'inHg'
  - platform: mqtt
    state_topic: 'climate/library_nasa_fan/status'
    name: 'NASA Fan Status'
  - platform: template
    sensors:
      library_nasa_temp:
        value_template: "{{ states.sensor.library_nasa_temp.state | minus(5) }}"
        friendly_name: "NASA Temp Adjusted"
        unit_of_measurement: "°F"

switch:
  - platform: mqtt
    name: Library NASA Ventilation Fan
    command_topic: "climate/library_nasa_fan/control"
    state_topic: "climate/library_nasa_fan/status"
    payload_on: "1"
    payload_off: "0"
    retain: true
  - platform: mqtt
    name: Library LED Lights
    command_topic: "lighting/library/led_strips/control"
    state_topic: "lighting/library/led_strips/status"
    payload_on: "1"
    payload_off: "0"

{{ states.sensor.library_nasa_temp.state | int - 5 }}
works for me.

Changing it to

  - platform: template
    sensors:
      library_nasa_temp:
        value_template: "{{ states.sensor.library_nasa_temp.state | int - 5 }}"
        friendly_name: "NASA Temp Adjusted"
        unit_of_measurement: "°F"

caused it to do this:

Please try another name for the template like

  - platform: template
    sensors:
      library_nasa_temp_adj:
        value_template: "{{ states.sensor.library_nasa_temp.state | int - 5 }}"
        friendly_name: "NASA Temp Adjusted"
        unit_of_measurement: "°F"
2 Likes

You can do float - 5 also if you wanted to keep the decimal.

1 Like

Super! That fixed it. I misunderstood and thought that was supposed to be the sensor name I was getting the data from, not a new sensor name. Makes sense though! Thanks @VDRainer!
Thanks @Mike_D for that tip - I was just going to ask and that solved that issue as well!
It now displays exactly how I want it to, and now I just need to hide the unadjusted one: