Humidifier automation with RAW RF

I am trying to automate my humidifier and I am really new at this and really not sure how to go about doing this. I have three thermostats added into home assistant that have a humidity reading I can use as my base. I am looking to average the three thermostat humidities and if the value is 35% or lower, I need it to trigger the raw captures I have in my other post (AirCare EP9R 800 humidifier pedestal). The caveat to all of this is the humidifier thinks it “has lost the remote” if it does not dial back every 60s with the same RAW RF. The other issue is it is needed to send multiple times on each of the 60 seconds. The current remote provided from the manufacturer repeats 5 times ever 20 seconds, but I have found I can send them as long as 2 min.

In essence I am looking for:

If (average(humidity on thermostats))<35% run on loop
send raw on 5 times
repeat every 60 seconds

if (average(humidity on thermostats))>35% run off loop
send raw on 5 times
repeat every 60 seconds

I have read a lot of the documentation, but I really don’t get where I am supposed to be adding these different things like:

My YAML from esphome

esphome:
  name: lr-esp360
  friendly_name: lr-esp360

esp32:
  board: esp32dev
  framework:
    type: arduino

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pswd

api:
  encryption:
    key: XXXXX
  services:
    - service: send_ir_raw
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            transmitter_id: IR_TX
            code: !lambda 'return command;'
    - service: send_rf_raw
      variables:
        protocol_number: int
        raw_data: string
      then:
        - remote_transmitter.transmit_rc_switch_raw:
            transmitter_id: RF_TX
            protocol: !lambda 'return  esphome::remote_base::RC_SWITCH_PROTOCOLS[protocol_number];'
            code: !lambda 'return raw_data;'

#i2c bus definition

i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

#Offsets have been added to the measurements taken. 
#These are necessary due to the heat generated by the PCB, the second layer of the board, and the case. 
#This way, sufficiently accurate values are obtained for automations

sensor:
  - platform: shtcx
    temperature:
      name: "ESP360 Temperature Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
        - offset: -3.7
    humidity:
      name: "ESP360 Humidity Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
        - offset: +19.1   
    address: 0x70
    update_interval: 300s
  - platform: bh1750
    name: "ESP360 BH1750 Illuminance Living Room"
    filters:
      - multiply: 5
    address: 0x23
    update_interval: 10s
  - platform: wifi_signal
    name: "ESP360 Living Room WiFi signal"
    update_interval: 60s

#Setting up IR and RF receivers

remote_receiver:
  - id: RF_RX
    pin: 
      number: GPIO5
      inverted: True
    dump: raw
    tolerance: 35%
    filter: 255us
    idle: 10ms
    buffer_size: 10kb
    on_rc_switch:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            protocol: !lambda 'return x.protocol;'
            code: !lambda 'return x.code;' 
  - id: IR_RX
    pin: 
      number: GPIO19
      inverted: True
    dump: all
    on_raw:
       - homeassistant.event:
          event: esphome.ir_raw_code_received
          data:
            code: !lambda '
            reverse(x.begin(), x.end());
            std::string str = "";
            for (auto it = x.begin(); it != x.end(); ++it)
            {
              str += to_string(*it);
              str += ",";
              if (it != x.end() - 1) str += " ";
            }
            if (str.size() > 0)  str.erase( str.end() - 2);
            return esphome::to_string("[" + str + "]");'   
    on_pronto:
       - homeassistant.event:
          event: esphome.ir_pronto_code_received
          data:
            code: !lambda 'return x.data;'
    on_lg:
       - homeassistant.event:
          event: esphome.ir_lg_code_received
          data:
            data: !lambda 'return x.data;'
            nbits: !lambda 'return x.nbits;'
    on_panasonic:
       - homeassistant.event:
           event: esphome.ir_panasonic_code_received
           data:
            address: !lambda 'return x.address;'
            command: !lambda 'return x.command;'

#Setting up IR and RF transmitters

remote_transmitter:
  - id: RF_TX
    pin: GPIO18
    carrier_duty_percent: 100%
  - id: IR_TX
    pin: GPIO13
    carrier_duty_percent: 50%

#Status LED

switch:
  - platform: gpio
    pin: 12
    name: "Status Led"
    restore_mode: ALWAYS_ON

  - platform: template
    name: "Humidifier"
    turn_on_action:
      - remote_transmitter.transmit_raw:
          code: [244,-814,244,-815,244,-814,245,-814,244,-814,244,-815,244,-3214,520,-1542,520,-521,520,-1542,1550,-533,520,-521,520,-521,520,-521,520,-534,520,-1543,520,-529,520,-1543,1542,-516,247]
          transmitter_id: RF_TX
          repeat:
            times: 4
            wait_time: 5ms
    turn_off_action:
      - remote_transmitter.transmit_raw:
          code: [244,-814,244,-814,244,-814,245,-814,244,-814,244,-814,244,-3205,520,-1542,521,-521,520,-521,1541,-1563,521,-521,520,-521,1541,-522,1541,-533,520,-1551,520,-521,1541,-1543,520,-1537,248]
          transmitter_id: RF_TX
          repeat:
            times: 4
            wait_time: 5ms

#Enable internal web server

#web_server:
#  port: 80

ota:
  password: !secret ota_pswd

# Enable logging
logger:
#  level: info

I just did this myself for a new humidifier I bought with a remote control. I had thought of hacking one of my old non-remote humidifiers with an ESP but needed a bigger capacity anyway.

For me, I didn’t go the ESP route but I did use a Broadlink RMMINI to relay my IR, so the process is similar. If your question is how to solve your problem I’m happy to offer what I’ve done, if it’s more about how to make ESPHome do what you want then I’ll let someone else who has worked with IR under ESPHome answer it for you :slight_smile:

@CO_4X4 On mine, I am not using IR, but RF. Not sure if that does follow the same rules as I am very new at automation. I got the codes in my other post to work correctly with the turn on/off with the RF RAW. I just am having issues wrapping my head around if I am supposed to be using the switch template or if I am supposed to be using something else. Then using the loop function as in the instructions don’t really go line by line in the documentation with comments. A majority of everything that is in this configuration is from the ESP360 example.

My next portion of this is to automate my AC which does use IR, but I don’t need that for a while since it is winter.

The rules would be more or less the same, my device does RF and IR. It’s really more about how to do “IR/RF in the blind” when you don’t have actual feedback telling you that something happened. I wrote a bunch of scripts that help me deal with that and other things like “how much water is left in the tank” by tracking run times and water levels at those run times then I can automate notifications to remind me to fill the humidifier, etc.

@CO_4X4 The issue mainly is, I don’t understand how to do the loop, if/then, or the build of the average. The documentation is very confusing to me as none of it is broken out line by line. The examples page leaves some to be desired as the examples are only for areas I haven’t even delved into. If you are able to explain to me how I even format the loop for my use (I am not really even sure if I’m using the right headers or what for all of this as the base explanation for someone that has not been running home assistant since the inception has no basic/dummy proof coding examples other than “error” which can/cannot be applicable in my scenario). I know two variables are correct. Both RAW statements are correct in data needed to turn on and off the humidifier.

I also would love to see your script for the tank level and how you dealt with that as those were slated for more advanced steps once I could figure out the basics.

I think I am getting a bit further, but still am having issues. I manually offset the value of the humidity sensor below the limits, but I am not seeing them trigger. I was able to manually trigger so I know the RAW’s are correctly captured. Is anyone able to look at this YAML and see where I am goofing this up or even if I am doing this correctly?

esphome:
  name: lr-esp360
  friendly_name: lr-esp360

esp32:
  board: esp32dev
  framework:
    type: arduino

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pswd

api:
  encryption:
    key: !secret noise_enc
  services:
    - service: send_ir_raw
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            transmitter_id: IR_TX
            code: !lambda 'return command;'
    - service: send_rf_raw
      variables:
        protocol_number: int
        raw_data: string
      then:
        - remote_transmitter.transmit_rc_switch_raw:
            transmitter_id: RF_TX
            protocol: !lambda 'return  esphome::remote_base::RC_SWITCH_PROTOCOLS[protocol_number];'
            code: !lambda 'return raw_data;'

#i2c bus definition

i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

#Offsets have been added to the measurements taken. 
#These are necessary due to the heat generated by the PCB, the second layer of the board, and the case. 
#This way, sufficiently accurate values are obtained for automations

sensor:
  - platform: shtcx
    temperature:
      id: temperature360
      name: "ESP360 Temperature Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
        - offset: -3.7
    humidity:
      id: humidity360
      name: "ESP360 Humidity Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
#        - offset: +19.1
        - offset: -20.0
    update_interval: 300s

  - platform: bh1750
    name: "ESP360 BH1750 Illuminance Living Room"
    filters:
      - multiply: 5
    address: 0x23
    update_interval: 10s

  - platform: wifi_signal
    name: "ESP360 Living Room WiFi signal"
    update_interval: 60s

#Setting up IR and RF receivers

remote_receiver:
  - id: RF_RX
    pin: 
      number: GPIO5
      inverted: True
    dump: raw
    tolerance: 35%
    filter: 255us
    idle: 10ms
    buffer_size: 10kb
    on_rc_switch:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            protocol: !lambda 'return x.protocol;'
            code: !lambda 'return x.code;' 
  - id: IR_RX
    pin: 
      number: GPIO19
      inverted: True
    dump: all
    on_raw:
       - homeassistant.event:
          event: esphome.ir_raw_code_received
          data:
            code: !lambda '
            reverse(x.begin(), x.end());
            std::string str = "";
            for (auto it = x.begin(); it != x.end(); ++it)
            {
              str += to_string(*it);
              str += ",";
              if (it != x.end() - 1) str += " ";
            }
            if (str.size() > 0)  str.erase( str.end() - 2);
            return esphome::to_string("[" + str + "]");'   
    on_pronto:
       - homeassistant.event:
          event: esphome.ir_pronto_code_received
          data:
            code: !lambda 'return x.data;'
    on_lg:
       - homeassistant.event:
          event: esphome.ir_lg_code_received
          data:
            data: !lambda 'return x.data;'
            nbits: !lambda 'return x.nbits;'
    on_panasonic:
       - homeassistant.event:
           event: esphome.ir_panasonic_code_received
           data:
            address: !lambda 'return x.address;'
            command: !lambda 'return x.command;'

#Setting up IR and RF transmitters

remote_transmitter:
  - id: RF_TX
    pin: GPIO18
    carrier_duty_percent: 100%
  - id: IR_TX
    pin: GPIO13
    carrier_duty_percent: 50%

script:
  id: humidity_control
  mode: restart
  then:
    - if:
        condition:
          and:
            - lambda: "return id(humidity360).state > 45.0;"
        then:
          - remote_transmitter.transmit_raw:
              code: [244,-814,244,-815,244,-814,245,-814,244,-814,244,-815,244,-3214,520,-1542,520,-521,520,-1542,1550,-533,520,-521,520,-521,520,-521,520,-534,520,-1543,520,-529,520,-1543,1542,-516,247]
              transmitter_id: RF_TX
              repeat:
                times: 4
                wait_time: 10ms
          - delay: 20s
        else:
          - if:
              condition:
                and:
                  - lambda: "return id(humidity360).state <= 45.0;"
              then:
                - remote_transmitter.transmit_raw:
                    code: [244,-814,244,-814,244,-814,245,-814,244,-814,244,-814,244,-3205,520,-1542,521,-521,520,-521,1541,-1563,521,-521,520,-521,1541,-522,1541,-533,520,-1551,520,-521,1541,-1543,520,-1537,248]
                    transmitter_id: RF_TX
                    repeat:
                      times: 4
                      wait_time: 10ms
                - delay: 20s

switch:
  - platform: template
    name: "Humidifier"
    turn_on_action:
      - remote_transmitter.transmit_raw:
          code: [244,-814,244,-815,244,-814,245,-814,244,-814,244,-815,244,-3214,520,-1542,520,-521,520,-1542,1550,-533,520,-521,520,-521,520,-521,520,-534,520,-1543,520,-529,520,-1543,1542,-516,247]
          transmitter_id: RF_TX
          repeat:
            times: 4
            wait_time: 5ms
    turn_off_action:
      - remote_transmitter.transmit_raw:
          code: [244,-814,244,-814,244,-814,245,-814,244,-814,244,-814,244,-3205,520,-1542,521,-521,520,-521,1541,-1563,521,-521,520,-521,1541,-522,1541,-533,520,-1551,520,-521,1541,-1543,520,-1537,248]
          transmitter_id: RF_TX
          repeat:
            times: 4
            wait_time: 5ms

# Status LED and Humidifier Switches
status_led:
  pin:
    number: 12
    inverted: true

ota:
  password: !secret ota_pswd

# Enable logging
logger:
#  level: info

Finally have a working config

esphome:
  name: lr-esp360
  friendly_name: lr-esp360

  on_boot:
    priority: 800
    then:
      - script.execute: humidity_control

esp32:
  board: esp32dev
  framework:
    type: arduino

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_pswd

api:
  encryption:
    key: !secret noise_enc
  services:
    - service: send_ir_raw
      variables:
        command: int[]
      then:
        - remote_transmitter.transmit_raw:
            transmitter_id: IR_TX
            code: !lambda 'return command;'
    - service: send_rf_raw
      variables:
        protocol_number: int
        raw_data: string
      then:
        - remote_transmitter.transmit_rc_switch_raw:
            transmitter_id: RF_TX
            protocol: !lambda 'return  esphome::remote_base::RC_SWITCH_PROTOCOLS[protocol_number];'
            code: !lambda 'return raw_data;'

#i2c bus definition

i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

#Offsets have been added to the measurements taken. 
#These are necessary due to the heat generated by the PCB, the second layer of the board, and the case. 
#This way, sufficiently accurate values are obtained for automations

sensor:
  - platform: shtcx
    temperature:
      id: temperature360
      name: "ESP360 Temperature Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
        - offset: -3.7
    humidity:
      id: humidity360
      name: "ESP360 Humidity Living Room"
      filters:
        - median: 
            window_size: 5
            send_every: 1
        - offset: +9.6
    update_interval: 300s

  - platform: bh1750
    name: "ESP360 BH1750 Illuminance Living Room"
    filters:
      - multiply: 5
    address: 0x23
    update_interval: 30s

  - platform: wifi_signal
    name: "ESP360 Living Room WiFi signal"
    update_interval: 60s

#Setting up IR and RF receivers

remote_receiver:
  - id: RF_RX
    pin: 
      number: GPIO5
      inverted: True
    dump: raw
    tolerance: 35%
    filter: 255us
    idle: 10ms
    buffer_size: 10kb
    on_rc_switch:
      - homeassistant.event:
          event: esphome.rf_code_received
          data:
            protocol: !lambda 'return x.protocol;'
            code: !lambda 'return x.code;' 
  - id: IR_RX
    pin: 
      number: GPIO19
      inverted: True
    dump: all
    on_raw:
       - homeassistant.event:
          event: esphome.ir_raw_code_received
          data:
            code: !lambda '
            reverse(x.begin(), x.end());
            std::string str = "";
            for (auto it = x.begin(); it != x.end(); ++it)
            {
              str += to_string(*it);
              str += ",";
              if (it != x.end() - 1) str += " ";
            }
            if (str.size() > 0)  str.erase( str.end() - 2);
            return esphome::to_string("[" + str + "]");'   
    on_pronto:
       - homeassistant.event:
          event: esphome.ir_pronto_code_received
          data:
            code: !lambda 'return x.data;'
    on_lg:
       - homeassistant.event:
          event: esphome.ir_lg_code_received
          data:
            data: !lambda 'return x.data;'
            nbits: !lambda 'return x.nbits;'
    on_panasonic:
       - homeassistant.event:
           event: esphome.ir_panasonic_code_received
           data:
            address: !lambda 'return x.address;'
            command: !lambda 'return x.command;'

#Setting up IR and RF transmitters

remote_transmitter:
  - id: RF_TX
    pin: GPIO18
    carrier_duty_percent: 100%
  - id: IR_TX
    pin: GPIO13
    carrier_duty_percent: 50%

script:
  - id: transmit_on_command
    then:
      - repeat:
          count: 4
          then:
            - remote_transmitter.transmit_raw:
                code: [244,-814,244,-815,244,-814,245,-814,244,-814,244,-815,244,-3214,520,-1542,520,-521,520,-1542,1550,-533,520,-521,520,-521,520,-521,520,-534,520,-1543,520,-529,520,-1543,1542,-516,247]
                transmitter_id: RF_TX
            - delay: 10ms

  - id: transmit_off_command
    then:
      - repeat:
          count: 4
          then:
            - remote_transmitter.transmit_raw:
                code: [244,-814,244,-814,244,-814,245,-814,244,-814,244,-814,244,-3205,520,-1542,521,-521,520,-521,1541,-1563,521,-521,520,-521,1541,-522,1541,-533,520,-1551,520,-521,1541,-1543,520,-1537,248]
                transmitter_id: RF_TX
            - delay: 10ms

  - id: humidity_control
    mode: restart
    then:
      - lambda: |-
          if (id(humidity360).state > 45.0) {
            ESP_LOGD("humidity_control", "Humidity is above 45.0, executing OFF command script.");
            id(transmit_off_command).execute();
          } else {
            ESP_LOGD("humidity_control", "Humidity is 45.0 or below, executing ON command script.");
            id(transmit_on_command).execute();
          }

interval:
  - interval: 20s
    then:
      - script.execute: humidity_control

# Status LED and Humidifier Switches
status_led:
  pin:
    number: 12
    inverted: true

ota:
  password: !secret ota_pswd

# Enable logging
logger:
#  level: info