Template Sensor For Modbus temperature sensors

Hi

I am new to Home Assistant & in the process of trying to change across from my Vera platform to HA.

I have a number of temperature sensors that are input via modbus. When the measured temperatures are under 0 degrees, the sensor value is 1000 less the actual temperature ( eg if the temperature is -1.2 degrees, then the sensor value is 1001.3.

I need a simple IF function that will return the raw sensor value for sensor values < 1000 and (- sensor value -1000) for values > 1000.

I think the way to go is to use a sensor template, but are completely lost on how to do this as the examples for templates seem to be based on states rather than state values

My sensor entity ID is sensor.outside_west_temp

Any help appreciated.

I think the code below should work or at least be close. This template assumes that the state of the sensor.outside_west_temp is the temperature value in float format.

sensor:
  - platform: template
    sensors:
      corrected_west_temp:
        friendly_name: 'West Temp'
        value_template: >
          {% set temp = float(states('sensor.outside_west_temp')) %}
          {% if temp < 1000.0 %} {{ temp }}
          {% else %} {{ temp-1000.0 }} {% endif %}

Thanks a lot. Had to make a couple of tweaks to get the value reading negative & to just one decimal place.

  - platform: template
    sensors:
      corrected_west_temp:
        friendly_name: 'West Temp'
        value_template: >
          {% set temp = float(states('sensor.outside_west_temp')) %}
          {% if temp < 1000.0 %} {{ '%.1f'%(temp)|float }}
          {% else %} {{ '%.1f'%(1000.0-temp)|float }} 
          {% endif %}

Now trying to work out why the value is graphed as a bar chart rather than line - thought it might be something to do with needing to convert the value back to float, but that did not work.

image

Its been a good morning to test as first real frost for the season!

Cheers

Likely because the result is being viewed as a string rather than a number.
Sorry, what does the’%.1f’ do? If you’re trying to get one decimal use the round function.
Btw states are always strings even if they are numbers (python thing).

try this

  - platform: template
    sensors:
      corrected_west_temp:
        friendly_name: 'West Temp'
        value_template: >
          {% set temp = float(states('sensor.outside_west_temp')) %}
          {% if temp < 1000.0 %} {{ temp }}
          {% else %} {{ (1000.0-temp)|round(1) }} 
          {% endif %}

Thanks again.
The issue was that I had not given ‘West Temp’ any units of measurement. Adding that into the script solved the problem. The ‘.1f’ trims the value to 1 decimal place, but I dont think it rounds the number. Will try using round later today, but for now the script is now doing pretty much all I wanted, so it will be fine tuning (and learning more) from here on in

Cheers