Initial value for global float variable not working
- Dominant language
- No language data
- Stars
- 313
- Forks
- 40
- PR merge metrics
- No merged PRs in 30d
Description
### The problem
I have a global float variable i want to initialize with 0 and restore it:
```
globals:
- id: g_water_daily
type: float
restore_value: true
initial_value: "0.0"
```
On boot, i set the restored value to a template sensor:
```
esphome:
...
on_boot:
then:
- sensor.template.publish: # restore today
id: today
state: !lambda 'return id(g_water_daily);'
sensor:
- platform: template
name: "Heute"
id: today
icon: "mdi:water-plus"
unit_of_measurement: "L"
device_class: "water"
```
This doesn't work. The template sensor is always unknown after startup:

I also have a global int-variable that restores pulse counts, it works flawlessly. That's why i think the problem is the float type.
Also worth mentioning is, that the reset to 0.0 via `globals.set` works too.
### Which version of ESPHome has the issue?
2024.6.6
### What type of installation are you using?
Home Assistant Add-on
### Which version of Home Assistant has the issue?
2024.6.1
### What platform are you using?
ESP32
### Board
Generic Esp32
### Component causing the issue
globals
### Example YAML snippet
```yaml
esphome:
name: garten-wasserzahler
friendly_name: Garten Wasserzähler
on_boot:
then:
- pulse_meter.set_total_pulses: # restore pulses
id: pulse_meter_id
value: !lambda 'return id(g_total_pulses);'
- sensor.template.publish: # restore today
id: today
state: !lambda 'return id(g_water_daily);'
esp32:
board: esp32dev
framework:
type: esp-idf
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "xxx"
ota:
- platform: esphome
password: "xxx"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Garten-Wasserzahler"
captive_portal:
web_server:
port: 80
preferences:
flash_write_interval: 3h
globals:
- id: g_total_pulses
type: int
restore_value: true
initial_value: "1125000"
- id: g_water_daily
type: float
restore_value: true
initial_value: "0.0"
sensor:
- platform: pulse_meter
id: pulse_meter_id
internal_filter: 100ms
pin:
number: 13 # D13
inverted: true
mode:
input: true
pullup: true # Anschluss zwischen Pin und GND
name: "Durchfluss"
icon: "mdi:water"
unit_of_measurement: "L/h"
state_class: "measurement"
device_class: "volume_flow_rate"
timeout: 20s
accuracy_decimals: 0
filters:
- multiply: 30
total:
name: "Gesamt"
id: gesamt_id
icon: "mdi:water-circle"
unit_of_measurement: "L"
state_class: "total_increasing"
device_class: "water"
accuracy_decimals: 1
filters:
- multiply: 0.5
on_raw_value: # ignore filters
then:
- globals.set:
id: g_total_pulses
value: !lambda 'return id(gesamt_id).raw_state;'
- globals.set: # increase global today by 0.5 L per pulse
id: g_water_daily
value: !lambda 'return id(today).state + 0.5;'
- sensor.template.publish: # increase today by 0.5 L per pulse
id: today
state: !lambda 'return id(today).state + 0.5;'
- platform: template
name: "Heute"
id: today
icon: "mdi:water-plus"
unit_of_measurement: "L"
device_class: "water"
time:
- platform: homeassistant
id: homeassistant_time
on_time:
# Midnight
- seconds: 0
minutes: 0
hours: 0
then:
- sensor.template.publish: # reset today
id: today
state: 0
- globals.set: # reset global today
id: g_water_daily
value: "0.0"
```
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.