Multiply by sensor value

Is it possible to multiply by a sensor value in a template?

I am using a template sensor that takes power usage and multiplies by a energy unit cost to give total cost of running an appliance for a day. The energy cost is currently hard coded into the template, it I get real-time energy costs from my energy supplier in another sensor, so I’d like to use this sensor value for the multiplication - is this possible?

My current template

- platform: template
    sensors:
      washing_energy_today_cost:
        friendly_name: Washing Energy Cost for Today
        value_template: >-
          {% if is_state('sensor.washing_machine_energy_today', 'unknown') %}
            Unknown
          {% else %}
            {{ '{:.2f}'.format(states('sensor.washing_machine_energy_today')  | multiply(1.74)) }}
          {% endif %}
        unit_of_measurement: "£"
        icon_template: mdi:cash-multiple

States are always strings (attributes OTOH retain types). Add a | int(0) or | float(0) filter before you multiply.

Thank you for the reply but I don’t understand sorry

Any chance of an example on how I multiply by sensor.energy_cost instead of the 1.74 in my code please?

{{ (states(‘sensor.whatever’) | float * 1.1135) | round(2) }}

{{ states('your_sensor_1')| float(0) * states('your_sensor_2') | float(0) }}
2 Likes

Thank you Mats789 this worked - do you know how i edit the template to only show the value to 2 decimal places please?

{{ (states('your_sensor_1')| float(0) * states('your_sensor_2') | float(0)) | round(2) }}
1 Like

Thank you so much