Dev tools template, what am I doing wrong in this code

on dev tools template i tried a code i found in here.

but no matter what i enter for azimuth, i always get a result thats 1, and I just dont see what I did wrong.

{% set my_test_json = {
  "sun_azimuth": "980"
} %}

sun azimuth is {{ my_test_json.sun_azimuth }}

{% if states('my_test_json.sun_azimuth') | float < 80 %}
  1
{% elif states('my_test_json.sun_azimuth') | float >= 90 and states('my_test_json.sun_azimuth') | float < 140 %}
  {{ (((-2 * (states('my_test_json.sun_azimuth') | float )  + 280) / 10) | int) * 10 }}
{% elif states('my_test_json.sun_azimuth') | float >= 140 and states('my_test_json.sun_azimuth') | float < 220 %}
  2
{% elif states('my_test_json.sun_azimuth') | float >= 220 and states('my_test_json.sun_azimuth') | float <= 240 %}
  {{ ((( 2 * (states('my_test_json.sun_azimuth') | float )  - 420) / 10) | int) * 10 }}
{% else %}
  100
{% endif %}

a hint would be highly appreciated

thanks Manne

That’s because the “states” method only works on entities in the state machine.

What you created was a variable called “my_test_json” with a value. You didn’t create an entity.

So anywhere you have “states(‘my_test_json.sun_azimuth’)” replace it with ‘my_test_json.sun_azimuth’ (without the quotes) and i believe it should work.

your right, thanks.

now I am just stuck with the calculations, today is not my brightest moment in life.

 {{ (((-2 * (states('my_test_json.sun_azimuth') | float )  + 280 )/ 10) | int) * 10 }}

After watchin several python for dummies vids and reading everything about the “|” in pyhton i am not one inch further down the knowledge road.

i cant change the
(states(‘my_test_json.sun_azimuth’)
for
my_test_json.sun_azimuth

the | character is a shorthand notation to direct the output of the previous function to a filter.

so “states(‘my_test_json.sun_azimuth’) | float” just means send the returned value of “states(‘my_test_json.sun_azimuth’)” to the “float” filter.

Of course you can. Why do you think you can’t?

{{ (((-2 * (my_test_json.sun_azimuth) | float )  + 280 )/ 10) | int) * 10 }}

herre is the whole thing from your first post converted to the way I suggested:

{% set my_test_json = {
  "sun_azimuth": "980"
} %}

sun azimuth is {{ my_test_json.sun_azimuth }}

{% if my_test_json.sun_azimuth | float < 80 %}
  1
{% elif my_test_json.sun_azimuth | float >= 90 and my_test_json.sun_azimuth | float < 140 %}
  {{ (((-2 * (my_test_json.sun_azimuth | float )  + 280) / 10) | int) * 10 }}
{% elif my_test_json.sun_azimuth | float >= 140 and my_test_json.sun_azimuth | float < 220 %}
  2
{% elif my_test_json.sun_azimuth | float >= 220 and my_test_json.sun_azimuth | float <= 240 %}
  {{ ((( 2 * (my_test_json.sun_azimuth | float )  - 420) / 10) | int) * 10 }}
{% else %}
  100
{% endif %}
1 Like

real dump issue :slight_smile: thanks for helping me out of that rabbit hole :smiley:

My error was that I also eliminated the ( before the “states…” as i said, real dump.

I just copied your perfectly working code and was dumbfounded it did indeed work as it was in my opinion exactly the same I had tried.

Took me a while to get my other computer online and take a real hard look at my test code in notepad++… and then it dawned.

1 Like