Sensor not working after update to 2022.2

Hi,

I just updated to latest version and somehow my sensor is not working anymore!

This is what I have:

  - platform: template
    sensors:
      verbruik_live:
        value_template: '{{ ((states.sensor.power_consumption_phase_l1.state | float)+(states.sensor.power_consumption_phase_l2.state | float)+(states.sensor.power_consumption_phase_l3.state | float)) | max (0) | round(3) }}'
        friendly_name: 'Verbruik Live'
        unit_of_measurement: 'kW'

But it give back an “unavailable”.

When I go back to the previous version it works again… any ideas?

What are the states of the individual sensors in the template?

Check in Developer Tools / States.

Also you should heed this warning:

https://www.home-assistant.io/docs/configuration/templating/#states

Also also you should provide default values for your float filters.

Thank you for your fast answer. But somehow I don’t get it to work. I’ve changed the code to what you suggested. But first, I get an error with the '. So tried it without ’ and with ". Both with no succes.

This is my code now, any pointers are more then welcome!

  - platform: template
    sensors:
      verbruik_live:
        value_template: '{{ ((states('sensor.power_consumption_phase_l1')|float(0.0))+ (states('sensor.power_consumption_phase_l2')|float(0.0))+ (states('sensor.power_consumption_phase_l3')|float(0.0)))|max(0)|round(3) }}'
        friendly_name: 'Verbruik Live'
        unit_of_measurement: 'kW'

And check gives this:
Error loading /config/configuration.yaml: while parsing a block mapping
in “/config/sensor.yaml”, line 50, column 9
expected , but found ‘’
in “/config/sensor.yaml”, line 50, column 39

I’ve got it working again!

It now looks like this:

  - platform: template
    sensors:
      verbruik_live:
        value_template: "{{((states('sensor.power_consumption_phase_l1')|float(0.0)) + (states('sensor.power_consumption_phase_l2')|float(0.0)) + (states('sensor.power_consumption_phase_l3')|float(0.0)))|round(3)}}"
        friendly_name: 'Verbruik Live'
        unit_of_measurement: 'kW'

type or paste code here

Yeah you have to be careful when you have quotes inside and outside your template. You end up with pairs of quotes like this if you don’t use single and double quotes:

value_template: '{{ ((states('

Yes, that was a problem. But also in the original code was “max(0)”, this was also a problem (?).

I was trying the code in developer tools and gave a problem. Without it not so much. So I removed it from my code.

The error is gave: TypeError: ‘float’ object is not iterable

I get the same error with the tesla-style-solar-power-card TypeError: ‘float’ object is not iterable

I removed the min and max, but it’s still not 100% working:-(

template:

  • sensor:
    • name: “Tesla Card Grid Consumption”
      unique_id: “tesla_card_grid_consumption”
      state: “{{ states(‘sensor.powerwall_site_now’) | float | max(0) | round(1) }}”
      device_class: power
      unit_of_measurement: kW

    • name: “Tesla Card Grid Feed In”
      unique_id: “tesla_card_grid_feed_in”
      state: “{{ states(‘sensor.powerwall_site_now’) | float | min(0) | abs | round(1) }}”
      device_class: power
      unit_of_measurement: kW

    • name: “Tesla Card Solar Consumption”
      unique_id: “tesla_card_solar_consumption”
      state: “{{ ((states(‘sensor.powerwall_solar_now’) | float) - (states(‘sensor.tesla_card_grid_feed_in’) | float ) - (states(‘sensor.tesla_card_battery_charging_inside’) | float) ) | round(1) }}”
      device_class: power
      unit_of_measurement: kW

    • name: “Tesla Card Battery Consumption”
      unique_id: “tesla_card_battery_consumption”
      state: “{{ states(‘sensor.powerwall_battery_now’) | float | max(0) | round(1) }}”
      device_class: power
      unit_of_measurement: kW

    • name: “Tesla Card Battery Charging Inside”
      unique_id: “tesla_card_battery_charging_inside”
      state: “{{ states(‘sensor.powerwall_battery_now’) | float | min(0) | abs | round(1) }}”
      device_class: power
      unit_of_measurement: kW

After updated to 2022.2 i also had similar problems with my PV calcluations.
I think i found a solution.
Based on the Jinja docs “max()” requires a list/array.
https://jinja.palletsprojects.com/en/latest/templates/#configuration#jinja-filters.max
That is also mentioned in the error “object is not iterable”.

So you have to turn this:

state: “{{ states(‘sensor.powerwall_site_now’) | float | min(0) | abs | round(1) }}”

into:

state: “{{ [ states(‘sensor.powerwall_site_now’) | float, 0 ] | min | abs | round(1) }}”

So that you push an array with our value and 0 into the min and max operation.

3 Likes

The specs changed silently. Before, you were allowed to pipe with min / max. You are not anymore, and all templates with the very frequently used notation will just break. Example of what you could do before:

some_variable: some_other_variable * 1.5 | min(100)
meaning it will take some_other_variable, multiply with 1.5, but not allow it to go above 100.

Now you have to add even more parentheses and brackets and confusion, making typical templates start with {{((((

some_variable: min([some_other_variable * 1.5, 100])

it is the min(100) part that breaks, as min (from now on) requires the argument to be an array to iterate through and pick the smallest value. And 100 is not an array, so it cannot iterate over that “int” thus the error printed that “int is not iterable” or similar.

Cannot do my_value | min(100) | max(1) to clamp a value between 1 and 100. You need to max([min([my_value, 100]),1])

1 Like

Thank you for this! It solved my issue. I believe your closing brackets are wrong, though (first reversed, second too many). Took me a sec to see it and correct it to make it work.

In case it’s useful to someone, I wanted to convert a lux meter that ranges 10-10000 to a brightness 1-100%, by dividing by 100 (simple for now) but keeping it between 1 and 100 %. My end result was:

{{ max([min([states("sensor.estimated_illuminance_native")|float / 100, 100]), 1]) }}

Yes, so many brackets… Sad that the pipe format isn’t supported anymore :frowning:

It’s still supported

{{ [ [states("sensor.estimated_illuminance_native")|float / 100, 100] | min, 1] | max }}

EDIT: to clarify the change. Everyone before used

{{ x | min(y) }}

which is surprising it actually worked as the syntax is invalid in the world of Jinja. What you’re really doing is

{{ min(x, y) }}

without realizing it. Min or max always requires an iterator for the argument.

It only worked because home assistant wasn’t using the normal min/max function built into jinja. It was using pythons, which is more forgiving but you can end up with odd results if you don’t understand the syntax.

1 Like

So then, what was possible before IS NOT possible anymore. Something changed.

{{ x | min(y) }} did work before, and is much more like how other things are working. E.g.

{{ x | int }} or {{ x | int(1) }} so why not {{ x | min(1) }} I’m not stupid, and I am a developer, so i see the difference; but we did in fact have breaking updates that broke most templates and it wasn’t preceded with deprecation warnings that is usually what one would do before removing functionality. Even if the functionality didn’t officially exist before, it did in fact work and was used frequently by most. So removing it was a breaking change.

Yes I know something changed… I wasn’t arguing you I was explaining what changed.

The previous method was not correct, and it was just dumb luck that the way you guys were using it worked.

My template sensors seem to be working properly but I know I need to clean this up but not sure how:

This is in my binary_sensor.yaml folder so I changed it to:

First question - do I have the first part correct? Second question - what do I need to correct the array? I read something about setting a default and/or min max.

Please take the time to format your code and post it as text. People do not want to rewrite your entire configuration by looking at an image.

As for your issue, the image 1 seems to be fine but if you put it in the wrong spot, it won’t work. My guess is you’re incorrectly adding the first image to the template section.

Your second image is a combination of the old configuration and the new configuration. You have to use the new config or the old config, not a mixture of both.

Ok. I posted the pic to show the errors as it wouldn’t copy them.

- platform: template
  sensors:
    freezer_too_warm:
      friendly_name: "Freezer Too Warm"
      value_template: "{{ states('sensor.freezer_sensor_temperature')|float > 25 }}"

I have it in my binary_sensor.yaml file. In my configuration.yaml file I have
binary_sensor: !include binary_sensor.yaml
Where should it be placed?

That should work then. If the sensor works, ignore the errors in vscode. VSCode may not know the context based on where it was included.

It seems to be working fine but what alerted it to me was this error in the logs:

* Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ states('sensor.freezer_sensor_temperature')|float > 25 }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

Yes, that will happen because you aren’t providing a default value for float when the sesnor has an unknown state. float(0) will make the default 0