Help Implementing Sun Solar Tracker

Hi, I have a DIY large solar sun tracker that i need some help with.
All the hardware is up and running. I am moving the software from “Blynk Local Server” to Home assistant and Esphome.
Hardware: Two linear actuators controlled by 4 relays.
Controller: ESP32 and sensor MPU6050

Current manual control code.

esphome:
  name: solar_tracker
  platform: ESP32
  board: esp-wrover-kit


wifi:
  ssid: "##########"
  password: "###########"


  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solar Tracker Fallback Hotspot"
    password: "lZEGI44isCwX"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

i2c:
  sda: 21
  scl: 22
  scan: True

sensor:
  - platform: mpu6050
    address: 0x68
    update_interval: 100ms
    
    accel_x:
      name: "MPU6050 Accel X"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
       - calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - -10.0 -> 270.0
          - 10.0 -> 90.0           

    accel_y:
      name: "MPU6050 Accel Y"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
       - calibrate_linear:
          # Map 0.0 (from sensor) to 0.0 (true value)
          - 0.0 -> 90.0
          - -10.0 -> 0.0   
          
          
    accel_z:
      name: "MPU6050 Accel z"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
    gyro_x:
      name: "MPU6050 Gyro X"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
    gyro_y:
      name: "MPU6050 Gyro Y"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
    gyro_z:
      name: "MPU6050 Gyro z"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
    temperature:
      name: "MPU6050 Temperature"
      filters:
       - sliding_window_moving_average:
          window_size: 10
          send_every: 5 
          

#relay bank Manual Switching
switch:
  - platform: gpio
    pin: 23
    id: relayUp
    name: "Solar Up"
    inverted: yes
  

  - platform: gpio
    pin: 19
    id: relayDown
    name: "Solar Down"
    inverted: yes
    
    
  - platform: gpio
    pin: 5
    id: relayEast
    name: "Solar East"
    inverted: yes
   

  - platform: gpio
    pin: 17
    id: relayWest
    name: "Solar West"
    inverted: yes

I need help!
I need to compare sun azimuth and sun elevation (from Home Assistant) with my MPU6050 data X and Y

I think the following code will expose the azimuth and elevation, but im new to coding in YAML and cant figure out how it implement this.




  - platform: template
    sensors:
      sun_elevation:
        friendly_name: "Sun Elevation"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

  - platform: template
    sensors:
      sun_azimuth:
        friendly_name: "Sun Azimuth"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'azimuth') }}"

The automation needs to be on the ESP32 as we do get power cuts and the WIFI goes off.
I really would appreciate any help as I love the esphome and home assistant platforms.
Many thanks

HI,

I found your post because I am also thinking about implementing an Heliostat with esphome, but now it is just an idea.

Some input for you:

mmm…intersting :+1:
did you success it?
Hev any info,links?

Thanks for sharing…

not found a fix yet

I’m also making an ESPHome based solar tracker \ heliostat. My prototype hardware is much, much smaller than yours though (2x SG90 servo’s). This is what I’ve got so far:

time:
  - platform: homeassistant  
    id: homeassistant_time
    on_time: # Every minute
      - seconds: 0
        minutes: /1
        then: # Output sun azimuth and elevation
          - logger.log: 
              format: "Sun position: Azimuth %.1f, Elevation %.1f"
              args: ['id(sunpos).azimuth()', 'id(sunpos).elevation()']

sun:
    id: sunpos
    latitude: ***
    longitude: ***

And here’s how it looks in the log:
[08:49:00][D][main:111]: Sun position: Azimuth 98.4, Elevation 28.9
[08:50:00][D][main:111]: Sun position: Azimuth 98.6, Elevation 29.1
[08:51:00][D][main:111]: Sun position: Azimuth 98.8, Elevation 29.2