jonasbkarlsson / ev_smart_charging

Electric vehicle smart charging for Home Assistant.
MIT License
169 stars 25 forks source link

Support for more Electricity price sensor (make it more universal) #260

Closed CasparTheBridge closed 1 month ago

CasparTheBridge commented 5 months ago

During configuration of the integration you need to select a 'Electricity price sensor' but it is not defind how the structure of the sensor should be. I use the standard EnergyZero integration but the output of the service call is not accepted as an input for this integration: Electricity price sensor not found

The content of the output of the service call in the sensor looks like this: example attributes of sensor

Please make it configurable where you can find the data in the sensor, so you can define keys how the data is accessable within the attributes.

(the ENTSO-e integration you have linked to in the documentation seems not maintainted anymore, so more possibilities of your integration will help to keep using your integration)

andreasd commented 4 months ago

we've had this discussion here: https://github.com/jonasbkarlsson/ev_smart_charging/issues/231

tl;dr - it was suggested to support a generic sensor format which can be used in combination with template sensors. with those template sensors, you can reformat lots of different price sources into the generic format (see my example in the linked thread).

but so far, no response about the implementation of a more generalized sensor.

CasparTheBridge commented 4 months ago

The issue is there is no documentation in which format the data needs to be delivered. And the structure to get it working is probably quite specific as with some templating I was unable to get it working with your integration.

So best option I thing is to have during the setup/configuration a sort of selection mode of the data input and in which format it is. The other option is to have a clear description on how the data should be delivered to the integration (and I think the way it is now is to specific and to difficult to create with template sensors)

andreasd commented 4 months ago

there currently is no option to get data from any other source than the pre-defined ones. even if you get it in the exact same format and name the variables like one of the supported extenions, the extension won't be able to pick it up. it will need modifications in ev_smart_charging to get this working.

the issue with just asking for the variables and attributes during setup is, that there is no standard in how prices over time can be delivered. every extension does this differently. so even if you ask for variable and attribute names, only a small fraction of price providers will be supported.

jonasbkarlsson commented 2 months ago

Hi @CasparTheBridge, I have made a v1.11.0-dev4 that supports a generic format. I have made a guess how a template sensor for EnergyZero should look like in order to translate the EnergyZero format to the generic format. Could you test if this works? Or if changes are needed in that template sensor?

CasparTheBridge commented 2 months ago

With help from @TheFes I was able to create a template which I think will work. I've not yet updated to the beta release, I'll probably try tomorrow and when it works I will share the template.

CasparTheBridge commented 2 months ago

It looks like this is going to work! (now I have to setup the charge start & stop properly)

This is the template:

- trigger:
    - platform: time_pattern
      hours: "/1"
    - platform: homeassistant
      event: start
  action:
    - service: energyzero.get_energy_prices
      data:
        incl_vat: false
        config_entry: ec3a34e8bc69248bd067e2211b0d67ab
        start: "{{ today_at() }}"
        end: "{{ today_at() + timedelta(days=2) }}"
      response_variable: prices
  sensor:
    - name: Energy Zero template prices
      unit_of_measurement: "€/kWh"
      unique_id: ff1849c3-b5c5-498a-873e-fb70b37ec7b9
      # availability: "{{ prices.prices | has_value }}"
      state: "{{ prices.prices | selectattr('timestamp', '<=', utcnow().strftime('%Y-%m-%d %H:%M:%S+00:00')) | map(attribute='price') | list | last }}"
      attributes:
        prices_today: >
          {%- set forecast = prices.prices %}
          {%- set time_key = 'timestamp' %}
          {%- set price_key = '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] )] %}
          {%- endfor %}
          {{ ns.data }}
        prices_tomorrow: >
          {%- set forecast = prices.prices %}
          {%- set time_key = 'timestamp' %}
          {%- set price_key = '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] )] %}
          {%- endfor %}
          {{ ns.data }}
jonasbkarlsson commented 2 months ago

Thanks! Any particular reason why the "availabilty" is not included?

Also, it seem like the "state" is calculated using UTC time, it seems. Is that correct? (I haven't tested) Or do you happen to be in the UTC time zone?

CasparTheBridge commented 2 months ago

With the availabilty how I used it in the template it is not working. Maybe due to the prices.prices use, I don't know. The EnergyZero is giving the values on UTC times, with this template it get corrected towards the timezone of the Home Assistant setup. I live in the netherlands so it is here GMT+1 and +1 for day light saving.

lvo12 commented 2 months ago

Thanks everyone for creating the integration of EneryZero into EV Smart Charing. Was looking for this for a while and just found out through the EV Smart Charging release notes. I'm not that experienced with HA yet, and fail to understand where to load the "template". Should I paste it into the automations.yaml or in the template section under devtools? I tried both, but resulted in errors either way.

jonasbkarlsson commented 2 months ago

@lvo12, it should be pasted into configuration.yaml. Copy the code from the Wiki, and replace to two entries that need to be replaced with your values.

lvo12 commented 2 months ago

Thanks it's working (I had to re-add the EV Smart Charging integration to HA). Very happy with this update!!

jonasbkarlsson commented 1 month ago

Template sensor included in v1.11.2.