How to from working devtools.template to sensor entity usable in lovelace and automations

Hi there,

I finally managed, with lots of help, to build a code, that mimics the suns movement through elevation and azimuth. It outputs the correct values of elevation for each degree of azimuth in devtools.template.

Not the real compound curves of course, its just linear movement, but it should be sufficient to test roller shutter code for shading and opening and closing for the night and day cycle. without waiting a whole day to see if the code works, just track it and the roller shutter with history graph in lovelace and you get an idea what the code does.

So how do I convert that code now from devtools.template to a working entity like sun.sun elevation? It should be named test.sun elevation and test.sun azimuth.

the goal is, that test.sun azimuth updates every second by one degree and test.sun elevation should be updated at the same time with the coresponding calucated value for elevation.

this is the code that works in devtools.template

{% set a = 1 %} {#  enter azimuth increments (how fast and detailed it runs) #} 
{% set b = 67  %} {# enter max elevation (for the location you want to test) #} 
{% set c = b - 80 %} {# no input here, calculates the lower elevation #}
{% set d = 80 / 180 * a %} {# no input here, calculates the elevation per azimuth increment#}

These are the Variables the code will run with
Upper Elevation: {{ b }}
Lower Elevation: {{ c }}
Set Increments: {{ a }}
Elevation step per increment:  {{ d }}

{% set ns = namespace(y=0.0, test={}) %}
{% for z in range(10) -%}
{% for x in range(a, 361, a) -%}
  {% if x <= 180 -%} 
    {% set ns.y = (x*d/a)+c %}
  {%- else -%}
    {% set ns.y = ((x-(2*(x-180)))*d/a)+c %}
  {%- endif-%}
  {% set ns.test = {  "azimuth": x,  "elevation": ns.y } %}
azimuth {{ns.test.azimuth}}       elevation {{ns.test.elevation}}
{%- endfor %}
{{z - 1}}
{%- endfor %}

thanks

its working now, thanks, i did not find that with google.

here the content of my templates.yaml regarding this issue

# Sun simulation
  - trigger:
      - platform: time_pattern
        # This will update every second
        seconds: /1
    sensor:
      # cycle throuch 1-360 rins and repeat
      - name: "test.sun_azimuth"
        unit_of_measurement: "°"
        state_class: measurement
        state: >
          {% set azimuth = states('sensor.test_sun_azimuth') | float %}
          {% if azimuth < 360 -%}
            {{ (azimuth + 1) | round(0) }}
          {%- else -%}
            {{ "0" }}
          {%- endif %}
  - trigger:
      - platform: time_pattern
        # This will update every second
        seconds: /1
    sensor:
      # cycle throuch 1-360 rins and repeat
      - name: "test.sun_elevation"
        unit_of_measurement: "°"
        state_class: measurement
        state: >
          {% set e = 1 %} {#  enter azimuth increments #} 
          {% set b = 67  %} {# enter max elevation #} 
          {% set c = b - 80 %} {# no input here, calculates the lower elevetion#}
          {% set d = 80 / 180 * e %} {# no input here, calculates the elevation per aimuth increment#}
          {% set x = states('sensor.test_sun_azimuth') | float %}
          {% if x < 180 -%}
            {{ ((x*d/e)+c) | round(2) }}
          {%- else -%}
            {{ (((x-(2*(x-180)))*d/e)+c) | round(2) }}
          {%- endif %}

any hint how I could update the elevation by change of azimuth?

could not get this to work, so now its updating each second, but thats not working every time.