Need help using created sensor [SOLVED]

Hello everyone. I have some motorized curtains. I want to know the position of them, however there isn’t an entity for them that has this info. There is a state attribute that will change with position (0=closed 100=open).

With help I created the following template

template:
  - sensor:
    - name: "Curtain Inside 2 Position"
      state_class: measurement
      state: "{{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}"
      unit_of_measurement: "%"

So at this point I am not sure how to get the position from this created sensor.

I want to create an automation that checks to see where the curtain is after a command is issued to change the position. That way if the curtain does not move or there is a problem, I can have HA issue the command again or notify me that there was a problem.

in developer tools, check for a sensor.curtain_inside_2_position

1 Like

Specifically, Developer Tools → States. If you don’t see it:

Did you restart home assistant after creating the template sensor?

As it is the first time you have used the template integration you need to restart.

After that you can use the reload option for new template sensors.

1 Like

Thank you both for the assistance. Looks like this is working as expected. Now I need to learn how to make an automation to check after the command to raise or lower the curtain happens so that if there is a problem it will let me know.

One thing that I am noticing is that when the curtain is moving it will report either 0% or 100% (down 0%, and 100% up). Wondering if there is a way to either not report any change (just hold the previous value) or not report anything (null). Thank you once again.

Sure (holds previous value while opening or closing):

template:
  - sensor:
    - name: "Curtain Inside 2 Position"
      state_class: measurement
      state: >
        {% if states('cover.curtaininside2') in ['open', 'closed']  %}
          {{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}
        {% else %}
          {{ this.state }}
      unit_of_measurement: "%"

Thank you tom. I replaced it in the config and it only displays 0% as I change the height of the curtain.

image
Do these have to match?

No, the syntax is correct, except that I forgot to end my if statement. Try this:

template:
  - sensor:
    - name: "Curtain Inside 2 Position"
      state_class: measurement
      state: >
        {% if states('cover.curtaininside2') in ['open', 'closed']  %}
          {{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}
        {% else %}
          {{ this.state }}
        {% endif %}
      unit_of_measurement: "%"

Better for sure in that it displays the value when stopping, but still shows 0% when moving down and 100% when moving up. I am understanding the If then statement part but not the else part. Maybe the this.state when moving down is 0%?

Yes that was one of your options:

If it started at 100% or 0% that is what it will be while moving. this.state is the previous state value before it started opening or closing.

We can not use anything other than a number as it has a unit of measurement.

If you want to ditch the unit then you can do this:

template:
  - sensor:
    - name: "Curtain Inside 2 Position"
      state_class: measurement
      state: >
        {% if states('cover.curtaininside2') in ['open', 'closed']  %}
          {{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}' %'
        {% else %}
          moving
        {% endif %}

Note that the unit (%) is now part of the state making the state a string that you can not use in a line graph or calculations in other templates. You can remove it altogether with:

      state: >
        {% if states('cover.curtaininside2') in ['open', 'closed']  %}
          {{ state_attr('cover.curtaininside2', 'current_position')|float(0) }}
        {% else %}
          moving
        {% endif %}