Basic Template Calc with number & percent Question

Hello

Hello,
I have some beginner questions about helpers and templates
I have created a few helpers that are intended to be used to enter the target room temperature and the temperature tolerance.

Here, for example, the set temperature input_number.max_temperatur_tag should be added to the input_number.temperatur_tolerance (but this is given in %) and it doesn’t do that.
I have a mistake in my thinking somewhere

Thats my configuration.yaml code

# Template Sensor
template:
  - number:
      - name: "Temp Tag BigBox incl. eingestellter Toleranz"
        unique_id: "MaxBigBoxTempDay"
        unit_of_measurement: "°C"
        state: >-
           {{(states('input_number.max_temperatur_tag')+('input_number.max_temperatur_tag' + ('input_number.max_temperatur_tag' * ('input_number.temperatur_toleranz'/100))))}}



States are strings. You need to convert them to numbers (e.g. using the floating point filter) before you can do math with them. You also forgot to access the state in a number of places.

state: >
  {% set temp_tag = states('input_number.max_temperatur_tag')|float %}
  {% set temp_tol = states('input_number.temperatur_toleranz')|float / 100 %}
  {{ temp_tag * ( 1 + temp_tol ) }}

It is very difficult to determine what you actually want from your template and description so you may need to adjust it a bit.

Thanks for help!
Ok, I hope this helps me a bit. I thought that if I saw the “number” type in a created helper (if I list my helpers under ==> Settings (Integrations, Devices, Entities, Helpers)), then that would already be a “Number” data type.
Okay, I’ll try it right away.
Thanks for the tip.

I want the following:
The speed of a fan should later be controlled, depending on the room temperature and any tolerance in percent.

Example:
Room temp sensor = 22°
Specification Max. room temp on the HA helper = 21°
Default tolerance in % = 5%

Thats mean
Helper-Room-Max-temp with add 5% = 22,05

I now have to calculate based on the set values ​​whether the room temperature is within the tolerance range, or whether it is above or even below.
If I can calculate these values, then I can also set my fan speed accordingly.
This is the reason why I have to calculate with a sensor temperature and set values ​​for helpers in HA

edit:
i add now this code in my configuration.yaml
but now he not find the template in “states” more


thats the code in configuration.yaml


# Template Sensor
template:
  - number:
      - name: "Temp Tag inclusive Toleranz"
        unique_id: "MaxTempDay"
#        unit_of_measurement: "°C"
        state: >-
            { % set temp_tag  = states('input_number.max_temperatur_tag')|float  % }
            { % set temp_tol = states('input_number.temperatur_toleranz')|float / 100 % }
            {{ temp_tag * (1 + temp_tol) }}

Yeah ok then what I posted will work.

You want a template sensor, not number.

This post was the first time I was aware of a template number. The docs don’t explain — why should we use template sensors instead of numbers, particularly in an application like this, which is a combination of two user input_numbers?

1 Like

Thats the problem of my thinking too.
i don’t understand when i have to use sensor: or number:
Okay i will try it but expaln would be very good if have anybody a link to this document

edit.:
thats my configuration-yaml now

# Template Sensor
template:
  - sensor:
      - name: "Temp Tag inclusive Toleranz"
        unique_id: "MaxTempDay"
        unit_of_measurement: "°C"
        state: >-
          { % set temptag = states('input_number.max_temperatur_tag') | float  % }
          { % set temptol = states('input_number.temperatur_toleranz') | float / 100 % }
          {{ (temptag * (1 + temptol)) }}

# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

and thats my output
unavailable

is this right… if i change any code in configuration.yamla then i hace to restart complete HA? or is enought to reload the configuration.yaml ?

You can reload unless you’ve added a new integration for the first time, in this case template, when you need a full restart. Once the integration is added, you can reload to pick up any changes.

You have a syntax error though. Your first two lines need to be:

{% ... %}

but you have

{ % ... % }

with extra spaces.

It’s good practice to paste templates into the editor at Developer Tools / Templates to see what output you get.

You can also create template sensors in the UI under Helpers:

It shows Unavailable for me because I don’t have your input_number entities.

You can edit numbers manually from the UI. Sensors are read only.

1 Like

that would be good but where can I find this template editor?
That would help me a lot because then I can see what code he produces and could better identify my mistakes

but where is he?

i change now the code…and delte the spaces…no work… again unaivalable

template:
  - sensor:
      - name: "Temp Tag inclusive Toleranz"
        unique_id: "MaxTempDay"
        unit_of_measurement: "°C"
        state: >
          {% set temptag = states('input_number.max_temperatur_tag') | float %}
          {% set temptol = states('input_number.temperatur_toleranz') | float / 100 %}
          {{(temptag * (1 + temptol))}}

is possible… the sliders what create the values

  • input_number.max_temperatur_tag
    and
    input_number.temperatur_toleranz
    make that problems?

edit:
sorry @Troon … i find it now… sorry… i try it with this editor under helpers

mmmhh… in this editot @Troon works fine… perfect… thank you… now i have to show what i do wrong in my configuration.yaml

What you’ve screenshotted there is not the template editor, it’s the template sensor creation tool which you use instead of the YAML in configuration.yaml. Save that sensor, which should now be available in your system, delete the code from your config file and restart. My guess is that you have two template: headers or something like that.

The template editor is here:

image

i use the same what you told me… i use now the edotor under helpers…
thats wrong?
thats not a template?

mmmhh… yes i not see in my configuration.yaml

why he work there fi9ne and in my configuration.yaml not?

There are two things:

  • The template editor under Developer Tools (my second screenshot). Use this for testing your templates.
  • The template sensor creator under Helpers (my first screenshot). Use this for actually creating the sensor with the template you tested in the editor.

We need to see all of your configuration.yaml to diagnose the problem. But it is easier to just delete your code from that file and use the UI template sensor creator instead.

ok now i do that

  1. i delte the template-code section in the configuration.yaml
  2. i restart HA
  3. Then I tested the code with the editor there under Developer Tools ==> Template
  4. it works now… wow. class
    What do I have to do now so that I can create the template in my configuration.yaml?
    I still have the helper I created…!

my complete configuration.yaml … now… after delte template-code.-section

homeassistant:
  auth_providers:
   - type: trusted_networks
     trusted_networks:
       - 192.168.2.0/24
       - ff00::/8
  name: xxxxxxx
  latitude: 49.460983
  longitude: 11.061859
  elevation: 430
  unit_system: metric
  currency: EUR
  country: DE
  time_zone: "Europe/Berlin"
  external_url: "https://www.xxxxx.xxx"
  internal_url: "http://homeassistant.local:8123"
  allowlist_external_dirs:
    - "/config/tmp"
    - "/config/tmp/Cameras/snapshots/reolink_E1_Zoom"
    - "/config/www/cards"
    - "/config/www"

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

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


# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

http:
  cors_allowed_origins:
    - https://google.com
    - https://www.home-assistant.io
  ip_ban_enabled: false 
  login_attempts_threshold: 5
  use_x_forwarded_for: true
  trusted_proxies:
    - xxx.xxx.xxx.xxx/xx

#miele:

#    lang: de
#    cache_path: "/config/custom_components/miele/secrets"

# Miele WWG 760 WPS TDos&9kg W1 Frontlader
miele:
  sensors:
   - id: sensor.waschmaschine_programm
     program_ids:
      - value_raw: 1
        value: cottons
      - value_raw: 2
        value: cottons2
      - value_raw: 3
        value: eco_40_60
      - value_raw: 4
        value: delicate
      - value_raw: 5
        value: woollens
      - value_raw: 6
        value: silks
      - value_raw: 7
        value: cottons_eco
      - value_raw: 8
        value: express_20
      - value_raw: 9
        value: dark_garments
      - value_raw: 10
        value: denim
      - value_raw: 12
        value: sportswear
      - value_raw: 13
        value: outerwear
      - value_raw: 14
        value: proofing
      - value_raw: 15
        value: starch
      - value_raw: 16
        value: rinse
      - value_raw: 17
        value: silks_handcare
      - value_raw: 19
        value: standard_pillows
      - value_raw: 22
        value: separate_rinse_starch
      - value_raw: 24
        value: steam_smoothin

You don’t need to create anything in configuration.yaml. This:

is exactly what you need, although change the name from “Testtemplate” to “Temp Tag inclusive Toleranz”. Save that (Absenden) and you will have sensor.temp_tag_inclusive_toleranz in your system.

If you have already saved it, it might appear as sensor.testtemplate. You can change the entity ID if you want.

I haven’t yet understood the difference between a template entry in the configuration.yaml and the template-sensore that I can create under “helper”.

Both are a kind of template… when do you need what? Can you do most of the work with the helpers? is that the better way?
I would like to better understand the syntax of the code so that I can better avoid such errors in the future.
I would also like to understand when and where you need a template entry in the Configuration.yaml and when it is best to create it with the template sensor under Helpers

it’s really crazy:
I’ve now managed it with both variants… the template sensor works
and thetemplate entry in the configuration.yaml now also works
I now have 2 identical results. with 2 different names… great.
Still it’s very confusing for me… sorry for being so stupid :frowning:

The “helper” version isn’t (easily) visible as YAML code, and you cannot do advanced configuration like adding attributes. But it’s quicker and easier to create, and it is also a “template sensor”.

The UI creation of template sensors is quite new — before that, you had to use YAML.

They both work exactly the same way once created.

and waht is the better way?
to create the code in configuration.yaml or in helpers?

You have more control via yaml. UI you can only template the state. There is no “better way”. There is only what suits your needs.

Thank you for all ( @Troon and all others too)
yaml-percentage-fan
your help and your patience. Editing the yaml file now works wonderfully too. I’m now using VS Code and I think it’s a bit easier for me with the spaces and indenting.

When I look at the state of the fan, I also see the set PWM power in percent.

Where can I find the code for this? I would like to query the PWM percentage with this set value