Help with a template sensor

I have a similar problem and I don‘t get it working. The sensor shall switch to on when all values meet for 5 seconds. There is no syntax error but it doesn‘t work :frowning:


- platform: template
  sensors:
    s_coffee_ready:
      friendly_name: "Coffeemaker is ready"
      icon_template: >
        {% if is_state('binary_sensor.s_coffee_ready', 'off') %}
          mdi:coffee-off-outline
        {% elif is_state('binary_sensor.s_coffee_ready', 'on') %}
          mdi:coffee
        {% endif %}
      delay_on: "00:00:05"
      value_template: >
        {{ is_state('switch.coffeemaker', 'on') and
          is_state('input_boolean.coffeemaker_nowater', 'off') and
          is_state('timer.coffee_check', 'idle') and
          is_state('sensor.coffeemaker_electric_consumed_w', '< 15') }}

Yeah, that’s not right :wink:

You’re checking to see if the state of sensor.coffeemaker_electric_consumed_w is the string < 15. Maybe you were wanting

          states('sensor.coffeemaker_electric_consumed_w')|float < 15 }}

Hmm, OK this makes sense :wink: I changed the code but unfortunately now the sensor is not available any longer. It is grey/deactivated and I got the following log:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:73
Integration: Template (documentation, issues)
First occurred: 06:15:16 (2 occurrences)
Last logged: 06:15:41

TemplateError(‘AttributeError: ‘bool’ object has no attribute ‘lower’’) while processing template ‘Template(“{{ is_state(‘switch.coffeemaker’, ‘on’) and is_state(‘input_boolean.coffeemaker_nowater’, ‘off’) and is_state(‘timer.coffee_check’, ‘idle’) and states(‘sensor.coffeemaker_electric_consumed_w’|float < 15) }}”)’ for attribute ‘_state’ in entity ‘binary_sensor.s_coffee_ready’

It seems I still do anything wrong ^^

Meanwhile I got this additional:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1254
First occurred: 06:01:34 (7 occurrences)
Last logged: 06:02:28

Template warning: ‘float’ got invalid input ‘sensor.coffeemaker_electric_consumed_w’ when compiling template ‘{{ is_state(‘switch.coffeemaker’, ‘on’) and is_state(‘input_boolean.coffeemaker_nowater’, ‘off’) and is_state(‘timer.coffee_check’, ‘idle’) and states(‘sensor.coffeemaker_electric_consumed_w’|float < 15) }}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1

Further information:

  • it become unavailable as soon as the timer (is_state(‘timer.coffee_check’, ‘idle’)) is idle
  • it becomes available again when switch.coffeemaker changes to off

Ah yes, there’s changes to templates that I haven’t fully digested yet. This post explains it in great depth, and I suspect you need:

states('sensor.coffeemaker_electric_consumed_w')|float(0) < 15
1 Like

Thanks, I didn’t realize that change.
I will give it a try :+1:

Unfortunately it becomes still just unavalable. As soon as some or one of the conditions become true :frowning:

if you have the code formatted properly it’s impossible for that logic to go unavailable. So you must have made another syntax error. Please post the full template again.

1 Like

I found MY mistake.

This was my code:

          states('sensor.coffeemaker_electric_consumed_w' | float(0) < 15) }}

The problem was the bracket behind the 15. It should be behind the sensor:

          states('sensor.coffeemaker_electric_consumed_w') | float(0) < 15 }}

Now it works :+1:

Thank you all for helping. Special thanks to @Tinkerer for the hint with the change of template what brought me to a solution for some other problems with sensor templates :wink:

1 Like

I’m having a different kind of problem. I’m trying to basically set this based on if the power is above or below a threshold:

- platform: template
  sensors:
    washing_machine_in_use:
      friendly_name: "Washing Machine"
      delay_on: "0:00:30"
      delay_off: "0:00:30"
      value_template: >
        {{ states('sensor.washing_machine_plug_power')|float > 1 }}

It keeps saying though delay_on and delay_off are invalid options but I don’t understand why.

Try:

- platform: template
  sensors:
    washing_machine_in_use:
      friendly_name: "Washing Machine"
      delay_on: "0:00:30"
      delay_off: "0:00:30"
      value_template: >
        {{ states('sensor.washing_machine_plug_power')|float(0)  > 1 }}

Thanks, but I get the exact same error.

Invalid config for [sensor.template]: [delay_on] is an invalid option for [sensor.template].

Maybe this helps? Template error: [delay_off] is an invalid option for [sensor.template]

Learn something new everyday. Moving my original code to the binary_sensors file did the trick…I guess I just assumed as long as it was “somewhere” in the config file chain it would have worked (and all my other sensors are in sensors.yaml).

hello,
i have a problem with this one in tempate:

 - sensor:
    - name: Zum Victron
      unique_id: zum_victron
      state_class: measurement
      icon: mdi:transmission-tower
      unit_of_measurement: W
      device_class: power 
      state: >
        {% if is_number (states('sensor.victron_inverter_ac_in_l1_w')) and states('sensor.victron_inverter_ac_in_l1_w') | float(0) > 0 %}
            {{(states('sensor.victron_inverter_ac_in_l1_w') | float(0))*+1 | round (3) }}
        {% else %}
            {{(states (0.0) | float(0)) | round(3) }}
        {% endif %}  

become this error:

TemplateError('AttributeError: 'float' object has no attribute 'lower'') while processing template 'Template<template=({% if is_number (states('sensor.victron_inverter_ac_in_l1_w')) and states('sensor.victron_inverter_ac_in_l1_w') | float(0) > 0 %} {{(states('sensor.victron_inverter_ac_in_l1_w') | float(0))*+1 | round (3) }} {% else %} {{(states (0.0) | float(0)) | round(3) }} {% endif %}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.zum_victron'

and this one:

Logger: homeassistant.helpers.event
Source: helpers/template.py:571
First occurred: 20:52:48 (1 occurrences)
Last logged: 20:52:48

Error while processing template: Template<template=({% if is_number (states('sensor.victron_inverter_ac_in_l1_w')) and states('sensor.victron_inverter_ac_in_l1_w') | float(0) > 0 %} {{(states('sensor.victron_inverter_ac_in_l1_w') | float(0))*+1 | round (3) }} {% else %} {{(states (0.0) | float(0)) | round(3) }} {% endif %}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 569, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2247, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 4, in top-level template code
  File "/usr/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 872, in __call__
    state = _get_state(self._hass, entity_id)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1126, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1586, in get
    return self._states_data.get(entity_id.lower())
                                 ^^^^^^^^^^^^^^^
AttributeError: 'float' object has no attribute 'lower'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 695, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 571, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: AttributeError: 'float' object has no attribute 'lower'

The value from the sensor comes positive as well as negative.
That’s why I want to split it, because the energy dashboard wants to have one in and one out for the battery

pleas help thanks

The states() function expects an entity ID as an argument. 0.0 is not an entity ID.

OK, I understand.
But how could I solve this then? It should only output a value when it is over 0.0

should I create a helper with 0.0?

I currently have it like this in the template yaml:

 - sensor:
    - name: Vom Victron
      unique_id: vom_victron
      state_class: measurement
      icon: mdi:transmission-tower
      unit_of_measurement: W
      device_class: power 
      state: >
        {% if is_number (states('sensor.victron_inverter_ac_in_l1_w')) and states('sensor.victron_inverter_ac_in_l1_w') | float(0) < 0 %}
            {{(states('sensor.victron_inverter_ac_in_l1_w') | float(0))*-1 | round (3) }}
        {% else %}
            {{(states (0.0) | float(0)) | round(3) }}
        {% endif %}   
        
 - sensor:
    - name: Zum Victron
      unique_id: zum_victron
      state_class: measurement
      icon: mdi:transmission-tower
      unit_of_measurement: W
      device_class: power 
      state: >
        {% if is_number (states('sensor.victron_inverter_ac_in_l1_w')) and states('sensor.victron_inverter_ac_in_l1_w') | float(0) > 0 %}
            {{(states('sensor.victron_inverter_ac_in_l1_w') | float(0))*+1 | round (3) }}
        {% else %}
            {{(states (0.0) | float(0)) | round(3) }}
        {% endif %} 

What is the purpose of multiplying the sensor’s value by one?

Just put 0 in the template.

        {% else %}
          0
        {% endif %}   
1 Like

I only found one example online and just swapped the sensor values.

The point is that the Multi brings a minus value when discharging, for example (-300 watts).

When charging (300 watts) without minus.

The energy dasbord requires 2 inputs with a battery. The one for the battery out. and the one for the battery input.

That’s why I wanted the - and + values ​​each in their own sensor value.
energy dashbord