Update for sensor.shelly_total_power_consumption fails

2020-04-13 15:31:15 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.shelly_total_power_consumption fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 474, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 224, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 221, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: can only concatenate str (not "float") to str
2020-04-13 15:31:15 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.shelly_total_power fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 474, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 224, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 221, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: can only concatenate str (not "float") to str
2020-04-13 15:31:15 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.shelly_total_power_consumption fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 474, in async_device_update
    await self.async_update()
  File "/usr/src/homeassistant/homeassistant/components/template/sensor.py", line 224, in async_update
    self._state = self._template.async_render()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 221, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
TypeError: can only concatenate str (not "float") to str

This is what the two template sensors look like:

- platform: template
  sensors:
    shelly_total_power_consumption:
      friendly_name: 'Verbrauch Gesamt'
      unit_of_measurement: 'kWh'
      value_template: '{{ ((float(states.sensor.shelly_shem_3_dc4f227648a6_1_total_consumption.state) + float(states.sensor.shelly_shem_3_dc4f227648a6_2_total_consumption.state) + float(states.sensor.shelly_shem_3_dc4f227648a6_3_total_consumption.state)) | round(2) )}}'
                               
- platform: template
  sensors:
    shelly_total_power:
      friendly_name: 'Leistung Gesamt'
      unit_of_measurement: 'W'
      value_template: '{{ float(states.sensor.shelly_shem_3_dc4f227648a6_1_current_consumption.state) + float(states.sensor.shelly_shem_3_dc4f227648a6_2_current_consumption.state) + float(states.sensor.shelly_shem_3_dc4f227648a6_3_current_consumption.state | round(0) )}}'

Any idea what’s wrong? Thanks!!

You’re missing some parentheses from your second value_template. Consequently it thinks part of your equation is a string

Thanks. Parentheses means “(” or “)” correct? If I go to “Developer Tools” -> “Template” and use it, all works fine and I get the value needed. Any btw: All is working fine so far, and the values show correctly in Lovelace.

I see there was something missing in the second statement, but it does throw that error for BOTH sensors. I corrected the parentheses error, but the problem still remains the same.

I don’t know if that helps, but I have no idea if the sensor is a string or number?!

image

Yes float it’s a pipe not a function call, sorry

Haha. Not I’d know what you want to tell me :wink: Does this solve my problem or not? Sorry, but I’m not into programming at all :frowning:

Here you can see the working sensor:

image

I looked at your code again. Assuming you modified the parentheses correctly it should be fine. Not sure why the system thinks it is doing a string concatenation unless what you are showing as the template sensors does not accurately reflecting the system throwing the error message. In any event, states are always represented as text, only attributes could be something other than text.

So you say the result of the template IS TEXT, so that would be a STRING and not a number?! How do I adress it then?

Try this version and let me know if it produces the same error message.

- platform: template
  sensors:
    shelly_total_power_consumption:
      friendly_name: 'Verbrauch Gesamt'
      unit_of_measurement: 'kWh'
      value_template: >
        {{ (states('sensor.shelly_shem_3_dc4f227648a6_1_total_consumption') | float +
            states('sensor.shelly_shem_3_dc4f227648a6_2_total_consumption') | float +
            states('sensor.shelly_shem_3_dc4f227648a6_3_total_consumption') | float)
            | round(2) }}

    shelly_total_power:
      friendly_name: 'Leistung Gesamt'
      unit_of_measurement: 'W'
      value_template: >
        {{ (states('sensor.shelly_shem_3_dc4f227648a6_1_current_consumption') | float +
            states('sensor.shelly_shem_3_dc4f227648a6_2_current_consumption') | float +
            states('sensor.shelly_shem_3_dc4f227648a6_3_current_consumption') | float)
            | round(0) }}

Yes text is a string and in your case you first convert it to float so you can add it just like you did.

I was wondering if utilizing states() and |float would make any difference. I did try float() with the full variable name as in the example and the Template tool accepted that just fine although I didn’t try an equation the length of the example.

Thanks for your suggestion. Tried it, and it gives exactly the same ERROR messages as above. :frowning:

There’s something strange happening here that I can’t quite identify and I suspect it may be due to missing clues.

I say that because the template I offered can convert any string value into a numeric (float) value. Even if all three shelly sensor values are the string unknown, the float filter is capable of converting non-numeric values to 0.0. The sum of three zeros is zero and would not cause the error message to appear.

There’s something else responsible for this behavior.

I maybe have a similar issue with this code :

- id: '1570892521738'
  alias: '[Chauffage][Ch. Enfants] Arrêt si t°>(consigne chambre enfants - 0.5°C)'
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: template
    value_template: "{{ (states('sensor.chambre_enfants_temperature')|float) > ((states('input_number.sliderchambreenfants')|float) - (0.5 |float)) }}"
  action:
  - alias: ''
    data: {}
    entity_id: switch.chauffage_enfants_rfx
    service: switch.turn_on

On execution of the automation I have the same error message that described above : TypeError: can only concatenate str (not “float”) to str :

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 403, in async_trigger
and not self._cond_func(variables)
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 578, in if_action
return all(check(hass, variables) for check in checks)
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 578, in
return all(check(hass, variables) for check in checks)
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 473, in template_if
return async_template(hass, value_template, variables)
File “/usr/src/homeassistant/homeassistant/helpers/condition.py”, line 453, in async_template
value = value_template.async_render(variables)
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 “”, line 1, in top-level template code
TypeError: can only concatenate str (not “float”) to str

Do you have any idea about the error ?

Thanks

Can I ask why you are using a Time Pattern Trigger for that automation?

The condition compares the value of a sensor to an input_number. The only time when it’s worth comparing them is when either one of them changes state (instead of checking every minute). In other words, the automation should be triggered when either the sensor or the input_number changes state.

- id: '1570892521738'
  alias: '[Chauffage][Ch. Enfants] Arrêt si t°>(consigne chambre enfants - 0.5°C)'
  trigger:
  - platform: template
    value_template: "{{ states('sensor.chambre_enfants_temperature')|float > states('input_number.sliderchambreenfants')|float - 0.5 }}"
  action:
  - alias: ''
    data: {}
    entity_id: switch.chauffage_enfants_rfx
    service: switch.turn_on

It’s unnecessary to do this:

(0.5 | float)

because 0.5 is understood to be a floating-point number.

Screenshot from 2020-10-07 12-56-32


EDIT

In the original question, the problem may have been due to how float was used (either as filter or a function). For example, notice how these two produce different results:

Screenshot from 2020-10-07 13-05-43

Your template presents a different situation and it’s unclear to me why it generates that error message.

1 Like

Thanks @123. The code is indeed working well. The error disappears when I restarted Home Assistant… It was like a ghost error. This error was raised every minute for a code that did not exist anymore, I don’t know why. It happened even with all automations disabled.

About the trigger, yes yours is better. I wonder now if for this example it is also a good idea to remove the time pattern and replace it by a changing state trigger :

- id: '1570892014600'
  alias: '[Chauffage][SdB] Marche si t°<consigne SDB - 3.5°c ET Présence'
  description: ''
  trigger:
    platform: time_pattern
    minutes: /1
  condition:
  - condition: state
    entity_id: alarm_control_panel.somfy_protect
    state: disarmed
  - condition: template
    value_template: '{{ (states(''sensor.salle_de_bain_temperature'')|float) < ((states(''input_number.slidersalledebain'')
      |float) - (3.5 |float)) }}'
  - condition: state
    entity_id: switch.seche_serviette_rfx
    state: 'on'
  action:
  - alias: ''
    data: {}
    entity_id: switch.seche_serviette_rfx
    service: switch.turn_off

In my understanding, If I replace the trigger by

- platform: template
    value_template: "{{ states('sensor.salle_de_bain_temperature')|float < states('input_number.slidersalledebain')|float - 3.5}}"

the risk is that if one of these conditions is not ok :

  - condition: state
    entity_id: alarm_control_panel.somfy_protect
    state: disarmed

or

  - condition: state
    entity_id: switch.seche_serviette_rfx
    state: 'on'

then the automation will never be triggered again until … I don’t know when in fact :slight_smile: . But if I stay with the time pattern trigger for this specific case, I am sure it will be checked with conditions every minutes.

I don’t know if you catch my point.

In that example, all three conditions would remain as conditions. The three conditions would also be used to create three equivalent triggers (instead of the Time Pattern Trigger). So when any one of these changes state:

  • alarm_control_panel.somfy_protect
  • switch.seche_serviette_rfx
  • sensor.salle_de_bain_temperature
  • input_number.slidersalledebain

it triggers the automation and then proceeds to evaluate the three conditions.

In this way, the automation is triggered only when needed and not every minute.

1 Like