Template sensor multiply with another sensor syntax

Hello,

I have tried a few different ways and also had a google and cant find anything that helps, usually it’s easy to find someone else with the same problem but no luck.

Bit of background, so I have a load smart plugs which tell me power in watts, I then use the integration platform to turn that into kwh.

  - platform: integration
    source: sensor.living_room_devices
    name: energy_living_room_devices_kwh
    unit_prefix: k
    round: 2

I then do hourly, weekly, monthly, yearly using the utility_meter integration

#Utility Per Month
utility_meter:
  energy_living_room_devices_month_kwh:
    source: sensor.energy_living_room_devices_kwh
    cycle: montly

I then take the monthly sensor and created another template sensor to multiply by 0.15p to give me my £ per month cost.

#Living Room Power Usage
  - platform: template
    sensors:
      energy_living_room_devices_month_cost:
        value_template: '{{ states.sensor.energy_living_room_devices_month_kwh.state | multiply(0.15) | round(2)}}'
        unit_of_measurement: £
        friendly_name: 'Living Room Devices Month Cost'

This all works fine but I do wonder if there is a cleaner way of doing it.

But my main question is can I instead of multiply(0.15) can I multiply(another sensor) because I have changed to a different electricity tariff with 30min changes in the price.

I tried the below but with no luck.

- platform: template
    sensors:
      energy_living_room_devices_new_month_cost:
        value_template: '{{ states.sensor.energy_living_room_devices_month_kwh.state | multiply(states.sensor.octopus_current_price.state) | round(2)}}'
        unit_of_measurement: £
        friendly_name: 'Living Room Devices New Month Cost'

I would need to /100 afterwards as the price comes in as 12 for 12p instead of 0.12.

Any suggestions would be much appreciated.

Cheers

you have to convert the states to floats and multiply them together that way. YOu can use normal math.

"{{ (states('sensor.energy_living_room_devices_month_kwh') | float * states('sensor.octopus_current_price.state')) | round(2)}}"

Of course, I use float for my solar sensor, thank you.

I have just thought though my logic isn’t going to work anyway because it is just going to x the total monthly kwh by whatever the price is at the time.

I need to come up with a way of taking a snapshot of the usage per 30mins x by the cost at that time and then add them up for total month.

I wonder if I can just pull my monthly usage in via the octopus API, I will have a look.

Cheers