XavierBerger / Solar-Router-for-ESPHome

Solar Router for ESPHome
https://xavierberger.github.io/Solar-Router-for-ESPHome/
GNU General Public License v3.0
11 stars 2 forks source link

power meter source #15

Closed tikismoke closed 1 week ago

tikismoke commented 5 months ago

Hi xavier, could you update the doc and/or some parts of the code to use another source of power consumption?

For example a value directly comming from homeassistant or the direct ip of a shelly em or anything llike this ? and add some security if value of external meter is NaN.

tikismoke commented 5 months ago

https://shelly-api-docs.shelly.cloud/gen1/#shelly-em-emeter-index

image Exemple of mesure from a shelly EM second entry (1st GEN) and this from entry 1 (emetter/0): image

XavierBerger commented 5 months ago

I have a prototype of configuration to use Shelly EM as power_meter.
As I don't own such a hardware, I can't validate if it works or not.
If you can give a try, I'll really appreciate and thanks to your feedback, I'll be able to add this new power_meter to the project.

To test is, you can simply use it as power_meter with the following configuration:

substitutions:
  # Shelly Ip address (and port if different from 80)
  power_meter_ip_address: "192.168.1.26:8000"
  # Shelly emeter index
  emeter_index : "0"

packages:
  power_meter:
    url: https://github.com/XavierBerger/Solar-Router-for-ESPHome/
    file: solar_router/power_meter_shelly_em.yaml
    ref: feature/shelly_em
    refresh: 5s

This code is referring to a development branch feature/shelly_em which is gathering the development file for Shelly EM.

Once I'll have the confirmation that it's effectively working, I'll update the doc and officially publish this power meter.

Thanks for your help.

tikismoke commented 5 months ago

Will try this before the WE i hope, and tell you 👍

tikismoke commented 4 months ago

Need to make more test but it crash at every http request. Bugs already exist in esphome but even with last update 2024.6.3 still reset when the esp made a request. I'm going to try with an ha sensor and make a pr for this.

XavierBerger commented 4 months ago

I tried my code with a simulator and I found an issue in http_request when Content-Lenght is missing.

I did add a comment in an issue already present on esphome : https://github.com/esphome/issues/issues/5949#issuecomment-2198563803

Can you paste the "En tête" of the request on the Shelly-EM? It will help to understand if the issue is the same.

Note: on configuration, it is also required to define power_meter_ip_address pointing to the Shelly I forgot to add it on the substitution section described upper (https://github.com/XavierBerger/Solar-Router-for-ESPHome/issues/15#issuecomment-2188738314)

tikismoke commented 4 months ago

image This his the header

dloriot commented 4 months ago

It would be great if the code could handle the Shelly Gen2 devices.

https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/EM1

Thanks for your help.

tikismoke commented 4 months ago

It would be great if the code could handle the Shelly Gen2 devices.

@XavierBerger That's the corresponding API request/response:

https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/EM1/#em1getstatus-example

dloriot commented 4 months ago

I tried this code but nothing is displayed on the console. https://github.com/dloriot/Solar-Router-for-ESPHome/blob/dev/solar_router/power_meter_proxy_client.yaml

esp

My esphome yaml

substitutions:
  name: esphome-web-6757a0
  friendly_name: ESPHome Web 6757a0
  # Regulator configuration ------------------------------------------------------
  # Define GPIO pin connected to the relay gate.
  regulator_gate_pin: GPIO22
  # LEDs -------------------------------------------------------------------------
  # Green LED is reflecting regulation status
  # Yellow LED is reflecting power meter status
  green_led_pin: GPIO19
  yellow_led_pin: GPIO18
  # Power meter source -----------------------------------------------------------
  # Define ip address of Shelly Power Meter
  power_meter_ip_address: "192.168.x.x"
  # Regulator --------------------------------------------------------------------
  regulator: "ac_dimmer"

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  min_version: 2024.6.0
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: '1.0'

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
- platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:

packages:
  power_meter:
    url: https://github.com/dloriot/Solar-Router-for-ESPHome
    file: solar_router/power_meter_proxy_client.yaml
    ref: dev
    refresh: 5s
  regulator:
    url: https://github.com/dloriot/Solar-Router-for-ESPHome
    file: solar_router/regulator_relay.yaml
    refresh: 5s
  solar_router:
    url: https://github.com/dloriot/Solar-Router-for-ESPHome
    file: solar_router/solar_router_engine.yaml
    refresh: 5s
XavierBerger commented 4 months ago

@dloriot Forgot this comment, I was answering the wrong question thinking it was coming from tikismoke


~~ In your configuration, ~~

substitution:
  ...

~~ In this section, emeter_index is missing. This is mandatory to let know which index has to be used. ~~ ~~ Note: if it is always 0, I can just remove it from configuration and write this value directly int the code.~~

XavierBerger commented 4 months ago

@dloriot

XavierBerger commented 4 months ago

@tikismoke the content-length is present... Can you paste your configuration and logs? It should help me to understant what could happen.

dloriot commented 4 months ago

it works for a few seconds then it stops...

image image
XavierBerger commented 4 months ago

@dloriot What is your version of ESPHome? It looks like the bug identified into esphome 2024.6.1. Ref. https://github.com/XavierBerger/Solar-Router-for-ESPHome/issues/14 and https://github.com/esphome/issues/issues/5951

dloriot commented 4 months ago

Using esp-idf framework, it is much more stable !

image
XavierBerger commented 4 months ago

@dloriot The problem is that ac_dimmer require arduino:

output.ac_dimmer: [source solar_router/regulator_triac.yaml:21]

  This feature is only available with frameworks ['arduino'].
  platform: ac_dimmer
  id: ac_dimmer_output
  gate_pin: GPIO22
  zero_cross_pin: 
    number: GPIO23
    mode: 
      input: True
    inverted: True
dloriot commented 4 months ago

seems supported ? https://github.com/esphome/esphome/pull/2303

XavierBerger commented 4 months ago

@dloriot

seems supported ? esphome/esphome#2303

ESP-IDF is supported by ESPHome, but not for every component as for example ac-dimmer which required Arduino framework. For this component, the code for ESP-IDF is simply not present in source code.

XavierBerger commented 4 months ago

@dloriot @tikismoke Following the previous conversation of this ticket, I'm not able to know what is working and what is not. Can you write a summary here about the result of your tests and, from your point of view, what remain to be performed?

tikismoke commented 4 months ago

For my part I'm using ha sensor directly

I'll made a PR when I'm sure all is ok.

Technically it's working ans simple 👍

dloriot commented 4 months ago

As for me, only the ESP-IDF is usable with the SSR relay. I made a proof of concept and it works well, i can heat the water for my tea :)

tikismoke commented 4 months ago

@dloriot @tikismoke Following the previous conversation of this ticket, I'm not able to know what is working and what is not. Can you write a summary here about the result of your tests and, from your point of view, what remain to be performed?

This is how i ended to adapt to my case:

substitutions:
  device_name: "pool-solar-router"
  friendly_name: "pool-solar-router"
  name: ${device_name}
  static_ip: 10.0.5.4

  # Regulator --------------------------------------------------------------------
  regulator: "ac_dimmer"
  # Regulator configuration 
  # Define GPIO pin connected to AC Dimmer for gate and zero crossing detection.
  regulator_gate_pin: GPIO22
  regulator_zero_crossing_pin: GPIO23
  # LEDs -------------------------------------------------------------------------
  # Green LED is reflecting regulation status
  # Yellow LED is reflecting power meter status
  green_led_pin: GPIO19
  yellow_led_pin: GPIO18

wifi:
  ssid: "WiFi-garden"
  password: !secret wifi_password
  #power_save_mode: HIGH
  use_address: 10.0.5.4
  manual_ip:
    static_ip: ${static_ip}
    gateway: 10.0.5.1
    subnet: 255.255.255.0
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "solar router Fallback"
    password: !secret wifi_password

# Enable logging
logger:

debug:
  update_interval: 30s

# Enable Home Assistant API
# Reboot if HA is not connected for 30 minutes
api:
  reboot_timeout: 30min

# Enable Web server.
web_server:
  port: 80

mdns:
  disabled: false

ota:
  platform: esphome

esphome:
  name: ${device_name}
  comment: ${friendly_name}
  #name_add_mac_suffix: true

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

<<: !include _base_sensor.yaml

#  power_meter:
#    url: https://github.com/XavierBerger/Solar-Router-for-ESPHome/
#    file: solar_router/power_meter_shelly_em.yaml
#    ref: feature/shelly_em
#    refresh: 5s
  regulator:
    url: https://github.com/XavierBerger/Solar-Router-for-ESPHome/
    file: solar_router/regulator_triac.yaml
    #ref: feature/shelly_em
    refresh: 5s
  solar_router:
    url: https://github.com/XavierBerger/Solar-Router-for-ESPHome/
    file: solar_router/solar_router_engine.yaml
    #ref: feature/shelly_em
    refresh: 5s

one_wire:
  - platform: gpio
    pin: GPIO2

# ----------------------------------------------------------------------------------------------------
# Sensor updated every second to give feedback in Home Assistant
# ----------------------------------------------------------------------------------------------------
sensor:

  - platform: dallas_temp
    address: 0x7a00000086de1e28
    name: "${friendly_name} radiator_triac"
    filters:
      - filter_out: 85.0

# ----------------------------------------------------------------------------------------------------
# Sensor updated every second to give feedback in Home Assistant
# ----------------------------------------------------------------------------------------------------
  # Sensor showing the actual power consumption
  - platform: homeassistant
    id: real_power
    entity_id: sensor.main_power #this is the Homeassistant sensor (value comes from the shelly)
    internal: False
    name: "Real Power"
    device_class: "power"
    unit_of_measurement: "W"
    on_value: 
      then:
          - if:
              condition:
                switch.is_on: activate
              then:
                - script.execute: energy_regulation
                - light.turn_off: yellow_led
                - if:
                    condition:
                      lambda: return id(real_power).state == 0.0;
                    then:
                      # When 0.0 is returns, it means that an error occurs
                      - light.turn_on:
                          id: yellow_led
                          effect: fast
                    else:
                      - light.turn_on: yellow_led
    #update_interval: 1s

Now i just need to add temp security if triac temp is >~70°C as it sould not reach such temp with the radiator i put on it :)

dloriot commented 4 months ago

By applying your settings, no more problems with the Arduino framework; I think I chose the wrong type of board. Thank you

XavierBerger commented 4 months ago

By applying your settings, no more problems with the Arduino framework; I think I chose the wrong type of board. Thank you

@dloriot so, is it working with a direct connection to shelly? Did you use the shelly power meter I proposed or did you use a cutom one? If you use the one I provide, do you think I can merge the code of this power meter in main branch?

ahivert commented 1 month ago

@XavierBerger I tested it on a shelly 3EM. I can confirm it works with your branch feature/shelly_em.

I've done a small change to be able to set authentication header available here => https://github.com/ahivert/Solar-Router-for-ESPHome/commit/461a128685de5f0582f871c97d9f8ec9999134ef

There is some stability issue.

I only test the router with basic config, nothing else. Did I miss something ?

tikismoke commented 1 month ago

Pretty sure a div_by_zero make the crash and restart. Maybe when request failed or something similar.

XavierBerger commented 1 month ago

I had recently the same issue of stability and I manage to troubleshoot and find a cause and a solution.

This is not a _div_byzero issue, but a problem of _watchdog in http_request which trigger when the website takes time to answer.

To fix the issue, it is possible to define a value for the watch dog greater than the timeout (which is set to 10s) of http_request.

The solution:

Add the following code into your main yaml file:

# Define watchdog time (it should be greater than timeout)
http_request:
  watchdog_timeout: 12s

@ahivert Could you tell me if it also fixes your issue?