Help checking my logic

Hi all
I’ve been fiddling with my code for ages it seems and I need some fresh eyes.
My basic idea is to use the status light on my oil burner to calculate the run time and thus the oil consumption. To detect the status light I have a photoresistor connected to my Wemos analogue input and that seems to work fine. My issue is with the lambda, it either gives me to high or to low result.
0.0396 is the rated oil consumption in liters / minute.
Can anyone please offer some insights? Do I have the whole “if” sequence wrong? Is it an issue for me to run the template every minute instead of every second?

globals:
  - id: totalOil
    type: float
    restore_value: no

sensor:
  - platform: mqtt_subscribe
    name: "MQTTsensor"
    id: mysensor
    topic: homeassistant/esp/heating/sensor/oljeforbrukning/state
    on_value:
      then:
        - logger.log: "MQTTsensor trigger"
    accuracy_decimals: 2

  - platform: adc
    pin: A0
    name: "BurnerVoltage"
    id: Burner
    update_interval: 1s


  - platform: template
    name: "Oilconsumption"
    id: oilconsumption
    icon: mdi:tanker-truck
    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);
        id(totalOil) +=  (num_executions*0.0396);
      }
      if (id(Burner).state < 0.7) {
       ESP_LOGI("main", "0 Executions");
       static float num_executions = 0;
      }
      return id(totalOil);
    update_interval: 60s
    accuracy_decimals: 2
    unit_of_measurement: 'l'

Thanks
Fredrik

Hi.

If run your template sensor every second you could probably get a better result.

I think though that I need to see the full yaml to understand.

The mySensor how does this relates to totalOil.

This looks littlebit odd.

      if (id(Burner).state < 0.7) {
       ESP_LOGI("main", "0 Executions");
       static float num_executions = 0;
      }

You are redefining a static variable in the scope of the if.
I’m not sure what going to happen. So it can be that the num_execution on the top never got set to zero. But do redefine a static in a scope of an if is over my c++ knowledge.
Suggest like this. This will clear the num_execution.

      if (id(Burner).state < 0.7) {
       ESP_LOGI("main", "0 Executions");
       num_executions = 0;
      }

/Mattias

Hi
I’ve edited my original post and added the global “totalOil” and MQTT fetch that was missing in my first post. I think my issue is my lack of understanding of the “update interval” and how this affects my lambda calculations. Doesn’t the Oilconsumption update interval of 60 seconds mean the template and it’s lambda is “run” every 60 seconds? I that case I don’t need the num_executions value at all since my calculation would be "totalOil (the last calculated amount of oil usage) + 0.0396 (the rated oil consumption per minute).

Regards
Fredrik

Hi.

The lambda is run ONCE every 60 seconds.
So you should probably read a littlebit more often and remove the num_execution.

So example read every second and I have remove the num_executions.

  - platform: template
    name: "Oilconsumption"
    id: oilconsumption
    icon: mdi:tanker-truck
    lambda: |-
     if (id(Burner).state>0.9) {
        ESP_LOGI("main", "TotalOil: %f", id(totalOil));
        id(totalOil) +=  (0.0396/60);
      }
      return id(totalOil);
    update_interval: 1s
    accuracy_decimals: 2
    unit_of_measurement: 'l'

This will check it every second if the led is on. Then increase the total with (0.0396/60) l

This does not take into account the mysensor value. So that you maybe need to explain some more how you where thinking and how it setup in Home Assistand.

Is this what you where thinking.

/Mattias

I think I get it, with the update interval at least.
About the mysensor. It’s somewhat of a scope-creep for this topic. It’s my take on making a value persistent across reboots or installs on the ESP, although a bit elaborate. Totaloil is sent to MQTT where Home Assistant picks it up and reposts it back to MQTT when a reboot of the ESP sets the value to “nan”. It’s on my todo list to make ESP native.

/Fredrik

I suspected that it was something like that.

It is definitely possible.

If you increase to one second you should probably limit the report interval. That can be done by a sensor filter. Forgot that in the last post.

/Mattias