Today's cost from Energy-sources-table as sensor

Rather than using the new Energy dashboard, I would like to display today’s cost (as presented in the energy-sources-table card) in a separate lovelace view. I have tried to setup a new template sensor based on the autogenerated “sensor._cost” but w/o success.

template:
  - sensor:
    - name: "Todays cost"
      unit_of_measurement: "EUR"
      device_class: monetary
      state_class: total
      state: "{{ states('sensor.<autogenerated entity>_cost') }}"

Is a new sensor the way to go here, and in that case what would it look like? Perhaps there is another, better method to get this value?

Why do you create a new sensor that shows the state of the existing sensor? You can just use the existing sensor in your lovelace view.

It was my noobie solution to the problem that the _cost state goes to zero upon every restart of HA, so in order to track today’s cost I tried to accumulate sensor values over today. Great if I could access this value directly by _cost[ NOW ] - _cost[period start time] or similar. Is that possible?

Many thanks

Hi, I use this to get all sources consumption price (snippet taken from Dev tools):

{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}{% if  s.object_id.endswith('_cost') %}
{% set ns.states = ns.states + [ s.state | float(default=0) ] %}
{% endif %}
{% endfor %}
{{ ns.states | sum | round(2,default=0)}}

It works OK as template sensor but does not persist HA reboots :frowning:
Tried to add state_class: total_increasing to all _cost sensors but last_reset indicates last restart time/date and result is of course ZERO
Is there a way to make a sensor which value will survive reboots?
Best, JR