Why can't I get value of my sensor in Template

I am trying to learn how to create templates for the first time so please be patient!
I am trying to create a new entity Electricity Unit Cost from the most recent days electricity usage and days cost. The new variable Cost and Kwh are just being set to the default value of 1. Any ideas why please?

template:
  - sensor:
      - name: "Electricity Unit Cost"
        unit_of_measurement: "£"
        state:
          "{% set Cost = float (states( 'sensor.ovo_last_energy_cost'), 1) %}"
          "{% set Kwh = float (states('sensor.last_electricity_reading'),1) %}"
          "{{ Cost / Kwh  }}"

Result type: string
template:
  - sensor:
      - name: "Electricity Unit Cost"
        unit_of_measurement: "£"
        state:
          ""
          ""
          "1.0"

There are a couple of formats for templates.

Templates on a single line that need to be quoted

state: "{{ some_template }}"

And multi-line templates that should not be quoted:

state: >
  {{ some_tempalte }}

Thanks but I am still getting a similar result:

template:
  - sensor:
      - name: "Electricity Unit Cost"
        unit_of_measurement: "£"
        state: >
          {% set Cost = float (states( 'sensor.ovo_last_energy_cost'), 1) %}
          {% set Kwh = float (states('sensor.last_electricity_reading'),1) %}
          {{ Cost / Kwh}}

Result type: string
template:
  - sensor:
      - name: "Electricity Unit Cost"
        unit_of_measurement: "£"
        state: >
          
          
          1.0

What does this return in th3e template editor:

{{ states('sensor.ovo_last_energy_cost') }}
{{ states('sensor.last_electricity_reading') }}

Opps - I had the names mixed up. Thank you for all your help.

I am now trying to use some attributes in a picture-elements card. This card can only use entities not attributes, so I have created entities from attributes in Templates. I have created the following in Developers → Template

template:
  - sensor:
      - name: "Kitchen Temperature"
        unit_of_measurement: "°C"
        unique_id: kitchen_temperature
        state: >
          {% set kitchen_temperature = float( state_attr('climate.wiser_kitchen', 'current_temperature'),1) %}
          {{ kitchen_temperature }}

template:
  - sensor:
      - name: "Kitchen Setpoint"
        unit_of_measurement: "°C"
        unique_id: kitchen_setpoint
        state: >
          {% set kitchen_setpoint = float( state_attr('climate.wiser_kitchen', 'temperature'),1) %}
          {{ kitchen_setpoint }}

{% if kitchen_temperature > kitchen_setpoint %}
  It is warm!
{% else %}
  It is cold
{% endif %}

Result:

template:

  • sensor:
    • name: “Kitchen Temperature”
      unit_of_measurement: “°C”
      unique_id: kitchen_temperature
      state: >

      22.8

template:

  • sensor:
    • name: “Kitchen Setpoint”
      unit_of_measurement: “°C”
      unique_id: kitchen_setpoint
      state: >

      21.0

It is warm!

I get exactly the result I want and expect. But when I put this into my yaml.config file, save and restart, these new entities always have a value of zero. Any ideas what I have done wrong please?

template:
  - sensor:
      - name: "My Electricity Unit Cost"
        unit_of_measurement: "'GBP/kWh"
        unique_id: my_electricity_unit_cost
        state: >
          {% set Cost = float (states( 'sensor.ovo_last_electricity_cost'), 1) %}
          {% set Kwh = float (states('sensor.ovo_last_electricity_reading'),1) %}
          {{ Cost / Kwh}}

  - sensor:
      - name: "Kitchen Temperature"
        unit_of_measurement: "°C"
        unique_id: kitchen_temperature
        state: >
          {% set kitchen_temperature = float( state_attr('climate.wiser_kitchen', 'current_temperature'),1) %}

  - sensor:
      - name: "Kitchen Setpoint"
        unit_of_measurement: "°C"
        unique_id: kitchen_setpoint
        state: >
          {% set kitchen_setpoint = float( state_attr('climate.wiser_kitchen', 'temperature'),1) %}

image

image