TheHoff
(Fredrik Hofgren)
August 7, 2021, 12:11pm
1
Greetings
My core issue is with a counter being reset to 0 when my Wemos D1 mini reboots, I need it to retain the last value. This seems to be an issue with the native API and I’ve turned to MQTT and it’s retain feature but I cannot get that to work.
My pseudo idea is to have the D1 read a photoresistor voltage and if it’s above 0.9V trigger a counter that will count the amount of time, calculate the fuel usage and publish that as totalOil over MQTT. This publish needs to be retained so I can later figure out how to retrieve the latest value from MQTT on reboot.
Any suggestions on why my retain isn’t working would be appreciated.
Thanks
/Fredrik
mqtt:
broker: x.x.x.x
username: !secret mqttlogin
password: !secret mqttpass
topic_prefix: homeassistant/esp
discovery_prefix: homeassistant
birth_message:
will_message:
globals:
- id: totalOil
type: float
restore_value: no
sensor:
- platform: mqtt_subscribe
#name: "Data from topic"
id: mysensor
topic: homeassistant/esp/sensor/oljeforbrukning/state
- platform: adc
pin: A0
name: "BurnerVoltage"
id: Burner
update_interval: 1s
retain: true
expire_after: 10 minutes
- platform: template
name: "Oljeförbrukning"
lambda: |-
static float num_executions;
id(totalOil) = id(mysensor).state;
if (id(Burner).state>0.9) {
num_executions +=1;
}
id(totalOil) += (num_executions/3600)*0.84;
return id(totalOil);
unit_of_measurement: 'l'
update_interval: 1s
retain: true
expire_after: 10 minutes
nickrout
(Nick Rout)
August 7, 2021, 9:18pm
2
Does it work to seed the value on_boot
with an mqtt sensor.
TheHoff
(Fredrik Hofgren)
August 8, 2021, 5:35am
3
Hi nickrout
My though exactly but it doesn’t seem to work. Even if I manually set the state of homeassistant/esp/sensor/oljeforbrukning/state it still goes to “nan” efter the Wemos finishes it’s boot.
esphome:
name: heating
platform: ESP8266
board: d1_mini
on_boot:
- priority: 250.0
then:
- script.execute: power_on_boot
script:
- id: power_on_boot
then:
- lambda: |-
static float oil;
oil = id(mysensor).state;
ESP_LOGI("main", "Boot value of oil: %f", oil);
id(totalOil) = id(mysensor).state;
ESP_LOGI("main", "Boot value of totalOil: %f", id(totalOil));
nickrout
(Nick Rout)
August 8, 2021, 5:39am
4
At level 250 the mqtt won’t be set up. ESPHome Core Configuration — ESPHome
nickrout
(Nick Rout)
August 8, 2021, 6:00am
6
Is this as simple as restore from flash? ESPHome Core Configuration — ESPHome
TheHoff
(Fredrik Hofgren)
August 8, 2021, 6:01am
7
Maybe, but isn’t writing fast changes to flash illadviced?
TheHoff
(Fredrik Hofgren)
August 8, 2021, 6:11am
8
I’ve been fiddling with my code back and forth and the issue as it is right now is “nan” being sent right after the D1 regains WIFI after a boot or config change.
esphome:
name: heating
platform: ESP8266
board: d1_mini
on_boot:
- priority: 200.0
then:
- script.execute: power_on_boot
# Enable logging
logger:
level: DEBUG
ota:
mqtt:
broker: 172.16.80.10
username: !secret mqttlogin
password: !secret mqttpass
topic_prefix: homeassistant/esp
discovery_prefix: homeassistant
birth_message:
will_message:
api:
globals:
- id: totalOil
type: float
restore_value: no
script:
- id: power_on_boot
then:
- lambda: |-
static float oil;
oil = id(mysensor).state;
ESP_LOGI("main", "Boot value of oil: %f", oil);
id(totalOil) = id(mysensor).state;
ESP_LOGI("main", "Boot value of totalOil: %f", id(totalOil));
sensor:
- platform: mqtt_subscribe
name: "MQTTsensor"
id: mysensor
topic: homeassistant/esp/sensor/oljeforbrukning/state
- platform: adc
pin: A0
name: "BurnerVoltage"
id: Burner
update_interval: 10s
retain: true
expire_after: 10 minutes
- platform: template
name: "Oljeforbrukning"
lambda: |-
static float num_executions = 0;
id(totalOil) = id(mysensor).state;
if (id(Burner).state>0.9) {
num_executions +=1;
ESP_LOGI("main", "Executions: %f", num_executions);
}
if (num_executions < 0.7) {
id(totalOil) += (num_executions/3600)*0.84;
ESP_LOGI("main", "0 Executions");
}
ESP_LOGI("main", "Value of totalOil before publish: %f", id(totalOil));
return id(totalOil);
unit_of_measurement: 'l'
update_interval: 10s
retain: true
expire_after: 10 minutes