Help with ESP8266 automation

Hi, I’m new to home assistant.

I have a working ESP8266 board ESPHome and IR emitter + PIR.

I have the IR emitter sending a OFF code to my air conditioner every 2 hours, except between 10PM and 8AM.

What I can’t figure out, is how do I temporarily prevent the above automation from running for X hours IF there’s movement input from the PIR sensor (so the air conditioner is not turned of if there are people in the room)?

Any help would be greatly appreciated.

So you want the AC to turn off if there’s no movement in the room for 2 hours?
If so, and your automation is in ha use the motion sensor as a trigger but set a for: time of two hours.
Or if you are doing it all in esphome, use delayed_off set to 2 hours on the motion sensor and then fire your ac command.

You can do it two ways:

  1. automation code in HA
  2. automation code in the ESP itself

Which way do you want to go?

Hi, thank you for replying.

I was just reading about ESPHome internal automation, that would be much better so the esp8266 boards don’t rely on home assistant server being online for the automation to work.

No worries.

Automations in ESPhome are pretty easy, only slightly different from yaml in HA.

Here is an example of one of mine. It does a motion controlled light with restart such that any new motion retriggers it without the light first turning off.

esphome:
  name: pantry_d1_mini
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: !secret wifi_iot24g_ssid
  password: !secret wifi_iot24g_password
  manual_ip:
    static_ip: 192.168.0.66  
    gateway: 192.168.0.1
    subnet: 255.255.255.0

web_server:

api:

logger:

ota:
  password: !secret ota_password

binary_sensor:
  - platform: gpio
    pin: D1
    name: "Pantry Motion Sensor"
    device_class: motion
    on_press:
      then:
        - script.execute: light_control


output:
  - platform: gpio
    id: r1
    pin:
      number: D5
      inverted: true

light:
  - platform: binary
    name: "Pantry Light"
    id: l1
    output: r1
   

script:
  - id: light_control
    mode: restart
    then:
      - light.turn_on: l1
      - delay: 20s
      - light.turn_off: l1

You could do something very similar, but with the logic the other way around, so motion prevents the output rather than causing it.

I would also have to fiddle with the time parameter, I was reading about it now.

But how would I store the IR HEX command inside ESPhome?

I’m not thinking straight anymore, it’s pretty late here in Brazil lol

never mind, already found my answer about the IR.

Thank you very much mates, I will give it a try and let you know if it worked.

Hi sparkydave,

If I understood, to integrate the script with the time parameter (to evaluate if the active period of the motion sensor, which would be only at daylight), I would have to use time in lambda, which involves C++ right? What would be the correct syntax for me to only run the script if I’m between 10AM and 22PM, for example?

Thanks in advance.

Kinda… or you could make it more dynamic by using a Home Assistant time element to provide the time data. ESPhome has access to HA entities so you could use two input_datetime’s as the condition parameters and use those in the ESP automation. This would however require HA to be online. Another way to do this but without needing HA to be online is to publish those times to MQTT and have the ESP subscribe to the topic, then set the topic as ‘retained’.

Hi again, is this the correct syntax for transmitting the IR command within the script?

script:
  - id: keep_ac_on
    mode: restart
    then:
      - delay: 30m
      on_...:
  	    - remote_transmitter.transmit_lg:
      		data: 0x1234567
     	    nbits: 28

Thank you!

I’m getting an error with the on_… command, not sure if I should change the three dots to something else…

bad indentation of a mapping entry at line 58, column 7:
          on_...:
          ^

Yes, the three dots in the documentation is meant to represent an example. You need to replace the three dots with a trigger type

Well, I came up with

time:
  - platform: sntp
    id: sntp_time
    timezone: America/Sao_Paulo
    
remote_transmitter:
  pin: GPIO0
  carrier_duty_percent: 50%

remote_receiver:
  pin:
    number: GPIO2
    inverted: True
  dump:
    - rc_switch
  tolerance: 50%
  filter: 250us
  idle: 4ms
  buffer_size: 2kb
  
switch:
  - platform: template
    name: Lg Power Button
    turn_on_action:
      - remote_transmitter.transmit_lg:
          data: 0x20DF10EF
          nbits: 32
          
binary_sensor:
  - platform: gpio
    pin: GPIO1
    name: "PIR Menano"
    device_class: motion
    id: pir_menano
    on_press:
      then:
        - script.execute: keep_ac_on
interval: 
  - interval: 10min
    then:
      - script.execute: turn_off_ac
      
script:
  - id: turn_off_ac
    mode: single
    then:
      - if:
          condition:
            and:
              - lambda: |-
                    return id(sntp_time).now().hour > 8;
              - lambda: |-
                    return id(sntp_time).now().hour < 22;
              - lambda: |-
                  if (id(pir_menano).state) {
                    return false;
                  } else {
                    return true;
                  }
          then:
            - logger.log: "Turn Off AC"
            - remote_transmitter.transmit_lg:
                data: 0x20DF10EF
                nbits: 32
          
          else:
            - logger.log: "Keep AC on"

# Script executado sempre que houver movimento no PIR
  - id: keep_ac_on
    mode: restart
    then:
      - lambda: |-
             id(pir_menano).publish_state(true);
      - delay: 60min
      - lambda: |-
             id(pir_menano).publish_state(false);

Can’t test it right now since I fried my esp8266 yesterday by mistake, must wait till Tuesday now lol

Anyway, I think espeasy is more indicated for what I’m trying to do (I also need to learn the remote code directly into the firmware through a push button or something like that) and I don’t think esphome can do it.

Thank you very much for your help, it was precious.

Regards

Cassio