Problems getting code to work

Hi,

I have had this code working for sometime recently it stopped working, I’ve looked at the Templates HA page and noticed its using a legacy format. I have tried to correct it but everytime I just get errors! Can anyone help?

template:
  - sensors:
      front_sump_on_24:
        friendly_name: "Front sump on over 24H"
        value_template: '{{ states("sensor.front_sump_pump_energy_current_2") | float > 1 }}'

Many thanks
James

there should be no reason that the legacy template code would have stopped working if it had worked before. I have many template sensors running on the legacy format and I have no issues.

the new template format uses different key options for the sensors.

template:
  - sensors:
      name: "Front sump on over 24H"
      state: '{{ states("sensor.front_sump_pump_energy_current_2") | float > 1 }}'

note that the entity_id of the sensor will change because the new format gets the entity_id from the name. the old format got the entity_id from the entity id you created.

Thank you for your response the issue I have is that I get ‘Entity not available’ on the dashboard and this in the log file.

Logger: homeassistant.helpers.template
Source: helpers/template.py:540
First occurred: 14 February 2023 at 14:51:48 (2318 occurrences)
Last logged: 10:09:02

Template variable error: 'sensor' is undefined when rendering '{{ states(sensor.front_sump_pump_energy_current_2) | float > 1 }}'
Template variable error: 'sensor' is undefined when rendering '{{ states(sensor.sump_pump_energy_current) | float > 1 }}'

That’s because you aren’t using the correct syntax.

you need to quote the inside of the parenthesis. And they need to be different than the quotation marks outside the brackets.

'{{ states("sensor.front_sump_pump_energy_current_2") | float > 1 }}'

see the difference between the error message and my example.