jonasbkarlsson / ev_smart_charging

Electric vehicle smart charging for Home Assistant.
MIT License
139 stars 18 forks source link

Alternative version for template sensor in Wiki #275

Open TheFes opened 2 days ago

TheFes commented 2 days ago

I couldn't find an edit button for the Wiki lemma, but I wanted to propose an alternative template sensor for the creation of the template sensor:

The variables set in the first 3 lines of the attribute make it more easy for the users to adjust the template for their own source sensor. I also found it a bit redundant to create the lists for both today and tomorrow in each attribute.

Not a user of the integration myself, but I was asked to help with creating a template sensor to be used with this integration, and thought it might be good to share :)

template:
  - sensor:
      - name: "Epex Spot Transformed"
        unit_of_measurement: "€/kWh"
        unique_id: 1daf4afa-8c54-4ab7-b055-7f85ea65a2c7
        state: "{{ states('sensor.epex_spot_data_net_price') }}"
        availability: "{{ 'sensor.epex_spot_data_net_price' | has_value }}"
        attributes:
            prices_today: >
                {%- set forecast_data = state_attr('sensor.epex_spot_data_net_price', 'data') %}
                {%- set time_key = 'start_time' %}
                {%- set price_key = 'price_ct_per_kwh' %}
                {%- set ns = namespace(data=[]) %}
                {%- for i in forecast_data | default([], true) if as_local(as_datetime(i[time_key])).date() == now().date() %}
                  {%- set ns.data = ns.data + [dict(time= as_local(as_datetime(i[time_key])).isoformat(), price = i[price_key])] %}
                {%- endfor %}
                {{ ns.data }}
            prices_tomorrow: >
                {%- set forecast_data = state_attr('sensor.epex_spot_data_net_price', 'data') %}
                {%- set time_key = 'start_time' %}
                {%- set price_key = 'price_ct_per_kwh' %}
                {%- set ns = namespace(data=[]) %}
                {%- for i in forecast_data | default([], true) if as_local(as_datetime(i[time_key])).date() == (now()+timedelta(days=1)).date() %}
                  {%- set ns.data = ns.data + [dict(time= as_local(as_datetime(i[time_key])).isoformat(), price = i[price_key])] %}
                {%- endfor %}
                {{ ns.data }}
hmmbob commented 2 days ago

@TheFes was helping me with the template to use the Zonneplan component as source (Zonneplan is one of the Netherlands energy providers with dynamic/hourly tariffs)

This is the working result template, might be nice to add to the Wiki:

template:
  - sensor:
      - name: "Zonneplan template price sensor"
        unique_id: zonneplan_template_price_sensor
        unit_of_measurement: "€/kWh"
        availability: "{{ 'sensor.zonneplan_current_electricity_tariff' | has_value }}"
        state: "{{ states('sensor.zonneplan_current_electricity_tariff') }}"
        attributes:
          prices_today: >
            {%- set forecast = state_attr('sensor.zonneplan_current_electricity_tariff', 'forecast') %}
            {%- set time_key = 'datetime' %}
            {%- set price_key = 'electricity_price' %}
            {%- set ns = namespace(data=[]) %}
            {%- for i in forecast | default([], true) if as_local(as_datetime(i[time_key])).date() == now().date() %}
              {%- set ns.data = ns.data + [dict(time= as_local(as_datetime(i[time_key])).isoformat(), price = i[price_key] / 10000000 )] %}
            {%- endfor %}
            {{ ns.data }}
          prices_tomorrow: >
            {%- set forecast = state_attr('sensor.zonneplan_current_electricity_tariff', 'forecast') %}
            {%- set time_key = 'datetime' %}
            {%- set price_key = 'electricity_price' %}
            {%- set ns = namespace(data=[]) %}
            {%- for i in forecast | default([], true) if as_local(as_datetime(i[time_key])).date() == (now()+timedelta(days=1)).date() %}
              {%- set ns.data = ns.data + [dict(time= as_local(as_datetime(i[time_key])).isoformat(), price = i[price_key] / 10000000 )] %}
            {%- endfor %}
            {{ ns.data }}
TheFes commented 2 days ago

Added unit_of_measurement, unique_id and availability to the YAML

The unique_id is an UUID which I generated here https://www.uuidgenerator.net/

Also updated the name to be more read friendly, this will be slugified to epex_spot_transformed for the entity_id

jonasbkarlsson commented 1 day ago

@TheFes, thanks for the alternative version. I have used it to update the EPEX Spot Wiki page. Except for the UUID, which is good for automatically generated IDs, but maybe not as good for manually created IDs.

@hmmbob, I also added the Zonneplan variant to the Wiki page.

Also, when adding unique_idto the template, the properties of the entity changed in such a way that I needed to update the code a bit. So now there is a v1.11.0-dev2 release available.