Set Value to "0" if entity is not Available

Hey there,
I’m completly new to the automation stuff and home assistant.

I’m Using an standart Gauge to Display the CPU-Load, Temperatur and some other stuff from my computer. But when the Computer is turned off it just displays “Entity not available”

Instead of that i want the Card to display the Value 0.

Okay, I think I need to create an Template for that. I know it has to look something like that:

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: >
         {% if states('sensor.CPULoad') == "unavailable" %}
              {{states('"0"')}}
         {% else %}
            {{states('sensor.CPULoad')}}
         {% endif %}

Maybe someone can help or recommend an good tutorial for creating templates.
I think i don’t get the syntax right.

Thanks
Daniel

This entity id wont have capital letters in it. Entity ids are always lower-case.

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: "{{ 0 if states('sensor.cpuload') == 'unavailable' else states('sensor.cpuload') }}
1 Like

Thanks for the input :slight_smile:

But besides of that the code looks good?

is there a way to script in python? Because the yaml syntax is a littlebit strange :smiley:

Almost. If you don’t want to use the shorter config I posted yours should be:

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: >
         {% if states('sensor.cpuload') == 'unavailable' %}
           0
         {% else %}
           {{states('sensor.cpuload')}}
         {% endif %}

You could also just do this:

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: "{{ states('sensor.cpuload')|float }

The float filter will return 0 for strings.

Yes. Here are two ways:

or

1 Like

Hey, i finally tested the code :slight_smile:
the version with the “if” works perfect.
the shorter version won’t :frowning:

The short version is missing a “}” at the end:

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: "{{ states('sensor.cpuload') | float }}"

1 Like

Not sure what happened there. I actually had )}" originally and thought I had corrected it. I obviously stuffed the edit up somehow too. Thanks for pointing out he fix.

Again, thank you very much. Your templates helped me to understand the syntax a little bit more. I was even able to create an own script after your help :slight_smile:

1 Like

I noticed someone liked the solution above. However it is now out of date due to changes in the way home assistant deals with defaults. You should now use:

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: "{{ states('sensor.cpuload')|float(0) }}"

Or

template:
  - sensor:
      - name: "CPULoad"
        unit_of_measurement: "%"
        state: >
         {% if has_value('sensor.cpuload') %}
           {{ states('sensor.cpuload') }}
         {% else %}
           0
         {% endif %}
1 Like

Hello all, I have a similar question with a twist.

I have created a sensors.yaml file that contains some template sensors mostly for conversion purposes for my solar panel system. Needing to change value scales.

This is the particular yaml entry that i have:

- platform: template
  sensors:  
      
    convert_power_photovoltaics:
      unique_id: convert_power_photovoltaics
      friendly_name: "Power Photovoltaics W"
      value_template: "{{ states('sensor.solarnet_power_photovoltaics') | float * 1000 | round(2) }}"

As you can see this converts the sensor.solarnet_power_photovoltaics from KW to W.
However sometimes when there are prolonged periods of darkness (I live in the high north), the sensor becomes unavailable which messes up some calculations.

In the developer tools templating section, I followed an example from a previous post and it the result is 0

state: "{{ 0 if states('sensor.solarnet_power_photovoltaics') == 'unavailable' else states('sensor.solarnet_power_photovoltaics') }}

So my question is how can i integrate that in my existing code? Adding it as a line after gives me an error.

- platform: template
  sensors:  
      
    convert_power_photovoltaics:
      unique_id: convert_power_photovoltaics
      friendly_name: "Power Photovoltaics W"
      value_template: "{{ states('sensor.solarnet_power_photovoltaics') | float(0) * 1000 | round(2) }}"
2 Likes

Thanks @tom_l I’ll try that out and post my findings!

@tom_l Thanks! It works and did the trick!

1 Like

state: "{{ 0 if states('sensor.solarnet_power_photovoltaics') == 'unavailable' else states('sensor.solarnet_power_photovoltaics') }}

Hi @all.
What should the code look like if the sensor contains two variables and then a specific value should be displayed?

In my case, a “0” should be displayed for “unavailable” and a “0” should also be displayed for “-1”.

Here is my attempt, but it doesn’t work:

{{ "0" if states('sensor.aeg_waschtrockner_td1_timetoend') == '-1' or "0" if states('sensor.aeg_waschtrockner_td1_timetoend') == 'unavailable' else states('sensor.aeg_waschtrockner_td1_timetoend') }}

If the value is 0 you can just output the value, no need to test for 0 and replace it with 0. And you can replace the unavailable state with zero using a default value for a float filter so you really only have one test (if ‘-1’).

state: >
  {{ 0 if is_state('sensor.aeg_waschtrockner_td1_timetoend', '-1')
       else states('sensor.aeg_waschtrockner_td1_timetoend')|float(0) }}

OK thanks. It is working.

Now I have another challenge, this sensor displays text, but when the machine is switched off, then the value of the sensor is " " (no content)

How do I get the “no content” to display a defined text and when the machine is running, the text from the sensor?

Here is my attempt so far:

{{ "keine" if states('sensor.aeg_waschtrockner_td1_cyclephase') == '' or "keine" if states('sensor.aeg_waschtrockner_td1_cyclephase') == 'unavailable'  else states('sensor.aeg_waschtrockner_td1_cyclephase') }}

What does this return in the template editor when the sensor is on and off:

{{ has_value('sensor.aeg_waschtrockner_td1_timetoend') }}

off: false
on: true

Excellent.

Then you can do this:

{{ "keine" if not has_value('sensor.aeg_waschtrockner_td1_cyclephase') 
    else states('sensor.aeg_waschtrockner_td1_cyclephase') }}

has_value() will also catch the unavailable and unknown states.

It’s crazy…

This sensor has_value(‘sensor.aeg_waschtrockner_td1_timetoend’) shows when on: true and when off: false.

But this sensor sensor.aeg_waschtrockner_td1_cyclephase shows “true” when on and when off.

Could it be because the last sensor gives a “blank” when turned on and off?