firstof9 / openevse

OpenEVSE Integration for Home Assistant
MIT License
67 stars 12 forks source link

Add option for mqtt #15

Closed omriasta closed 2 years ago

omriasta commented 3 years ago

It would be great to add the option to use mqtt as well to the config flow.... I hope this is the place for feature requests... I'll try to give it a go on my end though my python is pretty basic

firstof9 commented 3 years ago

I'll see what I can hammer out.

omriasta commented 3 years ago

Great, if you create a branch and get me the general direction I can try to test and fill in anything missing.

firstof9 commented 3 years ago

Would you be able to provide a dump of the mqtt topic? OpenEVSE 4.1.0 doesn't connect to my MQTT broker for some unknown reason or I'd grab a dump myself.

omriasta commented 3 years ago

Will do as soon as I get home

omriasta commented 2 years ago

Here you go:

Listen to a topic

Topic to subscribe to
openevse/#

Message 16 received on openevse/srssi at 8:14 AM:
-70
QoS: 0 - Retain: false
Message 15 received on openevse/divertmode at 8:14 AM:
1
QoS: 0 - Retain: false
Message 14 received on openevse/freeram at 8:14 AM:
209640
QoS: 0 - Retain: false
Message 13 received on openevse/manual_override at 8:14 AM:
0
QoS: 0 - Retain: false
Message 12 received on openevse/colour at 8:14 AM:
5
QoS: 0 - Retain: false
Message 11 received on openevse/vehicle at 8:14 AM:
0
QoS: 0 - Retain: false
Message 10 received on openevse/state at 8:14 AM:
254
QoS: 0 - Retain: false
Message 9 received on openevse/temp4 at 8:14 AM:
240
QoS: 0 - Retain: false
Message 8 received on openevse/temp3 at 8:14 AM:
false
QoS: 0 - Retain: false
Message 7 received on openevse/temp2 at 8:14 AM:
false
QoS: 0 - Retain: false
Message 6 received on openevse/temp1 at 8:14 AM:
145
QoS: 0 - Retain: false
Message 5 received on openevse/temp at 8:14 AM:
145
QoS: 0 - Retain: false
Message 4 received on openevse/wh at 8:14 AM:
455778
QoS: 0 - Retain: false
Message 3 received on openevse/pilot at 8:14 AM:
40
QoS: 0 - Retain: false
Message 2 received on openevse/voltage at 8:14 AM:
240
QoS: 0 - Retain: false
Message 1 received on openevse/amp at 8:14 AM:
0
QoS: 0 - Retain: false
Message 0 received on openevse/announce/e154 at 8:14 AM:
{
    "state": "connected",
    "id": "9c9xxxxxxx54",
    "name": "openevse-e154",
    "mqtt": "openevse",
    "http": "http://192.168.XX.XX/"
}
QoS: 0 - Retain: true
firstof9 commented 2 years ago

Thanks, I'll see what I can do

firstof9 commented 2 years ago

Do you think just enabling auto discovery would be enough?

omriasta commented 2 years ago

I think the best way would be to create a property for each device which will define if it is mqtt or http, then we would have to create all the entities based on that property ie if charger.connection = http then use existing way to create entities, if charger.connection = mqtt then create entities using the mqtt connection..... Also, the config flow should have an initial step which should ask if you are adding http or mqtt and then provide appropriate fields (mqtt would have to provide a topic and no authentication rather than an ip/host).

firstof9 commented 2 years ago

I think the easiest way would be to have openevse publish the discovery topics via the firmware honestly, that way the integration doesn't have to reinvent the wheel.

omriasta commented 2 years ago

Not sure I understand how that would work? Currently I have yaml entries for every entity with the mqtt platform set. If the integration autodiscovered the mqtt topic how would it know how to setup the switch, sensors etc.?

firstof9 commented 2 years ago

Auto discovery sets up the switch/sensors/etc based on the topics here's an example:

topic: openevse/pilot value: 40

Discovery topic: homeassistant/sensor/openevse/pilot/config value: {"name": "openevse pilot", "unit_of_measure": "A", "icon": "mdi:sine-wave", "state_topic": "openevse/pilot", "unique_id": "9c9xxxxxxx54_pilot" }

The discovery topic tells Home Assistant to look at openevse/pilot for the sensor data and generate the entity based on the other data contained in homeassistant/sensor/openevse/pilot/config

omriasta commented 2 years ago

awesome, was not aware how that worked. I guess that would be the way to go then...would it be able to discover if the base topic is not openevse? In my instance I have 2 units and each has it's own topic...Sorry just not that familiar with how the discovery works.....

firstof9 commented 2 years ago

The topics can be anything, the state topic is where you'd point everything to, ideally the device would generate all this. I'll see if maybe I can piece together a PR for OpenEVSE to enable HomeAssistant auto discovery, honestly shouldn't be too difficult since it's just publishing extra topics.

Or submit a feature request, I'm terrible with UI stuff lol

omriasta commented 2 years ago

That would be great, just hope that they update the 2.9.x version as well and not just the 4.x version. The 2.9.x mqtt looks like this and doesn't have the initial announcement that the 4.x has on openevse/announce (as you can see above) :

Listen to a topic

Topic to subscribe to
openevse_work/#

Message 12 received on openevse_work/srssi at 10:42 AM:
-70
QoS: 0 - Retain: false
Message 11 received on openevse_work/divertmode at 10:42 AM:
1
QoS: 0 - Retain: false
Message 10 received on openevse_work/freeram at 10:42 AM:
15312
QoS: 0 - Retain: false
Message 9 received on openevse_work/state at 10:42 AM:
254
QoS: 0 - Retain: false
Message 8 received on openevse_work/temp3 at 10:42 AM:
false
QoS: 0 - Retain: false
Message 7 received on openevse_work/temp2 at 10:42 AM:
false
QoS: 0 - Retain: false
Message 6 received on openevse_work/temp1 at 10:42 AM:
205
QoS: 0 - Retain: false
Message 5 received on openevse_work/wh at 10:42 AM:
346953
QoS: 0 - Retain: false
Message 4 received on openevse_work/pilot at 10:42 AM:
40
QoS: 0 - Retain: false
Message 3 received on openevse_work/voltage at 10:42 AM:
240
QoS: 0 - Retain: false
Message 2 received on openevse_work/amp at 10:42 AM:
0
QoS: 0 - Retain: false
Message 1 received on openevse_work/emoncms_message at 10:42 AM:
null
QoS: 0 - Retain: false
Message 0 received on openevse_work/emoncms_connected at 10:42 AM:
1
QoS: 0 - Retain: false
omriasta commented 2 years ago

I guess the change could be made here and possibly also copied to 2.9.x and then recompiled: https://github.com/OpenEVSE/ESP32_WiFi_V4.x/blob/80f66b4026e40bf0773aa7267c11d4e7044a9355/src/mqtt.cpp#L188

I assume the mqtt discovery you are mentioning is this: https://www.home-assistant.io/docs/mqtt/discovery/

firstof9 commented 2 years ago

Yes that's the discovery info.

omriasta commented 2 years ago

I have been messing around with it a bit by manually creating entities by publishing to mqtt....sensors are easy but I can't figure out the switch... I assume I need a value_template for the state {{ "off" if value == 254 else "on"}} but every variation I have tried so far and looking through the forums, I just couldn't get it to work....

firstof9 commented 2 years ago

that one's a little trickier, I'll see if I can get you a good topic for it, I assume for divertmode and/or manual_override right?

omriasta commented 2 years ago

I usually use the $FS and $FE RAPI commands for the action so the switch state should be off when OpenEVSE reports the state 254 or 255 and otherwise it should be on. I have mine slightly different where the switch is called OpenEVSE enable/disable as opposed to your integration which is on to activate sleep and off to start charging.

firstof9 commented 2 years ago

Which wifi firmware are you running?

omriasta commented 2 years ago

wifi firmware is 4.1.0 and openevse firmware is 7.1.3 BUT the openevse hardware itself is actually v4 not the latest v5.5

firstof9 commented 2 years ago

How do you currently have the switch setup for MQTT control, or do you not at this time?

omriasta commented 2 years ago

Using @TonyApuzzo package found here https://github.com/TonyApuzzo/home-assistant-config/blob/master/packages/openevse2.yaml

  - 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
          {%- elif value | regex_match('^3$') -%}
            mdi:car-electric
          {%- 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:ev-station
        value_template: >-
          {{-
            not state_attr('sensor.openevse_state', 'payload') in (254, 255)
          -}}
        turn_on:
          service: script.openevse_rapi_fe
        turn_off:
          service: script.openevse_rapi_fd
firstof9 commented 2 years ago

ah it uses a script, I think that's the only way to really get the switch part working due to the way the topics are setup

omriasta commented 2 years ago

Looking at the info, I think it's doable for the action though haven't even tried that yet (using payload_on and payload_off settings) Just couldn't figure the value_template option to get state of switch, according to the documentation it should be doable just couldn't figure the proper syntax to get it to work

omriasta commented 2 years ago

I think I see what you mean now.... I temporarily setup the switch with the below code. Now the switch was following the actual state (given only those 2 options for state obviously). I then tried to work on the actual commands but homeassistant expects one topic with different payloads and openevse expects 2 different topics with no payload. I guess the only way to make this easier to setup would be to completely overhaul the mqtt implementation in OpenEVSE which doesn't seem likely to me...

I guess I'll stick with your integration for the local and the yaml package for the remote device for now but I guess they should look into changing up the mqtt implementation in openevse....it just doesn't make that much sense to me the way it is currently functioning....

service: mqtt.publish
data:
  topic: homeassistant/switch/openevse/config
  payload: >-
    {"name": "openevse-test", "command_topic": "openevse/rapi/in/",
    "state_topic": "openevse/state", "state_off": "254", "state_on": "1"}