Updating Templates with the new default values in 2021.10.x

I was hoping to avoid adding defaults by using availability for all the templates. If I can’t than I definitely will add defaults.

Not sure where I got this idea but I cant find any relevant PR. So maybe I dreamt it.

I’m 99% sure it already went through but it only affects state. Not the other templates. his icon template is causing the problem as that’s the only string to int conversion that will fail on startup. The others aren’t conversions that would result in a failure.

ofc simply preventing that using:

        icon: >
          {% set state = states('input_number.intercom_volume') %}
          {% if state == '0.0' %} mdi:volume-off
          {% elif state <= '0.3' %} mdi:volume-low
          {% elif state <= '0.6'%} mdi:volume-medium
          {% else %} mdi:volume-high
          {% endif %}

is also possible :wink: though it might be luck this works considering this is comparing strings…

this is better in that regard:

          {% set state = states('input_number.intercom_volume')|float(0) %}
          {% if state == 0.0 %} mdi:volume-off
          {% elif state <= 0.3 %} mdi:volume-low
          {% elif state <= 0.6 %} mdi:volume-medium
          {% else %} mdi:volume-high
          {% endif %}

TIL templating is hard work. :wink:

This explains a lot. I’ll consider some other code for the icon.

That’s a nice suggestion.

I’ve discovered that templating the icon attribute is also determined by the availability attribute. When attribute is False, the sensor state is ‘unavailable’, and the sensor icon is not templated. The icon attribute will not be set.

Availability added:

Availability removed:

You can reproduce this with my below code and look at the developer tools with the availability template added or removed.

I’ve updated my code to this:

template:
  - sensor:
      - name: Living TV Volume Level
        unique_id: 655f97da-54dc-40d2-8de1-db1b9cc66bf3
        unit_of_measurement: "%"
        availability: >-
          {{ state_attr('media_player.living_tv','volume_level') is not none }}
        state: >-
            {{ (state_attr('media_player.living_tv','volume_level') 
              | float(0) * 100) | int }}
        icon: >-
          {% set state = states('sensor.living_tv_volume_level') | float(0) %}
          {% if state == 0.0 %} mdi:volume-off
          {% elif state <= 0.3 %} mdi:volume-low
          {% elif state <= 0.6 %} mdi:volume-medium
          {% else %} mdi:volume-high
          {% endif %}

Noice.

That has the effect of drawing the eye to the deeper problem. Don’t fuck with it.

This is awesome, but had to be pointed out to me by someone else.
Perhaps melding this into the Official Docs AND melding your sample availability example into the related docs for that would be more helpful. (I had NO CLUE how to use that before this article)
No offense, I use the community pages here but I always look first at the official Docs and put more weight in what they say rather than random guessers. I do take what you say an a few others as 99.5% correct, but there are lots of other advise in here that, well, not so much… That’s why I look to the Official Docs first and would not have found this without Thanasis’s help. Wouldn’t have known to look…

I am getting an error when using a value template for an automation to control air conditioning. Hoping someone can help fix up my code :slight_smile: It doesn’t specify the error, just says “error while processing template”.

Basically it triggers an automation when a temperature sensor goes above or below a certain value, in this case above 24 degrees.

trigger:
  - platform: template
    value_template: "{{ states('sensor.girls_sensor_temperature') | round(0,0) | int(0) >= 24 }}"
1 Like

The second argument for round is method. I.e. rounding to the floor, cieling, or just a normal round. You need to use round(0, default=0) if you don’t plan on providing a method for the round.

Also, if you provide a default for the round, you don’t need a default for the int.

4 Likes

Worked perfectly, thanks so much! Have a few other errors to clear, will give it a go and come back if I need more expert advice :slight_smile:

I have an automation which relies on it not being triggered within “x” amount of seconds (a sort of de-bounce if you wish) which works great apart from if the automation has never been triggered.

  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(state_attr('automation.cameras_person_detected_in_zone',
      'last_triggered') | default(0)) | int > 5)}}

I get this error:

ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '>-
      {{ (as_timestamp(now()) -
      as_timestamp(state_attr('automation.cameras_person_detected_in_zone',
      'last_triggered') | default(0)) | int > 5)}}' but no default was specified

I’ve searched high and low but I can’t figure this out…

If that automation runs in single mode you can achieve the same effect by deleting that condition and putting this as your last action:

- delay: 5

Otherwise try this:

  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) -
      as_timestamp(state_attr('automation.cameras_person_detected_in_zone',
      'last_triggered') | default(0), 0) | int > 5)}}
2 Likes

Thanks! I didn’t think of the delay option, the template works too.

1 Like

How do you define a default in an if/then statement? I have the following set up but it errors on startup until the entity it’s checking against is available.

trigger:
  - platform: state
    entity_id: sensor.coffee_maker_plug_power
binary_sensor:
  name: Coffee Maker State Power
  state: >-
    {% if (states('sensor.coffee_maker_plug_power')|int >= 100) %}
      Brewing
    {% else %}
      Off
    {% endif %}
  icon: mdi:coffee-maker-outline
  unique_id: 4xnwmtKQ4tm3LEgv4fcM9jPcJKkCf57

gets me an error like this.

Logger: homeassistant.components.template.binary_sensor
Source: components/template/trigger_entity.py:180
Integration: Template (documentation, issues)
First occurred: December 9, 2022 at 11:35:23 AM (43 occurrences)
Last logged: 8:39:56 PM

Error rendering state template for binary_sensor.coffee_maker_state_power: ValueError: Template error: int got invalid input 'unavailable' when rendering template '{% if (states('sensor.coffee_maker_plug_power')|int >= 100) %} Brewing {% else %} Off {% endif %}' but no default was specified

Since I have custom states, I can’t just use is_state, and I also wouldn’t be able to add an elseif in there. How would you go about setting a default in a configuration like this?

You don’t. You define it for the |int filter.

  state: >
    {% if (states('sensor.coffee_maker_plug_power')|int(0) >= 100) %}

Also please don’t demand people respond to you by tagging them.

1 Like

not demanding, simply asking since petro started the topic but gotcha.

And thank you for the answer Tom thats very helpful.

sorry, one more followup…what would I do if the original sensor gave me the output as an integer already and I didn’t have to pipe it?

It didn’t. States are always strings.

1 Like

Hi Guys

I’m using the following template to catch the average price of energy. Have a problem from a long time. Periodically, one or more sensors report unknow state for few seconds during the scraping phase. This broke other calculations I do with this value.
The question is: Is there a way to return a “last good value” instead of unknow?

Thanks

    value_template: "{{ ((states('sensor.mgp_pun_ore_1') |float
                      + states('sensor.mgp_pun_ore_2')|float 
                      + states('sensor.mgp_pun_ore_3')|float 
                      + states('sensor.mgp_pun_ore_4')|float
                      + states('sensor.mgp_pun_ore_5')|float 
                      + states('sensor.mgp_pun_ore_6')|float 
                      + states('sensor.mgp_pun_ore_7')|float 
                      + states('sensor.mgp_pun_ore_8')|float 
                      + states('sensor.mgp_pun_ore_9')|float 
                      + states('sensor.mgp_pun_ore_10')|float 
                      + states('sensor.mgp_pun_ore_11')|float 
                      + states('sensor.mgp_pun_ore_12')|float 
                      + states('sensor.mgp_pun_ore_13')|float
                      + states('sensor.mgp_pun_ore_14')|float 
                      + states('sensor.mgp_pun_ore_15')|float 
                      + states('sensor.mgp_pun_ore_16')|float
                      + states('sensor.mgp_pun_ore_17')|float 
                      + states('sensor.mgp_pun_ore_18')|float 
                      + states('sensor.mgp_pun_ore_19')|float 
                      + states('sensor.mgp_pun_ore_20')|float 
                      + states('sensor.mgp_pun_ore_21')|float 
                      + states('sensor.mgp_pun_ore_22')|float 
                      + states('sensor.mgp_pun_ore_23')|float 
                      + states('sensor.mgp_pun_ore_24')|float)/24) |round(3) }}"