Bug in template or Jinja issue? - dropping a digit?

Hi Guys,
Just wanted to check if I am doing something wrong or if there is a bug here. I have a template which is working fine and I went to add a new sensor to it and it failed but the error message is strange. This works:

      - unique_id: "power_circuit_2_all_other"
        name: "Power Circuit 2 minus Devices"
        unit_of_measurement: W
        icon: mdi:transmission-tower
        state: >
          {{ [(states.sensor.power2b.state | float(0)
           - states.sensor.tasmota_energy_power_2.state | float(0)
           - states.sensor.gpo2_2_power.state | float(0)
           - (states.sensor.car_charger_power.state | float(0) | multiply(0.97))),0] | max
          }}

This fails with the error message: Invalid config for [template]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'd_printer_power') for dictionary value @ data['sensor'][6]['state']. Got '{{ [(states.sensor.power2b.state | float(0)\n - states.sensor.tasmota_energy_power_2.state | float(0)\n - states.sensor.gpo2_2_power.state | float(0)\n - (states.sensor.car_charger_power.state | float(0) | multiply(0.97))),0] | max\n - states.sensor.3d_printer_power.state | float(0)\n}}\n'. (See /config/packages/energy.yaml, line 1).

      - unique_id: "power_circuit_2_all_other"
        name: "Power Circuit 2 minus Devices"
        unit_of_measurement: W
        icon: mdi:transmission-tower
        state: >
          {{ [(states.sensor.power2b.state | float(0)
           - states.sensor.tasmota_energy_power_2.state | float(0)
           - states.sensor.gpo2_2_power.state | float(0)
           - (states.sensor.car_charger_power.state | float(0) | multiply(0.97))),0] | max
           - states.sensor.3d_printer_power.state | float(0)
          }}

The template editor shows a similar issue. I put in {{ (states.sensor.3d_printer_power.state) }} and I get TemplateSyntaxError: expected token ')', got 'd_printer_power'

sensor.3d_printer_power is working fine it has a state of 12.15 and the attributes are:

state_class: measurement
last_seen: '2022-05-04T23:47:44.870Z'
linkquality: 65
power: 12.15
unit_of_measurement: W
device_class: power
friendly_name: 3D Printer power

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

You’ve put the line in the wrong place and outside the syntax :slight_smile: put it in the middle

      - unique_id: "power_circuit_2_all_other"
        name: "Power Circuit 2 minus Devices"
        unit_of_measurement: W
        icon: mdi:transmission-tower
        state: >
          {{ [(states.sensor.power2b.state | float(0)
           - states.sensor.tasmota_energy_power_2.state | float(0)
           - states.sensor.gpo2_2_power.state | float(0)
           - states.sensor.3d_printer_power.state | float(0)
           - (states.sensor.car_charger_power.state | float(0) | multiply(0.97))),0] | max
          }}
1 Like

And the warning still applies.

1 Like

Yes thanks @TheHolyRoger that was an obvious one but it was @tom_l guidance that solved it. I do need to go through and re-do all my templates to comply with that. Still weird why it was dropping the3 but its now worklng. Thanks to you both.