Get Energy Cost

Hi all,

Looking for some help with a sensor, I am trying to get the cost of charging my PHEV but not having much luck. I’ve got the following:

This gets the current usage in Watts.

- platform: template
  sensors:
    gm_power_active_phev:
      value_template: "{{ state_attr('sensor.good_measure_phev', 'power_active')['v'] }}"
      device_class: power
      unit_of_measurement: "kW"

- platform: template
  sensors:
    gm_power_active_phev_watts:
      unique_id: acb418ca-9d13-423d-a607-a23881affa4a
      value_template: "{{ states('sensor.gm_power_active_phev')|float(0) * 1000 }}"
      device_class: power
      unit_of_measurement: "W

As a sidenote I’ve heard about an available_template but can’t find any reference to it and adding this just causes a config error, anyone know what that is, please let me know.

Then to convert to kWh:

- platform: integration
  name: "PHEV Consumption"
  source: sensor.gm_power_active_phev_watts
  method: left
  unit_prefix: k
  round: 2

and then to get the cost, it is here things aren’t working.
Not getting any errors, but the value is always 0 (well mostly, seems to be odd gaps in the graph).

- platform: template
  sensors:
    phev_charge_cost:
      unique_id: 61b456b8-4e55-4b1f-8183-038f3e0f9086
      friendly_name: Daily PHEV Charging Cost
      value_template: >-
        {% set energy_price = 0 %}
        {% if states('sensor.grid_consumption_power_energy')|float(0) > 0 %}
          {% set energy_price = states('input_number.current_tariff')|float %}
        {% endif %}
        {{ states('sensor.phev_charging_daily')|float(0) * energy_price }}
      device_class: monetary
      unit_of_measurement: "$"

For reference this is the grid_export_power_energy sensor:

- platform: template
  sensors:
    grid_consumption_power_energy:
      unique_id: grid_consumption_power_energy
      value_template: >-
        {% set power = states('sensor.gm_power_active')|float(0) %}
        {% if power > 0.0 %} {{ (states('sensor.gm_power_active')|float(0) * 1000) }}
        {% else %} 0.0 {% endif %}
      device_class: Power
      unit_of_measurement: 'W'

No issues with this or the other sensors, and they all appear in the Energy dashboard, just looking for a more specific costing for charging the PHEV when there isn’t enough solar energy to do it.

Thanks!

It’s just simple math, if you multiply by 0 the result is 0