Sensor that displays value - or 0 if switch is OFF

Ive created a sensor that calculates a Kwh value, but I need this to be 0 if appliance is off? How can I do this is HA? So, I have sensor.appliance which returns say 24. sensor.enable is either ON or OFF. If ON then the value needs to be 24 (or whatever) else 0 (if off)

"{{ states('sensor.Flame_level') | float(0) * 0.01 * 27 +6.7 }}"

This is needs to work with sensor.ot_flame_enable which is either ON or OFF.

I also struggle in working out where code needs to go in yaml. and how it should be indented.

You can include an ‘if’ test…

{{ (states('sensor.Flame_level') | float(0) * 0.01 * 27 +6.7) if states('sensor.enable') == 'on' else 0 }}

And assuming your ‘configuration.yaml’ has an include for “templates.yaml”, you would put it in that file along the lines of:

- name: My Sensor
  unique_id: my_sensor
  state: >-
    {{ (states('sensor.Flame_level') | float(0) * 0.01 * 27 +6.7) if states('sensor.enable') == 'on' else 0 }}