Template Sensor for Cover Position

Hello,

I have a problem with the control of my blinds. I want to control the slats depending on the sun elevation. For this I have created a template sensor in configuration.yaml:

sensor:
  - platform: template
    sensors:
      reversierung:
        friendly_name: "Jalousie Reversierung"
        value_template: >
            {% if (state_attr('sun.sun', 'elevation')*50/35)|int>50 %}
                50
            {% elif (state_attr('sun.sun', 'elevation')*50/35)|int<0 %}
                 0
            {% else %}
                 {{ (state_attr('sun.sun', 'elevation')*50/35)|int }} 
            {% endif %}

I want to use the value in an Automation like this:

domain: cover
entity_id: cover.rs_essen_2
type: set_position
position: "{{sensor.reversierung}}"

When I want to specify the value of the sensor as cover position in an automation I get an error message. I think something like “wrong data type”. Is there a better way to achieve what I want? Where is my error?

Thanks in advance!

If you like can replace this template:

        value_template: >
            {% if (state_attr('sun.sun', 'elevation')*50/35)|int>50 %}
                50
            {% elif (state_attr('sun.sun', 'elevation')*50/35)|int<0 %}
                 0
            {% else %}
                 {{ (state_attr('sun.sun', 'elevation')*50/35)|int }} 
            {% endif %}

With this more compact version:

        value_template: "{{ ( [ 0, state_attr('sun.sun', 'elevation')*50/35)|int, 50 ]|sort )[1] }}"

This limits your template to be from 0 to 50 by sorting the list by magnitude and picking the middle value.

For your automation issue:

- service: cover.set_cover_position
  target: 
    entity_id: cover.rs_essen_2
  data:
    position: "{{ states('sensor.reversierung') }}"

See: https://www.home-assistant.io/docs/configuration/templating/#states

2 Likes