home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.78k stars 30.88k forks source link

Some Template Sensors Broken since 0.115 #40221

Closed basdejong95 closed 3 years ago

basdejong95 commented 4 years ago

The problem

Some template sensors are not working anymore since 0.115 They were working fine in 0.114.4

{{(state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Received)')/1000000)|float|round(3)}}
{{(state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Sent)')/1000000)|float|round(3)}}

Values example: KBytes (Received) == 1330738980.2929688 KBytes (Sent) ==166514890.8388672

Environment

Problem-relevant configuration.yaml

sensor:
  - platform: template
  sensors:
      total_internet_received:
        friendly_name: Total Internet Received
        unit_of_measurement: GB
        value_template: "{{(state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Received)')/1000000)|float|round(3)}}"
      total_internet_sent:
        friendly_name: Total Internet Sent
        unit_of_measurement: GB
        value_template: "{{(state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Sent)')/1000000)|float|round(3)}}"

Traceback/Error logs

2020-09-17 21:38:10 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup
    result_info = async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 778, in async_track_template_result
    tracker.async_setup()
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup
    self._info[template] = template.async_render_to_info(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'
2020-09-17 21:38:10 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup
    result_info = async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 778, in async_track_template_result
    tracker.async_setup()
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup
    self._info[template] = template.async_render_to_info(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

Additional information

These templates are working fine in the developer tools template tester. However they are not when being used as a template sensor.

probot-home-assistant[bot] commented 4 years ago

Hey there @phracturedblue, @tetienne, mind taking a look at this issue as its been labeled with an integration (template) you are listed as a codeowner for? Thanks! (message by CodeOwnersMention)

tiagofreire-pt commented 4 years ago

@basdejong95 , could you test it this way?

{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Received)') | float )/1000000) | round(3) }}
{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Sent)') | float )/1000000) | round(3) }}
basdejong95 commented 4 years ago

@basdejong95 , could you test it this way?

{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Received)') | float )/1000000) | round(3) }}
{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Sent)') | float )/1000000) | round(3) }}

Hi,

That works indeed. Thanks for the solution! From what i understand Home assistant could not determine the datatype of the state attribute before dividing it by 1 million?

What's curious is that my solution worked in 0.114.4 but not in 0.115.0.

But it DOES work in 0.115.0 in the Developer tools template tester :)

tiagofreire-pt commented 4 years ago

@basdejong95 , could you test it this way?

{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Received)') | float )/1000000) | round(3) }}
{{ (( state_attr('binary_sensor.edgeos_interface_eth4', 'KBytes (Sent)') | float )/1000000) | round(3) }}

Hi,

That works indeed. Thanks for the solution! From what i understand Home assistant could not determine the datatype of the state attribute before dividing it by 1 million?

The output for that attribute from the state_attr() function seems to be an integer or undefined. The best practice is always to convert the data type before any operation with it, regarding math or string manipulations.

What's curious is that my solution worked in 0.114.4 but not in 0.115.0.

But it DOES work in 0.115.0 in the Developer tools template tester :)

Perhaps, the yaml interpreter could be more tolerant there than on the ambient where it runs the production code.

PierreScerri commented 4 years ago

I am experiencing the same issue. One group of template sensors is not loading after HA restart.

Hitting 'Reload Template Entities' brings them online.

In the YAML below, sensor.house_secure is working.

The other sensor.sdmxxxxxx sensors are not. The value templates of these sensors refer to sensors defined in Node-Red. (eg sensor.sdm120solar is defined in Node-red)

- platform: template
  sensors:
    house_secure:
      friendly_name: "House Secure"
      value_template: >-
        {{(states.cover.all_shutters.state == 'closed') and 
        (states.climate.gree_ac.state == 'off') and
        (states.climate.haier_ac.state == 'off') and
        (states.sensor.garage_door_position.state == 'Closed') and
        (states.group.windows.state == 'off')}}

    sdm120solar_2:
      value_template: >-
        {{"%.2f" | format(states.sensor.sdm120solar.state |round(2))}}

    sdm230import_2:
      value_template: >-
        {{"%.2f" | format(states.sensor.sdm230import.state |round(2))}}

    sdm230export_2:
      value_template: >-
        {{"%.2f" | format(states.sensor.sdm230export.state |round(2))}}

    sdm120current_2:
      value_template: >-
        {{"%.1f" | format(states.sensor.sdm120current.state |round(1))}}

    sdm230current_2:
      value_template: >-
        {{"%.1f" | format(states.sensor.sdm230current.state |round(1))}}

    house_current:
      value_template: >-
        {{"%.1f" | format(states.sensor.house_current.state |round(1))}}

Other template sensors defined elsewhere in my config are working too. eg:

- platform: template
  sensors:

    master_bedroom_window_rssi:
      value_template: "{{states.binary_sensor.master_bedroom_window.attributes.rssi}}"
      device_class: signal_strength
      unit_of_measurement: dB

    front_bedroom_window_rssi:
      value_template: "{{states.binary_sensor.front_bedroom_window.attributes.rssi}}"
      device_class: signal_strength
      unit_of_measurement: dB
FaintGhost commented 4 years ago

i got same problem here, this config works fine in 0.114.x

sensor:
  - platform: template
    sensors:
      power_loaded:
        friendly_name: "总功率"
        unit_of_measurement: "W"
        value_template: "{{ (state_attr('sensor.kitchen_socket_power', 'power') + state_attr('sensor.bedside_socket_power', 'power') + state_attr('sensor.floor_socket_power', 'power') + state_attr('sensor.desk_socket_power', 'power')) }}"

but in 0.115.x got this error log, also this config works in 0.115.x in the developer tools template tester

Logger: homeassistant
Source: helpers/template.py:285
First occurred: 下午4:47:20 (1 occurrences)
Last logged: 下午4:47:20

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup
    result_info = async_track_template_result(
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 792, in async_track_template_result
    tracker.async_setup()
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup
    self._info[template] = template.async_render_to_info(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info
    render_info._result = self.async_render(variables, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'

could someone help me please?

balk77 commented 4 years ago

Similar issue here (0.115.0-5). After template.reload the value is correctly calculated. It did work in 0.114.x and before

This sensor does not work initially, but it does calculate in the template editor:

- platform: template
  sensors:
    humdelta:
      value_template: '{{ (states("sensor.badkamer_relhumidity") - state_attr("sensor.minhum_stat_mean","min_value")) | round(1) }}'
      unit_of_measurement: '%'

where sensor.badkamer_relhumidity is an ESPHome sensor.

The other term:

- platform: statistics
  name: minhum_stat_mean
  entity_id: sensor.badkamer_relhumidity
  sampling_size: 1500
  max_age:
    hours: 4

There are no specific errors in the logs. I found this, but it is unclear if it is related to the above.

2020-09-29 20:21:19 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved 
Traceback (most recent call last): 
  File "/usr/src/homeassistant/homeassistant/components/template/template_entity.py", line 264, in _async_template_startup 
    result_info = async_track_template_result( 
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 792, in async_track_template_result 
    tracker.async_setup() 
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 518, in async_setup 
    self._info[template] = template.async_render_to_info(variables) 
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 306, in async_render_to_info 
    render_info._result = self.async_render(variables, **kwargs) 
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render 
    return compiled.render(kwargs).strip() 
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render 
    self.environment.handle_exception() 
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception 
    reraise(*rewrite_traceback_stack(source=source)) 
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise 
    raise value.with_traceback(tb) 
  File "<template>", line 1, in top-level template code 
TypeError: unsupported operand type(s) for -: 'str' and 'str' 
github-actions[bot] commented 3 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

balk77 commented 3 years ago

Tested; This seems to be fixed for my specific issue 👍

github-actions[bot] commented 3 years ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.