Help with templates extract string from float base on other value

I have AC volt Sensor I need to get the state as string from float as below

{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over
{% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal
{% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under 
{% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline
{% else %} Unknown
{% endif %}

also, I create 3 input_number

input_number.power_max_ac_voltage
input_number.power_min_ac_voltage
input_number.power_offline_ac_voltage

I need to replace the number in templet with (input_number) value like

{% if states('sensor.power_main_ac_voltage') | float > {{states('input_number.power_max_ac_voltage')|float }} %}Over 

but without success

If using the legacy template sensor platform:

value_template: >
  {% set voltage = states('sensor.power_main_ac_voltage') | float(0) %}
  {% set max_v = states('input_number.power_max_ac_voltage') | float(240) %}
  {% set min_v = states('input_number.power_min_ac_voltage') | float(180) %}
  {% set offline_v = states('input_number.power_offline_ac_voltage') | float(80) %}
  {% if voltage > max_v %}
    Over
  {% elif max_v >= voltage >= min_v %}
    Normal
  {% elif voltage < min_v %}
    Under 
  {% elif voltage < offline_v %}
    Offline
  {% else %} 
    Unknown
  {% endif %}

Or if using the template integration:

state: >
  {% set voltage = states('sensor.power_main_ac_voltage') | float(0) %}
  {% set max_v = states('input_number.power_max_ac_voltage') | float(240) %}
  {% set min_v = states('input_number.power_min_ac_voltage') | float(180) %}
  {% set offline_v = states('input_number.power_offline_ac_voltage') | float(80) %}
  {% if voltage > max_v %}
    Over
  {% elif max_v >= voltage >= min_v %}
    Normal
  {% elif voltage < min_v %}
    Under 
  {% elif voltage < offline_v %}
    Offline
  {% else %} 
    Unknown
  {% endif %}

These both use your default limits if the input numbers or sensor are unavailable. This will prevent errors in the template but may give you a false sense of security (as it will never be ‘Unknown’). You should also use an availability template to catch if any of the input numbers or sensors are unavailable or unknown.

e.g. for the template integration:

availability: >
  {{ states('sensor.power_main_ac_voltage') | float(none) != none and
    states('input_number.power_max_ac_voltage') | float(none) != none and
    states('input_number.power_min_ac_voltage') | float(none) != none and
    states('input_number.power_offline_ac_voltage') float(none) != none }}

Change availability: to availability_template: if you are using the legacy template sensor platform.

@tom_l
Thanks for your replay I did it like this :

template:
  - sensor:             
      - name: "Main AC Voltage Status"
        unique_id: main_ac_voltage_status
        icon: mdi:format-list-bulleted-type
        state: >
          {% set voltage = states('sensor.power_main_ac_voltage') | float(0) %}
          {% set max_v = states('input_number.power_max_ac_voltage') | float(240) %}
          {% set min_v = states('input_number.power_min_ac_voltage') | float(180) %}
          {% set offline_v = states('input_number.power_offline_ac_voltage') | float(80) %}
          {% if voltage > max_v %}
            Over
          {% elif max_v >= voltage >= min_v %}
            Normal
          {% elif voltage < min_v %}
            Under 
          {% elif voltage < offline_v %}
            Offline
          {% endif %}         
        availability: >
          {{ states('sensor.power_main_ac_voltage') | float(none) != none and
            states('input_number.power_max_ac_voltage') | float(none) != none and
            states('input_number.power_min_ac_voltage') | float(none) != none and
            states('input_number.power_offline_ac_voltage') float(none) != none }}  

I receive this error in log

Logger: homeassistant.config
Source: config.py:454
First occurred: 14:15:27 (2 occurrences)
Last logged: 14:15:33

Invalid config for [template]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'float') for dictionary value @ data['sensor'][0]['availability']. Got "{{ states('sensor.power_main_ac_voltage') | float(none) != none and\n states('input_number.power_max_ac_voltage') | float(none) != none and\n states('input_number.power_min_ac_voltage') | float(none) != none and\n states('input_number.power_offline_ac_voltage') float(none) != none }} ". (See /config/Integrations/Templates/Power AC Voltage Status .yaml, line 1).

and this warning

Logger: homeassistant.helpers.template
Source: helpers/template.py:1292
First occurred: 04:37:24 (159 occurrences)
Last logged: 07:27:44

Template warning: 'float' got invalid input 'unknown' when rendering template '{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over {% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal {% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under {% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline {% else %} Unknown {% endif %} input_number.power_max_ac_voltage input_number.power_min_ac_voltage input_number.power_offline_ac_voltage {{ states('sensor.power_main_ac_voltage')|float }} {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), states('sensor.inverter2qpigs_pv1_charging_power') ] | map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
Template warning: 'float' got invalid input 'unknown' when rendering template '{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over {% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal {% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under {% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline {% else %} Unknown {% endif %} input_number.power_max_ac_voltage input_number.power_min_ac_voltage input_number.power_offline_ac_voltage {{ states('sensor.power_main_ac_voltage')|float }} {{ [ states('sensor.inverter1qpigs_pv1_charging_power'), states('sensor.inverter2qpigs_pv1_charging_power') ] | map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
Template warning: 'float' got invalid input 'unknown' when rendering template '{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over {% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal {% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under {% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline {% else %} Unknown {% endif %} input_number.power_max_ac_voltage input_number.power_min_ac_voltage input_number.power_offline_ac_voltage {{ states('sensor.power_main_ac_voltage')|float }} {{ [ states('sensor.power_sub_t_1la'), states('sensor.inverter2qpigs_pv1_charging_power') ] | map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
Template warning: 'float' got invalid input 'unknown' when rendering template '{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over {% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal {% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under {% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline {% else %} Unknown {% endif %} input_number.power_max_ac_voltage input_number.power_min_ac_voltage input_number.power_offline_ac_voltage {{ states('sensor.power_main_ac_voltage')|float }} {{ [ states('sensor.power_sub_t_1la'), states('sensor.inverter2qpigs_pv1_charging_power') ] | map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
Template warning: 'float' got invalid input 'unknown' when rendering template '{% if states('sensor.power_main_ac_voltage') | float > 240 %}Over {% elif states('sensor.power_main_ac_voltage') | float <= 240 and states('sensor.power_main_ac_voltage') | float >= 180 %}Normal {% elif states('sensor.power_main_ac_voltage') | float < 180 and states('sensor.power_main_ac_voltage') | float > 80 %}Under {% elif states('sensor.power_main_ac_voltage') | float < 80 %}Offline {% else %} Unknown {% endif %} input_number.power_max_ac_voltage input_number.power_min_ac_voltage input_number.power_offline_ac_voltage {{ states('sensor.power_main_ac_voltage')|float }} {{ [ states('sensor.power_sub_t_1la'), states('sensor.inverter2qpigs_pv1_charging_power') ] | map('float') | sum }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

also, I have a question for example :

 {% set max_v = states('input_number.power_max_ac_voltage') | float(240) %}

why put (240) if this value variable came from (input_number.power_max_ac_voltage )

thanks

Missed the | on the last line, should be:

states('input_number.power_offline_ac_voltage') | float(none) != none }}

Precicely to prevent this from occuring:

That is not the template I wrote.

@tom_l
thank you again

now its work

got this point

sorry for that

@tom_l
need your help again I try to set icon and icon_color based on the status

  - sensor:             
      - name: "Power Main AC Voltage Status"
        unique_id: power_main_ac_voltage_status
        icon: >
          {% if is_state("sensor.power_main_ac_voltage_status", "Over") %}
            mdi:speedometer
          {% if is_state("sensor.power_main_ac_voltage_status", "Normal") %}
            mdi:speedometer-medium
          {% if is_state("sensor.power_main_ac_voltage_status", "Under") %}
            mdi:speedometer-slow
          {% if is_state("sensor.power_main_ac_voltage_status", "Offline") %}
            mdi:link-variant-off
          {% endif %}
        icon_color: >  
        state: >
          {% set voltage = states('sensor.power_main_ac_voltage') | float(0) %}
          {% set max_v = states('input_number.power_max_ac_voltage') | float(240) %}
          {% set min_v = states('input_number.power_min_ac_voltage') | float(180) %}
          {% set offline_v = states('input_number.power_offline_ac_voltage') | float(80) %}
          {% if voltage > max_v %}
            Over
          {% elif max_v >= voltage >= min_v %}
            Normal
          {% elif min_v >= voltage >= offline_v %}
            Under 
          {% elif voltage < offline_v %}
            Offline
          {% endif %}         
        availability: >
          {{ states('sensor.power_main_ac_voltage') | float(none) != none and
            states('input_number.power_max_ac_voltage') | float(none) != none and
            states('input_number.power_min_ac_voltage') | float(none) != none and
            states('input_number.power_offline_ac_voltage') | float(none) != none }}

without success :triumph: :sob:

There is no icon colour option. Where did you see that?

Your icon template needs elif instead of if for the second, third and fourth tests.

here

thanks its work

Do you have the third party custom-ui integration installed?

That is where the icon colour option is available.

no for that it not work thanks for your advice

@tom_l I need your help again I’m looking for a templet that gives me a total of may sensors end up with this :

  - sensor:             
      - name: "Power Total AC Current"
        unique_id: power_total_ac_current
        device_class: current
        icon: mdi:alpha-a
        state: >
          {{ [ states('sensor.power_sub_t_1la'), 
               states('sensor.power_sub_t_2la'),
               states('sensor.power_sub_t_3la'),
               states('sensor.power_sub_t_4la'),
               states('sensor.power_sub_t_5la'),
               states('sensor.power_sub_t_6la'),
               states('sensor.power_sub_t_1ra'),
               states('sensor.power_sub_t_2ra'),
               states('sensor.power_sub_t_3ra'),
               states('sensor.power_sub_t_4ra'),
               states('sensor.power_sub_t_5ra'),
               states('sensor.power_sub_t_6ra'),
             ]
             | map('float') | sum 
             }}
             
        availability: >
          {{ 
            states('sensor.power_sub_t_1la') | float(none) != none and
            states('sensor.power_sub_t_2la') | float(none) != none and
            states('sensor.power_sub_t_3la') | float(none) != none and
            states('sensor.power_sub_t_4la') | float(none) != none and
            states('sensor.power_sub_t_5la') | float(none) != none and
            states('sensor.power_sub_t_6la') | float(none) != none and
            states('sensor.power_sub_t_1ra') | float(none) != none and
            states('sensor.power_sub_t_2ra') | float(none) != none and
            states('sensor.power_sub_t_3ra') | float(none) != none and
            states('sensor.power_sub_t_4ra') | float(none) != none and
            states('sensor.power_sub_t_5ra') | float(none) != none and
            states('sensor.power_sub_t_6ra') | float(none) != none 
          }}                          

but in dashboard show the result like this
2022-04-19

I’m looking for something like this :
2022-04-19 (1)

how to convert the result
any advice ?

Add a unit_of_measurement to make sensors display numeric charts rather than the string type strip chart ('V' in this case).

You could also add the following to allow for long term statistics:

device_class: voltage
state_class: measurement

Please don’t tag me and demand that I help.

sorry for that