Sensors - template & value math

How can I do math on values. I tried it like this:
sensor:

  • platform: template
    sensors:
    wohnzimmer_luft_temperatur:
    value_template: ‘{{ sensor.wohnzimmer_luft_temperatur | int - 10 }}’

But it throws:

Template sensor wohnzimmer_luft_temperatur has no entity ids configured to track nor were we able 
to extract the entities to track from the value template(s). This entity will only be able to be updated 
manually.

Thank you!

I posted something then realized your template is self-referential!

You’re creating a template sensor with the same name as the sensor in value_template. They should be different. Then you have to add an entity_id:

Try this (it assumes you have an existing sensor.wohnzimmer_luft_temperatur):

- platform: template
  sensors:
    neue_wohnzimmer_luft_temperatur:
      entity_id: sensor.wohnzimmer_luft_temperatur
      value_template: '{{ sensor.wohnzimmer_luft_temperatur | int - 10 }}'

First off, when you post code you need to format it correctly in the post per the blue box above.

however, that said, it doesn’t make sense what you are trying to do.

you are trying to change the value of a sensor by subtracting a value from the same sensor.

to me I would think that you would quickly get caught up in a infinite loop in that when the sensor changes you subtract 10. then when that changes the sensor it would again subtract 10, etc, etc

Ok, so it should be like:
- platform: template
sensors:
wohnzimmer_luft_temperatur_t:
value_template: ‘{{ sensor.wohnzimmer_luft_temperatur | int - 10 }}’

Or is there any functionality to subtract a integer value from an existing sensor?

Yes, you can use the existing sensor’s value_template (assuming it has one). I can help you if you provide me with the configuration for sensor.wohnzimmer_luft_temperatur.

Config is now:

- platform: template
  sensors:
    wohnzimmer_luft_temperatur_t:
      entity_id: sensor.wohnzimmer_luft_temperatur
      value_template: '{{ sensor.wohnzimmer_luft_temperatur | int - 10 }}'

But error:

Could not render template wohnzimmer_luft_temperatur_t: UndefinedError: 'sensor' is undefined

I need to see the configuration for sensor.wohnzimmer_luft_temperatur ( not sensor.wohnzimmer_luft_temperatur_t ).

If you don’t have this sensor defined in your configuration file, that may explain the error message UndefinedError: 'sensor' is undefined.

It’s a ESPHOME sensor, so no config in the configuration.yaml. I think that’s the issue.

If it is auto-discovered by Home Assistant, it will be defined in the Entity Registry. You can confirm it is present by looking for it in Home Assistant’s States page.

Here’s an example of my outdoor temperature sensor:
Screenshot%20from%202019-03-31%2017-04-00

I was going to answer this question:

Or is there any functionality to subtract a integer value from an existing sensor?

by showing you how to make sensor.wohnzimmer_luft_temperatur report a temperature 10 degrees less than what it normally reports (using its value_template). However, I can’t do that (easily) if sensor.wohnzimmer_luft_temperatur is auto-discovered.

It’s a auto discovered sensor. So no way to modify it?

It’s possible to modify the configuration of an entity defined by the MQTT Discovery process. Basically, you would publish additional information to the entity’s discovery topic. In this case, you would publish an additional value_template, containing {{value | int - 10 }}, to the sensor’s discovery topic.

However, because you are using ESPHome, you have another option. You can use ESPHome to re-program the sensor so that it always reports a value that is 10 degrees less.

The feature is called Sensor Filters and the one you would use is offset.

1 Like

Thank you! I have some values that I can adjust with the offset filter. But in Arduino I have the following calculation:
pressure = ((pressure / pow(1.0 - 658/44330.0, 5.255)) / 100);

How can I do the same in ESPHOME?

I believe you will need to use the lambda filter. The Sensor Filters page has a few examples, including this one to convert Celsius to Fahrenheit:

filters:
  - lambda: return x * (9.0/5.0) + 32.0;
unit_of_measurement: "°F"

seems like you’re just missing the states() call to get the value:

- platform: template
  sensors:
    wohnzimmer_luft_temperatur:
      value_template: "{{ states('sensor.wohnzimmer_luft_temperatur') | int - 10 }}"
1 Like

Ugh, yes. Multiple problems in the original template; not only self-referential but also an invalid state request! I can’t believe I missed that second error. It jumps off the page now! Good catch!

1 Like

sometimes you just need a second pair of eyes

1 Like

Well, I now fixed it with the filters in ESPHOME. Thanks for your kind help!