evcc icon indicating copy to clipboard operation
evcc copied to clipboard

Feature Request: Native Home Assistant Template for All Device Types

Open scruysberghs opened this issue 1 year ago • 10 comments

Feature Request: Home Assistant Integration Template

Summary

Add a native Home Assistant template type for evcc to simplify integration with Home Assistant sensors and services, similar to the existing homeassistant-switch template.

Motivation

Currently, integrating evcc with Home Assistant requires extensive custom HTTP configurations with repetitive patterns. Every sensor requires manual specification of:

  • Full HTTP URLs to Home Assistant API
  • Authorization headers with long-lived access tokens
  • Standard jq transformations
  • Timeout and error handling

This results in verbose, error-prone configurations with significant duplication.

Proposed Solution

Add a homeassistant template type that works across all evcc device types (meters, chargers, vehicles, tariffs).

Example Configuration

Current (verbose):

meters:
  - name: grid_meter
    type: custom
    power:
      source: http
      uri: http://homeassistant:8123/api/states/sensor.grid_power
      method: GET
      headers:
        - Authorization: Bearer <long-lived-access-token>
      insecure: true
      jq: .state|tonumber
      timeout: 2s
    currents:
      - source: http
        uri: http://homeassistant:8123/api/states/sensor.grid_current_l1
        method: GET
        headers:
          - Authorization: Bearer <long-lived-access-token>
        insecure: true
        jq: .state|tonumber
        timeout: 2s
      # ... repeat for L2, L3

Proposed (concise):

meters:
  - name: grid_meter
    type: homeassistant
    host: homeassistant:8123  # optional, defaults to homeassistant:8123
    token: ${HA_TOKEN}  # Home Assistant long-lived access token
    power: sensor.grid_power
    currents:
      - sensor.grid_current_l1
      - sensor.grid_current_l2
      - sensor.grid_current_l3

More examples:

Charger:

chargers:
  - name: wallbox
    type: homeassistant
    token: ${HA_TOKEN}
    status: sensor.wallbox_status
    enabled: switch.wallbox_enabled
    enable:
      service: switch.toggle
      entity: switch.wallbox_enabled
    maxcurrent:
      service: number.set_value
      entity: number.wallbox_max_current
      parameter: value  # maps to {{ .maxcurrent }}
    power: sensor.wallbox_power
    currents:
      - sensor.wallbox_current_l1
      - sensor.wallbox_current_l2
      - sensor.wallbox_current_l3

Vehicle:

vehicles:
  - name: my_car
    type: homeassistant
    token: ${HA_TOKEN}
    title: My Electric Car
    capacity: 75
    soc: sensor.car_battery_level
    range: sensor.car_range
    odometer: sensor.car_odometer
    limitsoc: number.car_charge_limit

Tariff:

tariffs:
  grid:
    type: homeassistant
    token: ${HA_TOKEN}
    forecast: 
      entity: sensor.energy_prices
      attribute: forecast  # uses .attributes.forecast from HA

Implementation Details

Template Parameters:

  • host: Home Assistant host (default: homeassistant:8123)
  • token: Long-lived access token (supports env vars like ${HA_TOKEN})
  • timeout: Request timeout (default: 2s)
  • insecure: Skip TLS verification (default: true for local HA)

Entity Types:

  • Sensors (sensor.*): GET /api/states/{entity_id}, extract .state
  • Switches (switch.*): GET for status, POST to /api/services/switch/turn_{on|off}
  • Numbers (number.*): GET for status, POST to /api/services/number/set_value
  • Services: POST to /api/services/{domain}/{service} with entity_id and parameters

Automatic Transformations:

  • Numeric conversion: jq: .state|tonumber automatically applied to numeric fields
  • Boolean conversion: jq: '.state == "on"' for switch states
  • Status mapping: Automatic mapping for charger status (A-F states)
  • Array attributes: Support for .attributes.{name} extraction

Error Handling:

  • Automatic retry with exponential backoff
  • Graceful fallback for unavailable entities
  • Detailed logging for troubleshooting

Benefits

  1. Dramatically reduced configuration: ~80% reduction in YAML lines
  2. Less error-prone: No manual URL construction or jq expressions
  3. Consistent patterns: Standardized approach across all device types
  4. Better maintainability: Token changes in one place
  5. Environment variable support: Secure token management
  6. Automatic type handling: No manual number/boolean conversions

Implementation Scope

This template should work for:

  • ✅ Meters: power, currents, energy readings
  • ✅ Chargers: status, enabled state, current limits, power measurements
  • ✅ Vehicles: SoC, range, odometer, charge limits
  • ✅ Tariffs: price forecasts from HA energy sensors
  • ✅ Batteries: power, SoC, capacity (future enhancement)

Migration Path

  1. Implement template alongside existing custom type
  2. Provide migration guide with examples
  3. Consider config validation warnings for overly verbose HA integrations
  4. Document best practices for HA sensor naming conventions

Related Work

  • Existing homeassistant-switch template shows this pattern works
  • Similar patterns exist in other home automation tools (Node-RED, etc.)
  • This follows evcc's template philosophy of simplifying common patterns

Example Real-World Use Case

A real user configuration with 2 Smappee chargers, 3 meters, 2 vehicles, and tariff integration currently requires ~200 lines of repetitive HA HTTP calls. With this template, the same functionality would reduce to ~40 lines while being more maintainable and less error-prone.

The template would eliminate the need for users to:

  • Memorize HA API endpoints
  • Handle HTTP headers and authentication manually
  • Write jq expressions for basic data extraction
  • Repeat timeout and error handling configuration
  • Debug HTTP integration issues

This would significantly lower the barrier to entry for evcc + Home Assistant integration.

scruysberghs avatar Aug 26 '25 14:08 scruysberghs

Seems you want a HA Sensor (and maybe Actor) Plugin? Afaik there‘s no strict model for such in HA. Happy to take a PR.

If what you are suggesting is a Better choice- also happy to take a PR. Since core team isn‘t using HA, it won‘t be working on this due to lack of experience.

andig avatar Aug 26 '25 16:08 andig

This results in verbose, error-prone configurations with significant duplication.

You can always declare sub templates for this purpose to modularise the template.

andig avatar Aug 26 '25 16:08 andig

@Threadersteller: Why ??? Next to Evcc ADDON is a HA Evcc INTEGRATION, offering 50+ Entities (or Sensors...) and since a long time closing the/any gap. May be used for tons of Monitoring, Automations and whatever usecases. I used it for some missing stuff a long time and its working always silently in the back automatic and smooth.

HA Addon style using Evcc was best and easiest way to get Evcc running noobproof. The coexistence of Evcc Addon AND Integration, is giving all someone may need, as far as I may realize 🤷🏼‍♂️.

What do you really miss at the moment, better said, what do you want zu manage which isn't already possible ?

Merwases avatar Aug 26 '25 20:08 Merwases

@Threadersteller: Why ??? Next to Evcc ADDON is a HA Evcc INTEGRATION, offering 50+ Entities (or Sensors...) and since a long time closing the/any gap. May be used for tons of Monitoring, Automations and whatever usecases. I used it for some missing stuff a long time and its working always silently in the back automatic and smooth.

HA Addon style using Evcc was best and easiest way to get Evcc running noobproof. The coexistence of Evcc Addon AND Integration, is giving all someone may need, as far as I may realize 🤷🏼‍♂️.

What do you really miss at the moment, better said, what do you want zu manage which isn't already possible ?

The home assistant evcc integration at the moment only shows evcc sensor values in home assistant, it does not offer a way to easily use home assistant sensors in evcc. If I for example want to use use an existing full featured car api integration in home assistant or if there is a charger with better compatibility in home assistant (like some OCPP chargers for example, https://github.com/evcc-io/evcc/discussions/18414, or chargers that are not supported in evcc but do work in HA like https://github.com/myny-git/smappee_ev). It often prevents having to poll the same external api twice (energy prices, solar predictions ... ) or having to use things like modbus proxy's to have home assistant and evcc read out the same solar inverter. In my setup Home Assistant is currently core energy management system (with emhass optimiser) and evcc is mainly the UI to control EV charging. That priority might switch with more and more features being added to evcc (like the optimiser) but for now this is how I, and frankly quite al lot of people use it.

scruysberghs avatar Aug 27 '25 08:08 scruysberghs

Hmm, Evcc is mainly for PV Charging and I think its not the right way to overequippe Evcc with more and more fancy stuff only some may use. HA offers tons of Addons and even more Integration. If any modbus is used, the Modbus Proxy Addon is one way to easy share it with all someone may use too. Anything may be connected/working by Automations, f. i. those Tibber stuff, weather forecast and so on (which I don't use as its making not really sense to me).

But if you may see any advantage using weather forecast, Tibber or simmilar and, and, and at PV charging, ALL can be made working together by clever Automations, set up the kind of : "if this and this and... and those and those is, then do/start/stop this and this and this"... and many more solutions for nearly everything. Implementig all thos specials into Evcc, will guide staight to more and more problems and hit peoole who don't want all this fany stuff at all. Keep it simple, keep it straight, keep it rock solid this way. Please Maintainers do not include each fancy stuff also in future. Who thinks he might need more, should include it using HA's Automations... as already mentioned Evcc INTEGRATION beside the Evcc Addon (2 upcycling things benefitting from each other).

Do you use both already... and Automations ? If not, start to do and the is no need for anything to add at Evcc. No need to let Evcc do all stuff, HA is the "machine" and Evcc "just" one gear ☝️.

Merwases avatar Aug 27 '25 11:08 Merwases

Let's please return to topic. Happy to include a HA charger/meter/tariff etc. if smdb. wants to implement it. For time being, there's no integration to HA that cannot be build using the existing plugins.

andig avatar Aug 27 '25 11:08 andig

This results in verbose, error-prone configurations with significant duplication.

You can always declare sub templates for this purpose to modularise the template.

Can you actually do that in evcc.yaml? I tried includes before but couldn't get it to work. Also tried yaml anchors and variabels (like for token) but those were not parsed either.

scruysberghs avatar Aug 29 '25 06:08 scruysberghs

You can use golang templates, for example this pattern is used in some tempaltes:

  {{- define "uri" -}}
  http://{{ .host }}/api/v1
  {{- end }}

...

  uri: {{ include "uri" . }}/status/74

andig avatar Aug 29 '25 09:08 andig

That said, happy to take a PR for a HA specific template (or a set of templates)!

andig avatar Aug 29 '25 09:08 andig

Is it already possible to use Homeassistent templates across all devices? Also via UI?

Gifford47 avatar Mar 03 '26 21:03 Gifford47