Help in modifying properties of a sensor

Greeting to all !!
I’ve been using HA for some time and I think I’m an average user.
I have the following problem (which I already solved), but I think it should be possible to do it in a more efficient way.
It usually happens that I have an operational sensor and I need to change some characteristic.

For example

  • A temperature sensor that has wrong temperature readings and I have to raise it by 5 degrees
  • An energy sensor that I must multiply by an adjustment factor
  • A volume sensor that I need to convert from cubic feet to cubic meters.
  • A sensor delivers a number in string format and I must convert it to a number.

In all cases, what I do is create a new sensor through a template, which contains the necessary adjustment, which generates a large duplication of sensors.

Is there a smarter way to do it?

Thank you very much for the suggestions and help.

Pablo

Show your code.

This is an example:

The original sensor is: “sensor.lumi_lumi_sensor_magnet_aq2_80540402_device_temperature”. The adjusted one is “t_jacinta_temp

  • sensor:
    • name: “Jacinta Temp”
      unit_of_measurement: “°C”
      unique_id: “t_jacinta_temp”
      state: >
      {{ states(‘sensor.lumi_lumi_sensor_magnet_aq2_80540402_device_temperature’) | float -5}}

Another example:

The original sensor is: “sensor.casa_agua_daily_water”. The adjusted one is “aguacalientedia

  • sensor:
    • name: “Agua solar diaria”
      unit_of_measurement: “lts”
      state_class: “measurement”
      device_class: “gas”
      unique_id: “aguacalientedia”
      state: >
      {% set Aux_Agua = states(‘sensor.casa_agua_daily_water’) | float * 3.78541 %}
      {{ Aux_Agua | round(1, default=0) }}

Some ideas???

That’s the primary way of adjusting an existing sensor’s value. To minimize the impact of duplication, you can hide the original sensor from the UI. This feature (hide an entity) was introduced in version 2022.4.0.

As far as I know, a sensor is always stored in HA as a string even if you created it with a conversion to an integer or float… example:

  - state_topic: "homeassistant/smappee/record1"
    name: "Smappee Sensor Battery 1"
    value_template: "{{ value_json['battLevel'] | int(default=0) }}"
    unique_id: "smappee_sensor_battery_1"

the “sensor.smappee_sensor_battery_1” is a string and if you want to add something to it, you have first to convert it to an integer or a float… That’s the way it is… So if you want to add 8 to the sensor with a value of 76, you have to do the following:

{{ states('sensor.smappee_sensor_battery_1') | int(0) + 8 }}

Thanks a lot to both of you!

Another similar question.

How can I add a State_class to an existing sensor without duplicating it?

I want to plot some data and in must be measurement , total or total_increasing State class.

Thanks again!

You can try Manual Customization.

For example:

homeassistant:
  customize:
    sensor.whatever:
      state_class: total_increasing
    sensor.foo:
      state_class: total_increasing
1 Like

Thanks a lot!!!

1 Like