Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Pulse counter #165

Closed
voicevon opened this issue Oct 1, 2018 · 5 comments
Closed

Pulse counter #165

voicevon opened this issue Oct 1, 2018 · 5 comments

Comments

@voicevon
Copy link
Contributor

voicevon commented Oct 1, 2018

I am using pulse sensor, the value unit is "pulse/min". I hope there are two measure unit. First is "Total pulses count" ,it is for "How many pulses the sensor has caught". Second is "Rate/Ratio", Just same requirement as currently what the pulse sensor is doing. Further more, I want the third value like "Acceleration", to show how much it's going faster/slower.
I tried to modify the code, But it should be one GPIO connected, Like the behavior of DHT-11, two sensors with one wire. This will be a challenge for me. @OttoWinter , Do you think my idea is doable?
I know we should consider about the counter overflow, power-down, might be other troubles I don't know right now.

BTW: one document error at https://esphomelib.com/api/sensor/pulse_counter.html at "Example usage"

strom_warme.mqtt->set_unit_of_measurement("kW");
strom_warme.mqtt->clear_filters();
strom_warme.mqtt->add_multiply_filter(0.06f); // convert from Wh pulse to kW

the mqtt is wrong, should be pcnt.

OttoWinter added a commit to esphome/esphome-docs that referenced this issue Oct 4, 2018
@OttoWinter
Copy link
Member

Yes, it is possible:

sensor:
  - platform: pulse_counter
    filters:
      - lambda: |-
          static float total_value = 0.0;
          // 0.25 because of update_interval 15s
          total_value += x * 0.25;
          return total_value;

The acceleration would be more or less the same, but I'll leave it as an exercise to the reader :P

@voicevon
Copy link
Contributor Author

voicevon commented Oct 6, 2018

Yes, Lambda can convert from "pulse/min" to "Total pulse count". Saying, I can only get one of them, I got this, I lost that. But my request is: The sensor provides two or three values that they represent different meaning at the same time.

@OttoWinter
Copy link
Member

Ok, now I understand, so you want to have all three units show up as sensor at the same time.

sensor:
- platform: pulse_counter
  name: "Pulses per minute"
  id: pulse
  filters:
- platform: template
  name: "Total pulses"
  lambda: |-
    static float total_value = 0.0;
    // 0.25 because of update_interval 15s
    total_value += id(pulse).value * 0.25;
    return total_value;
- platform: template
  name: "Pulse Acceleration"
  lambda: |-
    // ...

In pure C++ it would be similar, just with the use of the TemplateSensor class.

@voicevon
Copy link
Contributor Author

voicevon commented Oct 14, 2018

I am not able to use Yaml right now. I read a lot source code of TemplateSensor, but still can't archive . The main reason is I don't know how to write an instance of
void set_template(std::function<optional<float>()> &&f)
Could you give a two/three lines example? I guess the code should be like below. Thanks.

   auto water= App.make_pulse_counter_sensor("flow_per_min", GPIOInputPin( 18,INPUT_PULLUP));  
        water.pcnt->clear_filters();  
 auto total = App.make_template_sensor("total_flow");
 total .template_->set_template(&& water.pcnt->get_value());  //My target line, right?
total .template_->add_filter(.....)

@voicevon
Copy link
Contributor Author

I got it .

  Application::MakeTemplateSensor target = App.make_template_sensor("target",2000);
      target.template_->set_template([=]() -> optional<float> {
      static float result = 0.0;
      result = source->value + 123;   //result = anyfunction( source->value);
      return result; 
  });

@esphome esphome locked and limited conversation to collaborators Jun 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants