I just noticed that my REST sensor was no longer updating (I’d suspected this for a while, but I hadn’t really payed any attention recently.) I don’t know when it broke but it was a while ago.
Turns out that the “digest” authentication does not work anymore with the OpenEVSE service. I changed to “basic” and it started working again.
Second, I also made some changes to clean up a few potential warnings by avoiding use of None or “unknown” in cases where values aren’t available.
Thirdly, I reviewed the source code and updated my state values to match the OpenEVSE, e.g., “Ready” instead of “Disconnected”, etc.
sensor:
# Use OpenEVSE RAPI API to Retrieve the active charging current
# https://www.home-assistant.io/components/sensor.rest/
# Example:
# curl --digest --user 'user:pass' 'http://openevse.local/r?json=1&rapi=$GU'
# $OK 40629846 165327^27
- platform: rest
name: "OpenEVSE RAPI $GU"
resource: 'http://openevse.local/r?json=1&rapi=$GU'
force_update: yes
authentication: basic
username: !secret OPENEVSE_USERNAME
password: !secret OPENEVSE_PASSWORD
headers:
User-Agent: Home Assistant
Content-Type: application/json
json_attributes: [ 'cmd', 'ret' ]
value_template: '{{state_attr("sensor.openevse_rapi_gu", "ret")}}'
# Custom Sensors
- platform: template
sensors:
openevse_session_energy:
friendly_name: "OpenEVSE Session Energy"
icon_template: mdi:gauge
unit_of_measurement: 'kWh'
value_template: >-
{%- if states('sensor.openevse_rapi_gu') == 'unknown' -%}
0.0
{%- else -%}
{%- set ret = state_attr('sensor.openevse_rapi_gu', 'ret') -%}
{%- if ret == None -%}
0.0
{%- else -%}
{%- set rgx = '(?<=^\$OK )[0-9]+' -%}
{%- if ret|regex_search(rgx) -%}
{%- set v = ret | regex_findall_index(rgx) -%}
{{- (v|float / 3600000)|round(2) -}}
{%- else -%}
0.0
{%- endif -%}
{%- endif -%}
{%- endif -%}
openevse_state:
friendly_name: "OpenEVSE State"
icon_template: >-
{%- if states('sensor.openevse_mqtt_state') | regex_match('^[01]$') -%}
mdi:power-plug-off
{%- elif is_state('sensor.openevse_mqtt_state', '2') -%}
mdi:car-electric
{%- elif is_state('sensor.openevse_mqtt_state', '3') -%}
mdi:battery-charging
{%- elif states('sensor.openevse_mqtt_state') | regex_match('^([456789]|10)$') -%}
mdi:battery-alert
{%- elif is_state('sensor.openevse_mqtt_state', '254') -%}
mdi:sleep
{%- elif is_state('sensor.openevse_mqtt_state', '255') -%}
mdi:power-off
{%- else -%}
mdi:battery-unknown
{%- endif -%}
value_template: >-
{%- if states('sensor.openevse_mqtt_state') | regex_match('^[01]$') -%}
Ready
{%- elif is_state('sensor.openevse_mqtt_state', '2') -%}
Connected
{%- elif is_state('sensor.openevse_mqtt_state', '3') -%}
Charging
{%- elif states('sensor.openevse_mqtt_state') | regex_match('^([456789]|10)$') -%}
Error
{%- elif is_state('sensor.openevse_mqtt_state', '254') -%}
Sleep
{%- elif is_state('sensor.openevse_mqtt_state', '255') -%}
Disabled
{%- else -%}
Unknown-{{- value -}}
{%- endif -%}
# OpenEVSE MQTT Tracker
- platform: mqtt
name: "OpenEVSE Temp 1"
state_topic: "openevse/temp1"
unit_of_measurement: '°F'
device_class: temperature
value_template: >-
{%- macro C2F(temperature) -%}
{% set tmp = (((temperature *9) /50.0) + 32) %}
{{- "%0.2f" % tmp -}}
{%- endmacro -%}
{{- C2F(value|float)|float -}}
- platform: mqtt
name: "OpenEVSE Charging Current"
icon: mdi:current-ac
state_topic: "openevse/amp"
unit_of_measurement: "A"
value_template: >-
{{- (value|float / 1000.0) | round(1) -}}
- platform: mqtt
name: "OpenEVSE Pilot Current"
icon: mdi:current-ac
state_topic: "openevse/pilot"
unit_of_measurement: "A"
value_template: >-
{{- value|int -}}
- platform: mqtt
name: "OpenEVSE Total Energy"
icon: mdi:gauge-full
state_topic: "openevse/wh"
unit_of_measurement: "kWh"
value_template: >-
{{- value|float / 1000 | round(2) -}}
- platform: mqtt
name: "OpenEVSE MQTT State"
state_topic: "openevse/state"
And finally, Someone was asking about updating the charger in addition to only reading data. This is relatively straightforward by using the RAPI REST API. In theory MQTT is more efficient, but it is easier to get the REST commands working first, so that is what I did.
My first configuration for write control is a simple switch to enable/disable the charger.
rest_command:
openevse_enable:
url: 'http://openevse.local./r?json=1&rapi=$FE'
username: !secret OPENEVSE_USERNAME
password: !secret OPENEVSE_PASSWORD
headers:
User-Agent: Home Assistant
openevse_disable:
url: 'http://openevse.local./r?json=1&rapi=$FD'
username: !secret OPENEVSE_USERNAME
password: !secret OPENEVSE_PASSWORD
headers:
User-Agent: Home Assistant
openevse_sleep:
url: 'http://openevse.local./r?json=1&rapi=$FS'
username: !secret OPENEVSE_USERNAME
password: !secret OPENEVSE_PASSWORD
headers:
User-Agent: Home Assistant
# Switches
switch:
- platform: template
switches:
openevse_enable:
friendly_name: "OpenEVSE Enable"
value_template: >-
{{- states('sensor.openevse_mqtt_state') | regex_match ("^[0123]$") -}}
icon_template: >-
{%- if states('sensor.openevse_mqtt_state') | regex_match ("^[0123]$") -%}
mdi:power-plug
{%- else -%}
mdi:power-plug-off
{%- endif -%}
turn_off:
- service: rest_command.openevse_sleep
turn_on:
- service: rest_command.openevse_enable
Additional RAPI commands: