Sun elevation and cover/blinds angle

Yes, here is it. :slight_smile:

- id: '1546263622138'
  alias: Žaluzie (zatĆ”hnout JV) - teplota/slunce/azimut
  trigger:
    platform: state
    entity_id: sun.sun
  condition:
  - above: 25
    below: 30
    condition: numeric_state
    entity_id: sensor.teplota_obyvak
  - above: 0
    below: 4
    condition: numeric_state
    entity_id: sensor.yr_symbol
  - condition: template
    value_template: "{% if trigger.from_state is not none %}\n  {% set old_el = trigger.from_state.attributes.elevation\
      \ %}\n{% else %}\n  {% set old_el = 0 %}\n{% endif %} {% set el = state_attr('sun.sun',\
      \ 'elevation')|float(0) %} {% set az = state_attr('sun.sun', 'azimuth')|float(360)\
      \ %} {{ el > 5 and az < 235 and (el//5 - old_el//5)|abs > 0 }}\n"
  action:
  - data:
      entity_id: cover.francouzke_okno_2
    service: cover.close_cover
  - data_template:
      entity_id: cover.francouzke_okno_2
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position
  - data:
      message: Nastavuji JV okna na zĆ”kladě teploty, slunce a azimutu
    service: notify.domaci_asistent
  - data:
      entity_id: cover.navstevnicky_pokoj_2
    service: cover.close_cover
  - data_template:
      entity_id: cover.navstevnicky_pokoj_2
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position
  - data:
      entity_id: cover.mateo_2
    service: cover.close_cover
  - data_template:
      entity_id: cover.mateo_2
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position

The condition only lets the actions run when the elevation changes from one multiple of 5 to another multiple of 5. For example, if, on its way up, it was 18 (i.e., >= 15 and < 20) then changed to 22 (i.e., >= 20 and < 25), or if, on its way down, it was 22 then changed to 18. So, if it changed from >= 20 to, say 18, then it would set the tilt angle to 18 + 40, or 58. Could it be something like this that happened?

You say the elevation was 20 when the tilt angle was changed to 58. Exactly how do you know that? To be absolutely sure, youā€™d have to look in the logs to verify the exact sequence of events.

Do you want the tilt angle only to be multiples of 5 as well? The automation I suggested only changes the tilt angle when the sunā€™s elevation crosses a multiple of 5, but then sets the tilt angle to whatever the sunā€™s elevation is, plus 40. That would not guarantee the tilt angle is always a multiple of 5.

Oh, I understand that. Anyway, I watched it on the suncalk. When the sun was at 15 degrees, the tilt turned to 55 when it was 20 degrees, the slope was 58. Unfortunately I can not find a log. :frowning:

Anyway, now I have come to another crucial thing. Each time the sun rises, the blinds are always closed and then the correct angle is set. Is it sort of a solution?
For example, close the blinds if the current_position is greater than 0 (0 = fully down, 100 = full up).

Sorry, what is the suncalk? (Translation problem??? :slight_smile: )

Some information may be in the Logbook, but mostly you need to look in the file home-assistant.log. It is in the same directory as configuration.yaml. Depending on what you have for the logger: configuration, there may be a LOT of detail. Look for state_changed events for sun.sun and your cover entities.

When the automation is triggered, do all three covers change at the same time? Or is there some time between when the first one starts changing and the second one starts changing, etc.?

I donā€™t use cover entities, so Iā€™m not sure. In general trying to change something to a state that is already in is not a problem and results in no extra ā€œwork.ā€ For example, typically turning a light off that is already off causes no side effects. It would be possible to only close each cover if it was not already closed. Let me know if you would really want, and need, to do that.

Suncalc https://www.suncalc.org/ :slight_smile: Show where sun elevation and azimuth.

The difference is only a few seconds.

I need something in the sense:
If current_position> 0, set_cover_position = 0 else do nothing and call tilt.

Tilt template is great and no need change.

  - data:
      entity_id: cover.mateo_2
    service: cover.set_cover_position
    position: '0'
  - data_template:
      entity_id: cover.mateo_2
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position

Ah. Well, that doesnā€™t necessarily agree with what HAā€™s sun.sun entity thinks. And since sun.sun is what is driving this automation, you should be looking at that.

These are computes weā€™re talking about. Seconds is a long time! :wink: You may need to change state_attr('sun.sun', 'elevation') to trigger.to_state.attributes.elevation (and same thing for azimuth) to make sure the condition template, and each step of the automation action, use the same value (since it is possible, although maybe unlikely, for sun.sun to change between the time the automation is triggered and the last steps of the action complete.) But, for now, I wouldnā€™t worry about it.

Ok. Iā€™m running out of time now, but Iā€™ll get back to you with a suggestion hopefully later today. If not, then probably Monday.

Maybe i do it! :slight_smile: It works but I just had to add unnecessary cover.stop_cover because without elseif it did not work and I could not leave it blank.

  action:
  - entity_id: cover.francouzke_okno_2
    service_template: '{% if is_state("cover.francouzke_okno_2", "open") %}cover.close_cover{% else %}cover.stop_cover{% endif %}'
  - data_template:
      entity_id: cover.francouzke_okno_2
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position
1 Like

Dear all,

Iā€™m new in Home Assistant and I have a lot of problems to understand the programing languaje used for automationsā€¦ Iā€™m engineer and i know C languaje, assember, etc, but with home assistant languaje I have a lot of problems.

I have an eedomus controller, and my roller shutter fibaro to control my awning use with a script in php that I make. I want to down the awning according with the sun elevation, and additionally I want to put the awning in Manual, Semi Automatic, and Automatic operation. Then there are a lot of conditions, but in php is very easy with a switch/case instruction. The scipt is:

<?php
function sdk_MueveToldo ($equipo, $data )
{
	setValue($equipo, $data);
};

$Toldo = 1090079;
$ModoToldo = 1092530;
$AlarmaToldoCompleta = getValue("1092379");
$ToldoModo = getValue($ModoToldo);
$azimut = getValue('915991');
$inclinacion = getValue('915992');
$azimut_p1 = getValue('1092378');
$azimut_p2 = getValue('1092540');
$inclinacion_p1 = getValue('1092547');
$inclinacion_p2 = getValue('1092549');
$PosicionToldo = getValue($Toldo);

//Todas las instrucciones con echo son para tareas de depruaciƃĀ³n
// echo "AlarmaToldoCompleta=";
// echo $AlarmaToldoCompleta['value'];
// echo "  PosicionToldo=";
// echo $PosicionToldo['value'];
// echo "  ToldoModo=";
// echo $ToldoModo['change'];

// Las instrucciones a continuaciƃĀ³n miran el tiempo pasado entre el ƃĀŗltimo cambio del modo de funcionamiento del toldo
// Solo se evalua en el caso del case 0 que es cuando se ha activado el modo manual.
// Si el modo manual se activa mientras se vuelve a modo automƃĀ”tico pasadas 2 horas.

$Today = date('Y-m-d H:i:s');
$Dif_seconds = strtotime($Today)-strtotime($ToldoModo['change']);
$Dif_minuts = $Dif_seconds/60;
$Dif_hours = $Dif_minuts/60;


if ($AlarmaToldoCompleta['value'] == 0) {
	switch ($ToldoModo['value'])
	{ 
		case 0:    //Modo Manual
			if ($PosicionToldo['value'] != 100)
			{
				if ($Dif_hours > 2) setValue($ModoToldo, '2');
				else break;
			}
			else break;
		case 1:    //Modo Semiautomatico. Se cierra el toldo al 100% a partir de un azimut y elevaciƃĀ³n del sol. Y luego se abre al 100% cuando no se cumplen esas condiciones
			if (($azimut['value'] > $azimut_p1['value']) && ($azimut['value'] < $azimut_p2['value']) && ($inclinacion['value'] < $inclinacion_p1['value'] ) && ($inclinacion['value'] > $inclinacion_p2['value'])) sdk_MueveToldo($Toldo,0);
			else sdk_MueveToldo($Toldo,100);
		case 2:   //Modo totalmente automƃĀ”tico. Se va abriendo dependiendo de la evoluciƃĀ³n del azimut y elevaciƃĀ³n del toldo.
			

			if (($azimut['value'] > $azimut_p1['value']) && ($azimut['value'] < $azimut_p2['value']) && ($inclinacion['value'] < $inclinacion_p1['value'] ) && ($inclinacion['value'] > $inclinacion_p2['value']))
			{
				if (($inclinacion['value'] <= $inclinacion_p1['value']) && ($inclinacion['value'] > ($inclinacion_p1['value'] - 12.4))) sdk_MueveToldo($Toldo,70);
				if (($inclinacion['value'] < ($inclinacion_p1['value'] - 12.4+0.1)) && ($inclinacion['value'] > ($inclinacion_p1['value'] - 24.8))) sdk_MueveToldo($Toldo,60);
				if (($inclinacion['value'] < ($inclinacion_p1['value'] - 24.8+0.1)) && ($inclinacion['value'] > ($inclinacion_p1['value'] - 37.2))) sdk_MueveToldo($Toldo,20);
				if (($inclinacion['value'] < ($inclinacion_p1['value'] - 37.2+0.1)) && ($inclinacion['value'] > ($inclinacion_p1['value'] - 49.6))) sdk_MueveToldo($Toldo,0);
				if (($inclinacion['value'] < ($inclinacion_p1['value'] - 49.6+0.1)) && ($inclinacion['value'] > ($inclinacion_p1['value'] - 62))) sdk_MueveToldo($Toldo,0);
			}
			else
			{
				// echo "14";
				sdk_MueveToldo($Toldo,100);
			}
	}
}
else
{
	if ($PosicionToldo['value'] != 100) sdk_MueveToldo($Toldo,100);
}

?>

How I can develop in Home assistant something similar?

Hi after years. :slight_smile:
I have a problem that started to make me angry lately and you helped me a lot and came up with a beautiful code. This code has become angry and I canā€™t handle it.
Can you help me?

I started getting an error in HA.

Invalid config for [automation]: Unexpected value for condition: 'None'. Expected numeric_state, state, sun, template, time, zone, and, or, not, device @ data['condition'][3]. Got None required key not provided @ data['action']. Got None. (See /config/configuration.yaml, line 287).

This refers to this code. Where could the problem be?

- id: '1546263622138'
  alias: Žaluzie (zatĆ”hnout JV) - teplota/slunce/azimut
  trigger:
    platform: state
    entity_id: sun.sun
  condition:
  - condition: template
    value_template: '{{ states.sensor.teplota_obyvak.state > states.sensor.topeni.state
      }}'
  - above: 799
    below: 802
    condition: numeric_state
    entity_id: sensor.openweathermap_weather_code
  - condition: template
    value_template: "{% if trigger.from_state is not none %}\n  {% set old_el = trigger.from_state.attributes.elevation\
      \ %}\n{% else %}\n  {% set old_el = 0 %}\n{% endif %} {% set el = state_attr('sun.sun',\
      \ 'elevation')|float(0) %} {% set az = state_attr('sun.sun', 'azimuth')|float(360)\
      \ %} {{ el > 4 and el < 51 and az < 224 and (el//1 - old_el//1)|abs > 0 }}\n"
  action:
  - entity_id: cover.francouzke_okno
    service_template: '{% if is_state("cover.francouzke_okno", "open") %}cover.close_cover{%
      else %}cover.stop_cover{% endif %}'
  - data_template:
      entity_id: cover.francouzke_okno
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position
  - data:
      message: Nastavuji JV okna na zĆ”kladě teploty, slunce a azimutu
    service: notify.domaci_asistent
  - entity_id: cover.navstevnicky_pokoj
    service_template: '{% if is_state("cover.navstevnicky_pokoj", "open") %}cover.close_cover{%
      else %}cover.stop_cover{% endif %}'
  - data_template:
      entity_id: cover.navstevnicky_pokoj
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position
  - entity_id: cover.mateo
    service_template: '{% if is_state("cover.mateo", "open") %}cover.close_cover{%
      else %}cover.stop_cover{% endif %}'
  - data_template:
      entity_id: cover.mateo
      tilt_position: '{{ (states.sun.sun.attributes.elevation + 40)|int }}'
    service: cover.set_cover_tilt_position

I suspect the error is from another automation. The error indicates the problem is with the fourth condition (they are zero indexed, so 3 means the 4th), but the automation you posted only has three conditions.

Hi, today I modified a bit the automation thanks to new options (at least they are new for me :smiley: ). Now more can run in parallel in one automation. I am years grateful for the help, which conveys me less overheated house and more light.
The code first checks the weather, peeks to see if the blinds are open, if so it tilts according to the sun, if not it pulls the blinds down and waits for them to close, then adjusts the tilt according to the sun.
Iā€™m making it available to the community. :slight_smile:

alias: Žaluzie SZ slunce
description: ""
trigger:
  - platform: state
    entity_id: sun.sun
condition:
  - above: 799
    below: 802
    condition: numeric_state
    entity_id: sensor.openweathermap_weather_code
    enabled: true
  - condition: template
    value_template: >
      {% if trigger.from_state is not none %}
        {% set old_el = trigger.from_state.attributes.elevation %}
      {% else %}
        {% set old_el = 0 %}
      {% endif %} {% set el = state_attr('sun.sun', 'elevation')|float(0) %} {%
      set az = state_attr('sun.sun', 'azimuth')|float(360) %} {{ el > 10 and el
      < 59 and az > 160 and az < 305 and (el//1 - old_el//1)|abs > 0 }}
action:
  - parallel:
      - choose:
          - conditions:
              - condition: state
                entity_id: cover.dilna
                state: open
            sequence:
              - service: cover.close_cover
                data: {}
                target:
                  entity_id: cover.dilna
              - wait_for_trigger:
                  - platform: state
                    entity_id:
                      - cover.dilna
                    to: closed
              - data_template:
                  entity_id: cover.dilna
                  tilt_position: "{{ (states.sun.sun.attributes.elevation + 40)|int }}"
                service: cover.set_cover_tilt_position
          - conditions:
              - condition: state
                entity_id: cover.dilna
                state: closed
            sequence:
              - data_template:
                  entity_id: cover.dilna
                  tilt_position: "{{ (states.sun.sun.attributes.elevation + 40)|int }}"
                service: cover.set_cover_tilt_position
      - choose:
          - conditions:
              - condition: state
                entity_id: cover.koupelna
                state: open
            sequence:
              - service: cover.close_cover
                data: {}
                target:
                  entity_id: cover.koupelna
              - wait_for_trigger:
                  - platform: state
                    entity_id:
                      - cover.koupelna
                    to: closed
              - data_template:
                  entity_id: cover.koupelna
                  tilt_position: "{{ (states.sun.sun.attributes.elevation + 40)|int }}"
                service: cover.set_cover_tilt_position
          - conditions:
              - condition: state
                entity_id: cover.koupelna
                state: closed
            sequence:
              - data_template:
                  entity_id: cover.koupelna
                  tilt_position: "{{ (states.sun.sun.attributes.elevation + 40)|int }}"
                service: cover.set_cover_tilt_position
mode: single