How to prevent unavailable in template sensors?

Hi,

I got some ESP(home) devices that are offline when the “hacked” device is off. Take my Jura coffee machine as example. I attached an ESP to it and this counts the amount of coffee that we made.
afbeelding

So now I want to display the total amount of coffee in a nice graphic. only the sensor turns tot unavailable when the device is offline and that gives “NaN” on my lovelace items. So I want to prevent this. I read/googled a lot on this, tried to use the availability_template but that don’t work for this. and couldn’t find anything with previous_state or last_state on a template sensor…

Any other way that I can do this?

Sensor template:

- platform: template
  sensors:
    jura_coffee_total:
      friendly_name: Jura total
      value_template: "{{ (states('sensor.coffees_made') | int()) + (states('sensor.double_espressos_made') | int())  + (states('sensor.double_coffees_made') | int())  + (states('sensor.single_espressos_made') | int()) }}"
      availability_template:   >- {%- if not is_state("binary_sensor.coffee_machine_status", "off") %}  true {%- endif %}

You could add: ‘| default(0)’ so that it at least shows a value or something like

{% set var1 = states(... %} 
{{ var1 if var1 else 'off' }}

default 0 sets the value to 0 if the device is offline. and that will also affect my total counter. it’s an increasing number (of made cofffee :D) so pulling it to 0 is not the way…

OK…just trying to understand the issue without spending a lot of time :slight_smile: … there is also something like ‘input_number’ which you can update and use its state

Input Number - Home Assistant (home-assistant.io)

Another thought is to address this from the graph/card by excluding non-numeric values…I have seen this somewhere. apex/plotly allow to mod on the input

using availability templates make it so it goes unavailable.

Are you trying to see a graph? If yes, then just add a unit of measurement to the template sensor and it will be numerical.

secondly, you’re using a multiline template without using multilines.

lastly, if you only want it to show when it’s on…

availability_template: >-
  {{ is_state("binary_sensor.coffee_machine_status", "on") }}