Hi, Im getting an UNKNOWN output from my lambda function
- platform: template
name: "solar bed rotation"
id: solar_bed_rotation
update_interval: 1s
filters:
- lambda: |-
return (atan2(id(mpu6050_accel_x).state , id(mpu6050_accel_y).state)*180.0) / 3.14159265359;
- heartbeat: 1s
In Home Assistant front end I just get State “UnKnown”
Full code as follows:
esphome:
name: solar_tracker
platform: ESP32
board: esp-wrover-kit
wifi:
ssid: "####"
password: "####"
# Optional manual IP
manual_ip:
static_ip: 192.168.1.156
gateway: 192.168.1.2
subnet: 255.255.255.0
# 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
sun:
latitude: ####°
longitude: ####°
time:
- platform: homeassistant
button:
- platform: restart
name: "solar tracker Restart"
sensor:
- platform: sun
name: Sun Elevation Max
id: sun_elevation_max
type: elevation
filters:
- max:
window_size: 1440
send_every: 1
send_first_at: 1
- platform: sun
name: Sun Elevation
id: sun_elevation
type: elevation
- platform: sun
name: Sun Azimuth
id: sun_azimuth
type: azimuth
# - platform: template
# name: "Bed Azimuth"
# type: float
# lambda: return (atan(sensor.mpu6050_accel_y / sqrt(pow(sensor.mpu6050_accel_x, 2) + pow(sensor.mpu6050_accel_z, 2))) * 180 / PI);
# lambda: return (atan(mpu6050_accel_y / sqrt(pow(mpu6050_accel_x * 2.0) + pow(mpu6050_accel_z * 2.0))) * 180.0 / PI);
# lambda: return (atan(mpu6050_accel_y / sqrt(pow(mpu6050_accel_x * 2.0) + powmpu6050_accel_z * 2.0))) * 180.0 / PI);
- platform: mpu6050
address: 0x68
update_interval: 1s
accel_x:
name: "MPU6050 Accel X"
id: mpu6050_accel_x
unit_of_measurement: "°"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 1
#- calibrate_linear:
# Map 0.0 (from sensor) to 0.0 (true value)
#temp change to max min until full.calibration full east raw -7.44 with direction 72 . west raw +8.86 with direction 250
#- -7.44 -> 80
#- 8.86 -> 250
#########################################
# - lambda: return {(atan(sensor.mpu6050_accel_y / sqrt(pow(sensor.mpu6050_accel_x, 2) + pow(sensor.mpu6050_accel_z, 2))) * 180 / PI)};
# accAngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI)
accel_y:
name: "MPU6050 Accel Y"
id: mpu6050_accel_y
unit_of_measurement: "°"
filters:
- sliding_window_moving_average:
window_size: 3
send_every: 1
##sensor was reading from 0 to 5
# - multiply: 2
- calibrate_linear:
# Map 0.0 (from sensor) to 0.0 (true value) max down angle measued at 29 with raw sensor reading 8.9
- 0.0 -> 90.0
- 8.9 -> 29
accel_z:
name: "MPU6050 Accel z"
id: mpu6050_accel_z
unit_of_measurement: "°"
filters:
- sliding_window_moving_average:
window_size: 10
send_every: 1
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
- platform: template
name: "solar bed rotation"
id: solar_bed_rotation
update_interval: 5s
filters:
# return (id(mpu6050_accel_x).state +100.0); ###test
- lambda: |-
return (atan2(id(mpu6050_accel_x).state , id(mpu6050_accel_y).state)*180.0) / 3.14159265359;
cover:
- platform: time_based
name: "East West Solar"
#1.66min is 1 min and 40 sec
open_action:
- switch.turn_on: relayEast
open_duration: 1.9min
# open is move west
close_action:
- switch.turn_on: relayWest
close_duration: 1.9min
stop_action:
- switch.turn_off: relayWest
- switch.turn_off: relayEast
#relay bank Manual Switching
switch:
- platform: gpio
pin: 23
id: relayUp
name: "Solar Up"
inverted: yes
on_turn_on:
- delay: 240s
- switch.turn_off: relayUp
- platform: gpio
pin: 19
id: relayDown
name: "Solar Down"
inverted: yes
on_turn_on:
- delay: 240s
- switch.turn_off: relayDown
- platform: gpio
pin: 5
id: relayEast
name: "Solar East"
inverted: yes
on_turn_on:
- delay: 240s
- switch.turn_off: relayEast
- platform: gpio
pin: 17
id: relayWest
name: "Solar West"
inverted: yes
on_turn_on:
- delay: 240s
- switch.turn_off: relayWest