Home assistant – energy – price entity that switches between night and day tariff (peak and off-peak hours)
I have a raspberry with Home Assistant Tracking energy usage using the Aeotec Home Energy METER Gen5 (ZWave) and a WiFi Shelly 3EM (2 separate locations).
I was in need to track the consumed energy in €, home assistant allows you to pick a “Use an entity with current price” in the energy dashboard

I did not want to deal with automation and such, so I devised a simple template sensor that switches its “price” between specific time periods. (pro tip: convert the current hour of the day to minutes + add the current minutes)
here we see the sensor switching price

The new modern way of doing this would be with a template sensor
Based on EDF’s peak and off-peak hours

template:
- sensor:
- name: "EDF Price"
unique_id: edf_price
unit_of_measurement: EUR/kWh
state: "
{% set time = now().hour * 60 + now().minute %}
{% if time > 750 and time < 870 or time > 90 and time < 450 %}
{{'14.70'|float/100}}
{% else %}
{{'18.41'|float/100}}
{% endif %}
"
Depending on your peak hours they could be different:

A variant on the same template sensor
template:
- sensor:
- name: "EDF Price"
unique_id: edf_price
unit_of_measurement: EUR/kWh
state: "
{% set time = now().hour * 60 + now().minute %}
{% set startTime = 21 * 60 + 56 %}
{% set endTime = 5 * 60 + 56 %}
{% if time >= startTime or time <= endTime %}
{{'14.70'|float/100}}
{% else %}
{{'18.41'|float/100}}
{% endif %}
"

The legacy way of doing it (don’t do this!)
The old way would have been adding a template under the sensor
#old way of doing things
sensor:
- platform: template
sensors:
edf_price:
friendly_name: "EDF Price"
unit_of_measurement: EUR/kWh
value_template: "
{% set time = now().hour * 60 + now().minute %}
{% if time > 750 and time < 870 or time > 90 and time < 450 %}{{'14.70'|float/100}}
{% else %}{{'18.41'|float/100}}
{% endif %}
"
