Ricks88
November 27, 2021, 10:37am
1
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
tom_l
November 27, 2021, 11:28am
2
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
123
(Taras)
November 27, 2021, 1:30pm
3
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
Ricks88
November 29, 2021, 4:56pm
4
Thank you, it’s working
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
How should i read this?
Thanks
AllHailJ
(J Gent)
November 29, 2021, 6:02pm
5
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