ESPhome activate GPIO for X seconds

Hi

I am looking for a way to control GPIO from the code. I want to set GPIO high for X seconds and then set it back to low again. Then sleep for e.g. 60 sec. Then repeat all over again.

How do I integrate it within this code?

It seems you are looking for a DSMR/P1 port reader using ESPHome.I don’t know how to fix you code, but I found a much simpler approach using this fragment from gist (https://gist.github.com/oxan/4a1a36e12ebed13d31d7ed136b104959)

substitutions:
  devicename: p1
  upper_devicename: P1
esphome:
  name: p1
  platform: ESP8266
  board: esp01_1m
  includes:
  - includes/streamserver/stream_server.h
  - includes/streamserver/stream_server.cpp
switch:
- platform: restart
  name: P1 restart
  icon: mdi:restart
text_sensor:
- platform: wifi_info
  ip_address:
    name: P1 IP address
    icon: mdi:ip-network-outline
  ssid:
    name: P1 SSID
    icon: mdi:access-point-network
- platform: template
  name: P1 MAC address
  lambda: !lambda |-
    return {WiFi.macAddress().c_str()};
  icon: mdi:expansion-card-variant
  update_interval: 1d
- platform: version
  name: P1 version
  icon: mdi:new-box
  hide_timestamp: false
sensor:
- platform: wifi_signal
  name: P1 WiFi signal strength
  update_interval: 60s
  icon: mdi:wifi-strength-outline
  force_update: false
  unit_of_measurement: dB
  accuracy_decimals: 0
- platform: uptime
  name: P1 uptime
  unit_of_measurement: s
  icon: mdi:timer-outline
  force_update: false
  accuracy_decimals: 0
  update_interval: 60s
uart:
- id: uart_bus
  tx_pin: 1
  rx_pin: 3
  baud_rate: 9600
  data_bits: 7
  parity: EVEN
  rx_buffer_size: 256
  stop_bits: 1
custom_component:
- lambda: !lambda |-
    auto stream_server = new StreamServerComponent(id(uart_bus));
    return {stream_server};
wifi:
  ap:
    ssid: P1 Fallback Hotspot
    password: !secret 'pwd_p1_ap'
    ap_timeout: 1min
  domain: .local
  reboot_timeout: 15min
  power_save_mode: NONE
  fast_connect: false
  output_power: 20.0
  networks:
  - ssid: !secret 'ssid_wifi'
    password: !secret 'pwd_wifi'
    priority: 0.0
  use_address: p1.local
captive_portal: {}
logger:
  baud_rate: 0
  tx_buffer_size: 512
  hardware_uart: UART0
  level: DEBUG
  logs: {}
  esp8266_store_log_strings_in_flash: true
api:
  password: !secret 'pwd_esp_p1_api'
  port: 6053
  reboot_timeout: 15min
ota:
  password: !secret 'pwd_esp_p1_ota'
  safe_mode: true
  port: 8266
  reboot_timeout: 5min
  num_attempts: 10

With some minor electronics this is powered from an USB port to continuously supply 5V on my DSMR and that means that every 10 seconds a telegram is received by the dsmr platform.

Which for my DSMR is configured like this:

- platform: dsmr
  host: !secret dsmr_p1_host
  port: !secret dsmr_p1_port

If you have an additional GPIO pin to spare you can than set up some switch like the example in the esphome docs (https://esphome.io/components/switch/gpio.html) and connect it to the request line of your P1 port:

# Example configuration entry
switch:
  - platform: gpio
    pin: 25
    id: relay
  - platform: template
    name: "P1 request"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 10s
    - switch.turn_off: relay

You are absolutely right on what this all is about :slight_smile: DSMR, P1 and meter.

The problem is that my new Meter is Landis E360 which has a bug in it’s firmware. To read the data from it one needs to supply 5V on RTS line. Normally there would be a constant connection between 5V and RTS and the data would be flowing every 10 sec. But due to the bug in the FW the P1 stops sending the telegrams after several hours to days. And you need to do a full restart of the meter to get it going again. Obviously this is not feasible.

A workaround is to instead of constantly connecting RTS to 5V only occasionally send 5V or 3.3V to RTS. And that is what I am trying to achiev .

Thanks for sharing your code. It quite big and not so easy to grasp what it does.

There is yaml fir for the esphome code I am using . It is in the same directoru as the p1reder.h if you want to take a sneap peak.

In your code you have this

switch:
- platform: restart
 name: P1 restart
 icon: mdi:restart

What does it do? Does it restart your esp device?

I have plenty of GPIOS available to play with. Example you posted with these

switch:
  - platform: gpio
    pin: 25
    id: relay

Will that create a switch in the HASS GUI that one can push and it will initiate pin 25 going up , then 10s delay then down. But this will only be once? I need a way to happen this at certain intervals all the time. How to do that?