Template sensor struggling again

I want to have a sensor:

wasmachine_percentage:
        friendly_name: "Washmachine percentage ready"
        device_class: timestamp
        value_template: >
          {% if states('sensor.wasmachine_program_progress', 'unavailable') %}
              {percentage}
            {% else %}
              "0 %"
            {% endif %}

Most of the time the sensor sensor.wasmachine_program_progress shows unavailable and when it runs it show the percentage.

I want to show “something” when unavailable is there. So I am struggling to get the value template syntax to get the status and translate that to “something” when it is unvailable.

Your condition should be:

is_state('sensor.wasmachine_program_progress', 'unavailable')

Bit I’d suggest you use an INT or FLOAT with default 0 like this:

{{ states("sensor.wasmachine_program_progress")|int(default=0) }}

Why have you configured that sensor as a Timestamp sensor?

device_class: timestamp

The value_template you have is clearly not providing a date and time value (which is required for a Timestamp sensor).

exactly the same way you grabbed the state in that other thread…

What else for “percentage”?

We can start again, that’s why I called this topic a “struggle”. I do totally not get the logics behind getting a template sensor to work. I have REALLY tried (I am not going to get into that direction again).
I think I do not get the syntax/logics on how this language works and I must use it in order to “get what I want”. Every time I face such a thing I google around and copy-past stuff until it works.

So now I have this:

washmachine_percentage:
        friendly_name: "Washmachine percentage ready"
        device_class: #<What do I need for percentage>
        value_template: >
          {% is_state('sensor.wasmachine_program_progress', 'unavailable') %}
              "0 %" #<--- here I want the value when unavailable shows
            {% else %}
              state #<--- here I want the percentage to show when there is
            {% endif %}

help :-)?

EDIT:
Now have this rendering “nothing” (blank) when unavailable (currently) as I want. But I wonder if it will return the actual sensor output (percentage) when running:

wasmachine_percentage:
        friendly_name: "Washmachine percentage ready"
        value_template: >
          {% if is_state('sensor.wasmachine_program_progress', 'unavailable') %}
             
            {% else %}
              {state}
            {% endif %}

I want to overcome this in a picture entity card:
image

EDIT2:
would this work:

wasmachine_percentage:
        friendly_name: "Washmachine percentage ready"
        value_template: >
          {% if is_state('sensor.wasmachine_program_progress', 'unavailable') %}
              
            {% else %}
              {{ state_attr('sensor.wasmachine_program_progress', 'unit_of_measurement') }}
            {% endif %}

So your answer is you don’t know which device_class to select for a percentage value so you chose timestamp?

I suggest you review the documentation for a sensor’s device_class.

battery or humidity should be %? no that’s logical…:slight_smile: ? I can also leave this empty?

That’s my point; none of the three types that report the value as a percentage (battery, humidity, power_factor) are a perfect fit for your application. Don’t use the device_class option for this sensor (unless you don’t mind having it displayed with a battery/humidity/power_factor icon).

1 Like

try this:

washmachine_percentage:
  friendly_name: "Washmachine percentage ready"
  unit_of_measurement: '%'
  value_template: >
    {% if is_state('sensor.wasmachine_program_progress', 'unavailable') %}
      0
    {% else %}
      {{ states('sensor.wasmachine_program_progress') }}
    {% endif %}

Would the use of an availabilty_template help in this situation?

I don’t know for sure. I never use them and I’m not sure what the state of the template sensor would be if the original entity was unavailable. I would think it would say ‘unavailable’ which is what the OP is trying to avoid.