mannebk
(ManneBK)
January 27, 2022, 2:09pm
1
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
finity
January 27, 2022, 2:34pm
2
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.
mannebk
(ManneBK)
January 27, 2022, 3:51pm
3
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
finity
January 28, 2022, 6:48am
4
mannebk:
“|” in pyhton
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
mannebk
(ManneBK)
January 28, 2022, 11:16am
5
real dump issue thanks for helping me out of that rabbit hole
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