Change value in yaml sensor code to a Input_text value

Hi everyone

Im not a coder, just edit code enough to get myself into trouble

I have this sensor defined in configuration.yaml

template: 
  - sensor:
      - name: "Preço Electricidade"                       
        unit_of_measurement: "EUR/kWh"             
        state: >
          {% set cv, ci, ev, ei, pv, pi, hv = bool(states("schedule.eda_cheias_verao")),
                                              bool(states("schedule.eda_cheias_inverno")),
                                              bool(states("schedule.eda_vazio_verao")),
                                              bool(states("schedule.eda_vazio_inverno")),
                                              bool(states("schedule.eda_ponta_verao")),
                                              bool(states("schedule.eda_ponta_inverno")),
                                              (5 < now().month < 11) %}
          {% if (cv and hv) or (ci and not hv) %}
            {{ (0.1736 * 1.16) | round(4) }} 
          {% elif (ev and hv) or (ei and not hv) %}
            {{ (0.1072 * 1.16) | round(4) }}
          {% elif (pv and hv) or (pi and not hv) %}
            {{ (0.2416 * 1.16) | round(4) }}
          {% endif %}

what I want is to replace the number 0.1736 / 0.1072 / 0.2416 with the values from text input helpers (example: input_text.eda_preco_cheias)

I break the sensor every time i try this, anyone can help me with the right way to code it ?

I had tried (states(“input_text.eda_preco_cheias”)) … but broke it

Any help welcome

Is there a reason you are using a text helper rather than a number?

Try

int(states('input_text.eda_preco_cheias'))

e.g.

{{ (int(states('input_text.eda_preco_cheias')) * 1.16) | round(4) }} 

This will only work if you are putting a number into the text helper, if it’s not a number it will return 0.

I changed to number, I then edited the code as follows:

  - sensor:
      - name: "Preço Electricidade"                       
        unit_of_measurement: "EUR/kWh"             
        state: >
          {% set cv, ci, ev, ei, pv, pi, hv = bool(states("schedule.eda_cheias_verao")),
                                              bool(states("schedule.eda_cheias_inverno")),
                                              bool(states("schedule.eda_vazio_verao")),
                                              bool(states("schedule.eda_vazio_inverno")),
                                              bool(states("schedule.eda_ponta_verao")),
                                              bool(states("schedule.eda_ponta_inverno")),
                                              (5 < now().month < 11) %}
          {% if (cv and hv) or (ci and not hv) %}
            {{ (int(states('input_number.eda_preco_cheias')) * 1.16) | round(4) }} 
          {% elif (ev and hv) or (ei and not hv) %}
            {{ (int(states('input_number.eda_preco_vazio')) * 1.16) | round(4) }}
          {% elif (pv and hv) or (pi and not hv) %}
            {{ (int(states('input_number.eda_preco_ponta')) * 1.16) | round(4) }}
          {% endif %}

and then on the helper I added the number

example

Screenshot 2024-10-01 at 14.16.24

But with that code, the sensor price comes back as 0

Screenshot 2024-10-01 at 14.17.13

is it because I am using decimals instead of a whole number ? i need to use decimals

I’ve re-read the Jinja documentation and realised you need to use float() rather than int().

This should work without any changes to the input_number:

{{ (float(states('input_number.eda_preco_cheias')) * 1.16) | round(4) }}

Thank you !!! … that does seem to be working. really appreciate

1 Like