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 %}