[solved] ESPHOME : add Sensor value to url (float to string)

I would like to push my pulse_meter value by url to googledocs.
I can push a static value to gdocs after following this tutorial.

the result is a url in the shape of : "https://script.google.com/macros/s/XXXLongSecretXXX/exec?A=1&termperature=42&humidity=123

with 42 and 123 as static values it works. How can I get in esphome the value of my pulse_meter into the URL ?

My attempt (not working yet):

  - platform: total_daily_energy
    name: "Tagesverbrauch_e1"
    power_id: energy1
    filters:
      - multiply: 0.010416667  # (1/96 pulses per kWh)
 - platform: pulse_meter
    pin: 12
    name: "Pulse Meter"    
    internal_filter: 100ms
    accuracy_decimals: 3
    filters:
      - multiply: 0.625
    on_value: 
      #- http_request.post:
      #    url: !lambda |-
      #      return ((std::string) "https://esphome.io?state=" + id(my_sensor).state).c_str();  # not working due to sensor value not float
      - http_request.get:
          url: !lambda |-
            return ((std::string) "https://script.google.com/macros/s/xxxsecretxxxx/exec?A=1&temperature=42&humidity=%f" + id(energy1).state.c_str());
          # {{ states.sensor.whatever_name.state | regex_replace(find='string', replace='') | int }}  << shall I use this? 
          # was working with  url: https://script.google.com/macros/s/xxxsecretxxxx/exec?A=1&temperature=42&humidity=4711
          headers:
            Content-Type: application/json
          verify_ssl: false
          on_response:
            then:
              - logger.log:
                  format: 'Response status: %d'
                  args:
                    - status_code

Thank you for any help or advise!

I think you are looking for sprintf to build your URL.
Something like this…

char buf[200];
sprintf(buf, "https://script.google.com/macros/s/XXXLongSecretXXX/exec?A=1&termperature=%f&humidity=%f", id(mysensor1).state, id(mysensor2).state));

Try to use logging statements in your lambdas for debugging:

ESP_LOGD("DEBUG", "URL built: %s", buf.c_str());

3 Likes

Thank you for your help. It works now. Just the inkrement does not match with the new total sum, but I will start a new thread for that

Here is my code for future reference:
I can pass 2 values to google docs spreadsheet

sensor:
  - platform: pulse_counter
    pin: GPIO12
    name: "Energieverbrauch"
    unit_of_measurement: 'kW'
    update_interval: 60s
    count_mode:
     rising_edge: DISABLE
     falling_edge: INCREMENT
    internal_filter: 70ms
    filters:
      - multiply: 0.625
    accuracy_decimals: 2
    icon: 'mdi:flash'
    id: energy
    on_value: 
      - http_request.get:
          url: !lambda |-
             char buf[256];
             sprintf(buf,  "https://script.google.com/macros/s/xxxxxxscretkeyxxxx/exec?increment=%1.3f&summe=%1.3f", id(energy).raw_state, id(fooo).state);
             return ((std::string) buf).c_str();
          headers:
            Content-Type: application/json
          verify_ssl: false
          on_response:
            then:
              - logger.log:
                  format: 'Response status: %d'
                  args:
                    - status_code
                    
  - platform: total_daily_energy
    name: "Tagesverbrauch_e1"
    power_id: energy
    filters:
      - multiply: 0.010416667  # (1/96 pulses per kWh):
  - platform: integration
    name: "Total Daily Energy"
    sensor: energy
    time_unit: min
    id: fooo

time:
  - platform: sntp
    id: my_time
3 Likes

Just need to verify that you
return buf
and not return buf.c_str()

what worked for me is to add
headers: Content-Type: text