jonasbkarlsson / ev_smart_charging

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

`sensor.ev_smart_charging_charging` does not update when new template prices available #279

Closed hmmbob closed 3 months ago

hmmbob commented 3 months ago

Version of the custom_component

Version 1.11.0-dev2

Configuration

Standard component with Zonneplan template sensor

Describe the bug

sensor.ev_smart_charging_charging does not update when new template prices are available

Debug log

Attributes of sensor.zonneplan_template_price_sensor:

prices_today: 
- time: '2024-07-03T00:00:00+02:00'
  price: 0.3540204
- time: '2024-07-03T01:00:00+02:00'
  price: 0.2846269
- time: '2024-07-03T02:00:00+02:00'
  price: 0.2664769
- time: '2024-07-03T03:00:00+02:00'
  price: 0.2517149
- time: '2024-07-03T04:00:00+02:00'
  price: 0.2517149
- time: '2024-07-03T05:00:00+02:00'
  price: 0.2483269
- time: '2024-07-03T06:00:00+02:00'
  price: 0.2674207
- time: '2024-07-03T07:00:00+02:00'
  price: 0.2887288
- time: '2024-07-03T08:00:00+02:00'
  price: 0.2883779
- time: '2024-07-03T09:00:00+02:00'
  price: 0.2702279
- time: '2024-07-03T10:00:00+02:00'
  price: 0.2579827
- time: '2024-07-03T11:00:00+02:00'
  price: 0.2610439
- time: '2024-07-03T12:00:00+02:00'
  price: 0.2604269
- time: '2024-07-03T13:00:00+02:00'
  price: 0.2484479
- time: '2024-07-03T14:00:00+02:00'
  price: 0.2262323
- time: '2024-07-03T15:00:00+02:00'
  price: 0.2236066
- time: '2024-07-03T16:00:00+02:00'
  price: 0.2243568
- time: '2024-07-03T17:00:00+02:00'
  price: 0.2474436
- time: '2024-07-03T18:00:00+02:00'
  price: 0.2648555
- time: '2024-07-03T19:00:00+02:00'
  price: 0.2865024
- time: '2024-07-03T20:00:00+02:00'
  price: 0.3382057
- time: '2024-07-03T21:00:00+02:00'
  price: 0.2788552
- time: '2024-07-03T22:00:00+02:00'
  price: 0.2573898
- time: '2024-07-03T23:00:00+02:00'
  price: 0.2355856

prices_tomorrow: 
- time: '2024-07-04T00:00:00+02:00'
  price: 0.2224329
- time: '2024-07-04T01:00:00+02:00'
  price: 0.1939979
- time: '2024-07-04T02:00:00+02:00'
  price: 0.1714314
- time: '2024-07-04T03:00:00+02:00'
  price: 0.1637479
- time: '2024-07-04T04:00:00+02:00'
  price: 0.1606382
- time: '2024-07-04T05:00:00+02:00'
  price: 0.1605414
- time: '2024-07-04T06:00:00+02:00'
  price: 0.1894604
- time: '2024-07-04T07:00:00+02:00'
  price: 0.1938889
- time: '2024-07-04T08:00:00+02:00'
  price: 0.1914085
- time: '2024-07-04T09:00:00+02:00'
  price: 0.1785462
- time: '2024-07-04T10:00:00+02:00'
  price: 0.1395479
- time: '2024-07-04T11:00:00+02:00'
  price: 0.108463
- time: '2024-07-04T12:00:00+02:00'
  price: 0.0669479
- time: '2024-07-04T13:00:00+02:00'
  price: -0.01509
- time: '2024-07-04T14:00:00+02:00'
  price: -0.028642
- time: '2024-07-04T15:00:00+02:00'
  price: 0.0548479
- time: '2024-07-04T16:00:00+02:00'
  price: 0.0895265
- time: '2024-07-04T17:00:00+02:00'
  price: 0.1139201
- time: '2024-07-04T18:00:00+02:00'
  price: 0.1455979
- time: '2024-07-04T19:00:00+02:00'
  price: 0.2009796
- time: '2024-07-04T20:00:00+02:00'
  price: 0.2506985
- time: '2024-07-04T21:00:00+02:00'
  price: 0.2823037
- time: '2024-07-04T22:00:00+02:00'
  price: 0.2518359
- time: '2024-07-04T23:00:00+02:00'
  price: 0.2257846

unit_of_measurement: €/kWh
friendly_name: Zonneplan template price sensor

Graph based on this sensor: image

Attributes of sensor.ev_smart_charging_charging


current_price: 0.2604269
EV SOC: 63
EV target SOC: 100
Charging is planned: true
Charging start time: 2024-07-03T15:00:00+02:00
Charging stop time: 2024-07-03T17:00:00+02:00
Charging number of hours: 2
raw_two_days: 
- value: 0.3540204
  start: '2024-07-03T00:00:00+02:00'
  end: '2024-07-03T01:00:00+02:00'
- value: 0.2846269
  start: '2024-07-03T01:00:00+02:00'
  end: '2024-07-03T02:00:00+02:00'
- value: 0.2664769
  start: '2024-07-03T02:00:00+02:00'
  end: '2024-07-03T03:00:00+02:00'
- value: 0.2517149
  start: '2024-07-03T03:00:00+02:00'
  end: '2024-07-03T04:00:00+02:00'
- value: 0.2517149
  start: '2024-07-03T04:00:00+02:00'
  end: '2024-07-03T05:00:00+02:00'
- value: 0.2483269
  start: '2024-07-03T05:00:00+02:00'
  end: '2024-07-03T06:00:00+02:00'
- value: 0.2674207
  start: '2024-07-03T06:00:00+02:00'
  end: '2024-07-03T07:00:00+02:00'
- value: 0.2887288
  start: '2024-07-03T07:00:00+02:00'
  end: '2024-07-03T08:00:00+02:00'
- value: 0.2883779
  start: '2024-07-03T08:00:00+02:00'
  end: '2024-07-03T09:00:00+02:00'
- value: 0.2702279
  start: '2024-07-03T09:00:00+02:00'
  end: '2024-07-03T10:00:00+02:00'
- value: 0.2579827
  start: '2024-07-03T10:00:00+02:00'
  end: '2024-07-03T11:00:00+02:00'
- value: 0.2610439
  start: '2024-07-03T11:00:00+02:00'
  end: '2024-07-03T12:00:00+02:00'
- value: 0.2604269
  start: '2024-07-03T12:00:00+02:00'
  end: '2024-07-03T13:00:00+02:00'
- value: 0.2484479
  start: '2024-07-03T13:00:00+02:00'
  end: '2024-07-03T14:00:00+02:00'
- value: 0.2262323
  start: '2024-07-03T14:00:00+02:00'
  end: '2024-07-03T15:00:00+02:00'
- value: 0.2236066
  start: '2024-07-03T15:00:00+02:00'
  end: '2024-07-03T16:00:00+02:00'
- value: 0.2243568
  start: '2024-07-03T16:00:00+02:00'
  end: '2024-07-03T17:00:00+02:00'
- value: 0.2474436
  start: '2024-07-03T17:00:00+02:00'
  end: '2024-07-03T18:00:00+02:00'
- value: 0.2648555
  start: '2024-07-03T18:00:00+02:00'
  end: '2024-07-03T19:00:00+02:00'
- value: 0.2865024
  start: '2024-07-03T19:00:00+02:00'
  end: '2024-07-03T20:00:00+02:00'
- value: 0.3382057
  start: '2024-07-03T20:00:00+02:00'
  end: '2024-07-03T21:00:00+02:00'
- value: 0.2788552
  start: '2024-07-03T21:00:00+02:00'
  end: '2024-07-03T22:00:00+02:00'
- value: 0.2573898
  start: '2024-07-03T22:00:00+02:00'
  end: '2024-07-03T23:00:00+02:00'
- value: 0.2355856
  start: '2024-07-03T23:00:00+02:00'
  end: '2024-07-04T00:00:00+02:00'

Graph based on this sensor: image

jonasbkarlsson commented 3 months ago

Maybe the update only happens once per hour? And not when tomorrow's price becomes available? Can you check again now?

hmmbob commented 3 months ago

image

Yeah, it updated now, but it would be great if an update of the price-sensor attributes also triggers an update of the ev_smart_charging_charging sensor

jonasbkarlsson commented 3 months ago

I think the problem is that the template sensor only updates when the state of the source sensor is updated (in your case sensor.zonneplan_current_electricity_tariff), and not when the attribute with all the prices are updated (in your case forecast)

The solution to this is to add triggers in the template sensor. Just reading the documentation for trigger, I think you need to do the following. Will this work?

template:
  - trigger:
    # This should generate triggers for all changes in the entity, both in it's state and it's attributes.
    - platform: state
      entity_id: sensor.zonneplan_current_electricity_tariff
    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('sensoensor.zonneplan_current_electricity_tariff' | has_value }}"
        state: "{{ states('sensor.zonneplan_current_electricity_tariff') }}"
        attributes:r.zonneplan_current_electricity_tariff') }}"
        attributes:
hmmbob commented 3 months ago

Yes, that worked! It was within milliseconds now 😄

image