Skip to content

Instantly share code, notes, and snippets.

@TonyApuzzo
Last active November 30, 2023 19:35
Show Gist options
  • Save TonyApuzzo/c0fe22458d4dae0effb47b951b464484 to your computer and use it in GitHub Desktop.
Save TonyApuzzo/c0fe22458d4dae0effb47b951b464484 to your computer and use it in GitHub Desktop.
new openevse package
###
# OpenEVSE package for Home Assistant
# Supports ONLY MQTT interactions
# Author: Tony Apuzzo
#
# 2022-11-23 - Updates for MQTT platform changes coming in HASS 2022.12
###
mqtt:
number:
- name: OpenEVSE Pilot
command_topic: openevse/rapi/in/$SC
min: 6
max: 40
state_topic: openevse/pilot
icon: mdi:current-ac
- name: OpenEVSE Charge Limit
min: 0
max: 200
command_topic: openevse/rapi/in/$SH
sensor:
- name: OpenEVSE Current Now
icon: mdi:current-ac
state_topic: >-
openevse/amp
device_class: current
state_class: measurement
unit_of_measurement: "A"
value_template: >-
{{- (value|float / 1000.0) | round(1) -}}
- name: OpenEVSE Voltage
icon: mdi:flash
state_topic: >-
openevse/voltage
device_class: voltage
state_class: measurement
unit_of_measurement: "V"
value_template: >-
{{- value | float -}}
- name: OpenEVSE Temperature
icon: mdi:thermometer
state_topic: >-
openevse/temp1
device_class: temperature
state_class: measurement
unit_of_measurement: '°C'
value_template: >-
{{- (value | float / 10.0) | round(0) -}}
- name: OpenEVSE Pilot
icon: mdi:current-ac
state_topic: >-
openevse/pilot
device_class: current
unit_of_measurement: "A"
value_template: >-
{{- value | int -}}
- name: OpenEVSE Total Energy
state_topic: >-
openevse/wh
device_class: energy
state_class: total_increasing
unit_of_measurement: "kWh"
value_template: >-
{{- (value | float / 1000.0) | round(0) -}}
template:
- trigger:
- platform: mqtt
topic: 'openevse/rapi/in/#'
sensor:
- name: OpenEVSE RAPI In
state: '{{- trigger.topic.split("/") | last -}}'
attributes:
topic: '{{- trigger.topic -}}'
payload: '{{- trigger.payload -}}'
- trigger:
- platform: mqtt
topic: 'openevse/rapi/out'
sensor:
- name: OpenEVSE RAPI Out
state: >-
{{- trigger.payload | regex_replace('^\$([^ ]+) ?(.*)\^[0-9A-F]*$', '\\2') }}
attributes:
ok: >-
{{- trigger.payload | regex_replace('^\$([^ ]+) ?(.*)\^[0-9A-F]*$', '\\1') == 'OK' -}}
topic: '{{- trigger.topic -}}'
payload: '{{- trigger.payload -}}'
command: '{{- states("sensor.openevse_rapi_in") -}}'
icon: >-
{{ "mdi:eye" if trigger.payload is match('\$OK') else "mdi:alert" }}
- trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GH'
sensor:
- name: OpenEVSE Energy Limit
device_class: energy
unit_of_measurement: "kWh"
state: >-
{{- states('sensor.openevse_rapi_out') | int -}}
- trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GU'
sensor:
- name: OpenEVSE Session Energy
device_class: energy
state_class: measurement
unit_of_measurement: "kWh"
# '41660042 1328789'
state: >-
{{- (states('sensor.openevse_rapi_out').split(' ')[0]
| float / 3600000.0) | round(2) -}}
- trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GE'
sensor:
- name: OpenEVSE Service Level
# Bitfield
# Bit 1: Off = Level 1
# Bit 1: On = Level 2
# Bit 6: Off = Use Level designation from bit 1
# Bit 6: On = Automaticly determine service level
state: >-
{%- set flags = states('sensor.openevse_rapi_out').split(' ')[1] | int(0, 16) -%}
{%- if (flags | bitwise_and(0x20)) == 0 -%}
Auto
{%- elif flags | bitwise_and(0x1) -%}
L2
{%- else -%}
L1
{%- endif -%}
- trigger:
- platform: mqtt
topic: openevse/state
sensor:
- name: OpenEVSE State
state: >-
{%- set value = trigger.payload -%}
{%- if value | regex_match('^[01]$') -%}
Not Connected
{%- elif value | regex_match('^2$') -%}
Connected
{%- elif value | regex_match('^3$') -%}
Charging
{%- elif value | regex_match('^([456789]|10)$') -%}
Error ({{- value -}})
{%- elif value | regex_match('^254$') -%}
Sleeping
{%- elif value | regex_match('^255$') -%}
Disabled
{%- else -%}
Unknown ({{- value -}})
{%- endif -%}
icon: >-
{%- set value = trigger.payload -%}
{%- if value | regex_match('^[01]$') -%}
mdi:power-plug-off
{%- elif value | regex_match('^2$') -%}
mdi:car-electric
{%- elif value | regex_match('^3$') -%}
mdi:battery-charging
{%- elif value | regex_match('^([456789]|10)$') -%}
mdi:battery-alert
{%- elif value | regex_match('^254$') -%}
mdi:sleep
{%- elif value | regex_match('^255$') -%}
mdi:power-off
{%- else -%}
mdi:battery-unknown
{%- endif -%}
attributes:
topic: '{{- trigger.topic -}}'
payload: '{{- trigger.payload -}}'
switch:
- platform: template
switches:
openevse_enablement:
friendly_name: OpenEVSE Disable/Enable
icon_template: mdi:power
value_template: >-
{{- not(
is_state_attr('sensor.openevse_state', 'payload', 254) or
is_state_attr('sensor.openevse_state', 'payload', 255))
-}}
turn_on:
service: mqtt.publish
data:
topic: 'openevse/rapi/in/$FE'
turn_off:
service: mqtt.publish
data:
topic: 'openevse/rapi/in/$FD'
script:
openevse_rapi_ge:
alias: OpenEVSE Get Settings
sequence:
service: mqtt.publish
data:
topic: 'openevse/rapi/in/$GE'
openevse_rapi_gu:
alias: OpenEVSE Get Energy Usage
sequence:
service: mqtt.publish
data:
topic: 'openevse/rapi/in/$GU'
openevse_rapi_gh:
alias: OpenEVSE Get Energy Limit
sequence:
service: mqtt.publish
data:
topic: 'openevse/rapi/in/$GH'
automation openevse:
- alias: Request update of settings flags
mode: single
trigger:
- platform: state
entity_id: sensor.openevse_state
- platform: state
entity_id: sensor.openevse_pilot
action:
- service: script.openevse_rapi_gh
- wait_for_trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GH'
for: '00:00:01'
- service: script.openevse_rapi_gu
- wait_for_trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GU'
for: '00:00:01'
- service: script.openevse_rapi_ge
- wait_for_trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GE'
for: '00:00:01'
- alias: Enforce charge limit
mode: single
trigger:
- platform: state
entity_id: sensor.openevse_state
to:
- Connected
- Charging
action:
- service: mqtt.publish
data:
topic: 'openevse/rapi/in/$SH'
payload_template: >-
{{- states('number.openevse_charge_limit') -}}
- wait_for_trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$SH'
for: '00:00:01'
- service: script.openevse_rapi_gh
- wait_for_trigger:
- platform: state
entity_id: sensor.openevse_rapi_out
attribute: command
to: '$GH'
for: '00:00:01'
@ruimarinho
Copy link

@TonyApuzzo one thing to note is that the OpenEVSE team is considering deprecating RAPI over MQTT.

IMPORTANT: It is no longer recommended to use RAPI API if EVSE also had a WiFi module fitted, since use of the RAPI API will conflict with the WiFi module, instead the HTTP API should be used to control the WiFi module instead of the controller via RAPI.

User RAPI will be removed in a future version of the firmware or at least limited to $Gx commands

Source: https://openevse.stoplight.io/docs/openevse-wifi-v4/ZG9jOjI5NzAxMTA1-rapi-api

@TonyApuzzo
Copy link
Author

@ruimarinho thank you for the warning, but it doesn't seem that the HTTP API is available for the legacy ESP8266 WiFi module, only with the newer v4 ESP32 gateway. I'm personally not going to upgrade either the base firmware nor the WiFi module any time soon based on my usage.

@quentindavid
Copy link

Hi, it seems the usage of the MQTT platform will change soon. I have some warnings :

Manually configured MQTT sensor(s) found under platform key sensor.

Please move the configuration to the mqtt integration key and restart Home Assistant to fix this issue. See the documentation, for more information.

Have you already fixed it ?

@TonyApuzzo
Copy link
Author

@quentindavid I am not using the OpenEVSE charger any more, so I can't test any changes, but I made the change based on documentation and verified that it loads without errors or warnings in HASS.

Let me know if it works or not.

@quentindavid
Copy link

Hi @TonyApuzzo ! Thanks a lot for the refactoring :) It seems to work except this warning

Manually configured MQTT number(s) found under platform key number.

Please move the configuration to the mqtt integration key and restart Home Assistant to fix this issue. See the [documentation](https://www.home-assistant.io/integrations/number.mqtt/#new_format), for more information.

I still need to dig a little to be sure everything is running fine except that.

@TonyApuzzo
Copy link
Author

TonyApuzzo commented Nov 23, 2022

Ah yeah, I should've checked the other platforms. It seems that number is the only other one I use that's changing, so I updated that one too.

@quentindavid
Copy link

Yeah it seems to have done the trick. Thanks a lot @TonyApuzzo !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment