Switch case

Hi all,

I’m stuck templating.
The goal is to get the valve opening degree set based on helper value that is a delta temp.

Example:

Case
states(‘sensor.dt_kensil’) == 1
set number.“{{ trigger.entity_id }}”_valve_opening_degree to 24
states(‘sensor.dt_kensil’) == 2
set number.“{{ trigger.entity_id }}”_valve_opening_degree to 32

I’m looking at https://community.home-assistant.io/t/is-there-a-case-statement-or-equivalent-when-using-templates/553155 as a possible solution but i get a ‘None’ result

{% set d = { 'Green': 0, 'Yellow': 1, 'Orange': 2, 'Red': 3 } %}
{{ d.get( states('sensor.dt_kensil') ) }}

even if i do


{% set d = { 'Green': 0, 'Yellow': 1, 'Orange': 2, 'Red': 3 } %}
{{ d.get(0) }}

Where i expected to get ‘Green’

Can somebody point me in the right direction or suggest a better approach?
Thanks!

You have it back to front. get() looks for a key and returns the value for key: value.

Try:

{% set d = { '0': 'Green', '1': 'Yellow', '2': 'Orange', '3': 'Red' } %}
{{ d.get( states('sensor.dt_kensil') ) }}
1 Like

thanks! making progress
Now it’s something like this

{% set d = { 1: '20', 1.5: '50', 2: '70', 2.5: '100' } %}
{{ d.get(states('sensor.dt_kensil')) }}

this gives me ‘None’

Where testing with a value instead of sensor state

{% set d = { 1: '20', 1.5: '50', 2: '70', 2.5: '100' } %}
{{ d.get(2.0) }}

gives me 70 proving it works

the outcome of the sensor.dt_kensil state is 2.0 so i expected that the value returned would be the same as the test

—Update----
doing it with a variable in stead of a sensor gives also result (the sensor is a template helper that is exactly the same a the variable)

can’t figure out why the sensor is ‘ignored’

Because states are strings. Look at my example, see how I quoted the numbers to make them strings?

{{ d.get(2.0) }}

is not the same as

{{ d.get('2.0') }}

Which is what states() will return.

All you have to do is:

{% set d = { '1': '20', '1.5': '50', '2': '70', '2.5': '100' } %}
{{ d.get(states('sensor.dt_kensil')) }}

thanks! got confused because i first got my test working (what only works without the quotes) and thought simply replace the 2.0 with the sensor state.