Convert state number to name

Hello,

Is it possible to change state number to a name, example:
If Temperature is between 0 - 15 > state is “cold” If Temp is 15-25 > state is warm and so on.
I was trying to make this with
state = 1 > cold
state = 2 > cold

but that will make a mega list :stuck_out_tongue:

Using a template sensor:

{% set temp = states('sensor.your_temperature_sensor_here')|float(0) %}
{% if temp > 40 %}
  Bloody hot
{% elif 40 >= temp > 30 %}
  Very Hot
{% elif 30 >= temp > 25 %}
  Hot
{% elif 25 >= temp > 21 %}
  Getting Warm
{% elif 21 >= temp > 20 %}
  Perfect
{% elif 20 >= temp > 18 %}
  Pretty good
{% elif 18 >= temp > 13 %}
  Cool
{% elif 13 >= temp > 0 %}
  Cold
{% elif 0 >= temp > -25 %}
  Freezing (or sensor broken)
{% elif  temp <= -25 %}
  Snot freezingly cold
{% else %}
  Unknown
{% endif %}
1 Like

Another way to do the same thing.

{% set t = states('sensor.whatever') | float(-99) %}
{% set values =
    { 40: 'Extremely hot', 30: 'Very Hot', 25: 'Hot',
      21: 'Warm', 20: 'Ideal', 18: 'Cool',
      13: 'Chilly', 0: 'Cold', -15: 'Very cold',
      -25: 'Extremely cold', -50: 'Arctic cold',
      -100: 'Unknown'} %}
{{ values.get(values.keys() | select('<', t) | first, 'Unknown') }}

2 Likes

Thank you, it’s working :smile:

Can you maybe explain how this works? I am used to work with PHP, in PHP it should look like:

$Name = “name”
If $Temp <= 50 { name = Bloody hot }
ElseIf $Temp <= 40 { name = Hot }

So when temp is smaller than 50, name is Bloody hot.
Else if temp is smaller than 40, name is Hot

But with yaml i can’t “read” it.
{% if temp > 40 %}
Bloody hot
{% elif 40 >= temp > 30 %}

For me it reads like: if temp is bigger than 40, name is Bloody hot
else if 40 bigger than temp bigger than 30 = Hot

:see_no_evil: How should i read this?

Thanks

You are reading correctly.

Some of this is programmer preference and this is acceptable as well

{% if temp > 40 %}
Bloody hot
{% elif 30 < temp <= 40 %}
hot
(% endif %}

Here are other ways to look at code as all ways shown are equivalent

{% set temp = 50 | float(1) %}
{{ temp }}
{% if temp > 130 %}
  Bloody hot
{% elif 130 >= temp > 100 %}
  Very Hot
{% elif 100 >= temp and temp > 90 %}
  Hot
{% elif 90 >= temp and temp > 75 %}
  Getting Warm
{% elif 75 >= temp and temp > 65 %}
  Perfect
{% elif temp <= 65 and temp > 55 %}
  Pretty good
{% elif 45 < temp <= 55 %}
  Cool
{% elif 45 >= temp > 30 %}
  Cold
{% elif 30 >= temp > -10 %}
  Freezing (or sensor broken)
{% elif  temp <= -10 %}
  Snot freezingly cold
{% else %}
  Unknown
{% endif %}

Screen shot from developer

1 Like