Change the value of a sensor

Hello

I need to change the value returned by a sensor (| float -5) . To do that I use template:

But unfortunately it does not works. In the lovelace, I get:
T ext Inconnu (unknown in french)

What I’m doing wrong ?
In this example I do not change the sensor value, I 1st just want want to display the value returned by the sensor

- platform: template                
    sensors:           
      oregontemphygro_3d0a_temp:
        entity_id:              
          - sensor.oregontemphygro_3d0a_temp
        friendly_name: "T ext"              
        value_template: "{{ states.sensor.oregontemphygro_3d0a_temp.state }}"   

Change the name of the template sensor. Currently, its name is identical to the sensor it is monitoring.

If this is the correct name of the actual temperature sensor:

sensor.oregontemphygro_3d0a_temp

then change the template sensor’s name to something different:

sensor.oregon_externe

Try this:

- platform: template                
    sensors:           
      oregon_externe:
        entity_id: sensor.oregontemphygro_3d0a_temp
        friendly_name: "T ext"              
        value_template: "{{ states('sensor.oregontemphygro_3d0a_temp') | float - 5 }}"   
1 Like

it works !
Thanks

Hi Taras, I’m having the same issue as Frank.

I cant figure out what is wrong. May you point me in right direction?

This is my configuration yaml

Loads default set of integrations. Do not remove.

default_config:

Load frontend themes from the themes folder

frontend:
themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

media_player:

  • platform: pioneer
    host: 192.168.10.86
    port: 8102
    name: VSX-1122

platform: template
sensors:
maquina_lavar_loica_correct:
entity_id: sensor.maquina_lavar_loica_power
friendly_name: “Maquina Lavar Loica”
value_template: “{{ (states(‘sensor.maquina_lavar_loica_power’) | float) | abs | round(0) }}”

maquina_lavar_roupa_correct:
  entity_id: sensor.maquina_lavar_roupa_power
  friendly_name: "Maquina Lavar Roupa"              
  value_template: "{{ (states('sensor.maquina_lavar_loica_roupa') | float) | abs | round(0) }}"

2 things -

It’s difficult to tell because you haven’t formatted the code properly, but it looks like you are missing the sensor: key before you started saying - platform: template

But in any case number 2 -
There is no need to set this specific sensor up in YAML, you can do this in the UI in Devices and Services > Helpers > Template > Sensor

Hi,

What should I fill in state template?

Whatever you would normally have put in value_template

Hi, I added this function. Im receiving negative values from this sensor.

I would like to filter negative values to 0. The result I receive is unavailable. I dont understand why. May someone give some input?

type or paste code here
`Hi, I added this function. Im receiving negative values from this sensor.

I would like to filter negative values to 0. The result I receive is unavailable. I dont understand why. May someone give some input?

{% if states("sensor.maquina_lavar_loica_power") >= 0 %}
 {{ states("sensor.maquina_lavar_loica_power") | round (2)}}
{% else %}
 {{0}}
{% endif %}`

states(sensor) returns a string, it has to be converted to do maths with it.

{% if states("sensor.maquina_lavar_loica_power")|float(0) >= 0 %}
 {{ states("sensor.maquina_lavar_loica_power")|float(0) | round (2)}}
{% else %}
 0
{% endif %}

but can be done in less lines with:

{% set v = states('sensor.maquina_lavar_loica_power')|float(0) %}
{{ 0 if v < 0 else v|round(2) }}

It worked! Thank you!

1 Like