Passing string from HA to ESP32

This was supposed to be simple, but I’m tying myself up in knots. The ESP32 documentation… for some reason it’s not, “clicking,” in my head and AI has been driving me round the twist because it seems to be hallucinating.

The situation is straightforward. It’s an IR blaster and rather than on-board codes, I want to pass the strings from HA to the ESP32 (C3 Zero) to transmit, so that I don’t have to keep large sections of IR codes on the limited C3 flash.

Here’s my current code which is simply publishing buttons and then triggering, but the actual codes I need to send are nearly 900 characters long, so I’m not sure if I’m hitting limits anywhere…

esphome:
  name: ir-blaster
  comment: "IR Blaster device"

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: arduino

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: !secret IRBlasterapi

ota:
  - platform: esphome
    password: !secret IRBlasterota

captive_portal:

# IR transmitter
remote_transmitter:
  id: my_remote_transmitter
  pin: GPIO0
  carrier_duty_percent: 50%

# IR receiver (optional)
remote_receiver:
  pin: GPIO1
  dump: all
  tolerance: 25%
  filter: 50us
  
button:
- platform: template
  name: "Open Drawer"
  on_press: 
    then:
      - remote_transmitter.transmit_pronto:
          data: "0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012..."
- platform: template
  name: "Power Button"
  on_press: 
    then:
      - remote_transmitter.transmit_pronto:
          data: "0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012..."

Grateful for help in turning this into what I need please.

How and where are you storing a 900 character long string in HA?
But yes, that could be the issue.
Each remote action is not that long is it?
So what if you use an input text, and an automation to change the value of the input text when the remote triggers?

You could try using a `platform: home assistant" sensor - although if the data is longer than 255 characters you will need to pass it as an attribute:

text_sensor:
  - platform: homeassistant
    id: code1
    entity_id: sensor.some_sensor_made_from_a_ha_template
    attribute: sensor_attribute_for_code

...

  on_press: 
    then:
      - remote_transmitter.transmit_pronto:
          data: !lambda return id(code1).state.c_str();

Not sure if the .c_str() is required - have a play…

This is an example of your template sensor in HA:

template:
  - sensor:
      - unique_id: some_sensor_made_from_a_ha_template
        state: >
          {{ "doesn't matter what this is" }}
        attributes:   
          code1: >
            {{ "0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012..." }}
          code2: >
            {{ "0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012..." }}

etc...

The intention is to have an automation pass that code in a script, when required to be called.
An input text is an interesting thought. Good call.

This is part of my problem… I’m not sure where I’m putting this code. I’ll have a play and see what I can break :slight_smile: Thanks for taking the time to help.

Input text will probably still come up against the 255 char limit, but my suggestion doesn’t help with memory unfortunately.

This block on your ESP.

This block in configuration.yaml on your HA.

But you really only want the one sensor attribute, and do the transmit in an on_value: block. Otherwise you still chew up memory. Maybe a combination of what @Hellis81 suggested with a HA sensor. I will have to think about it.

Have you looked at User Defined Actions on ESPHome?

Maybe have a play with that - I don’t what the limit of length of the strings you can pass are though:

These show up as callable actions in HA.

Many thanks.

Thanks to the code snippets you kindly provided, I was able to get closer, and then adjust what I asked of AI. There’s a chance I might now have a solution… possibly… got to test.

It appears to work. Many thanks for all the help.

The code for ESP32 C3 Zero…

# Enable Home Assistant API
api:
  encryption:
    key: !secret IRBlasterapi
  services:
    - service: send_pronto
      variables:
        data: string
      then:
        - remote_transmitter.transmit_pronto:
            data: !lambda 'return data;'

ota:
  - platform: esphome
    password: !secret IRBlasterota

captive_portal:

# IR transmitter
remote_transmitter:
  id: my_remote_transmitter
  pin: GPIO0
  carrier_duty_percent: 50%

# IR receiver (optional)
remote_receiver:
  pin: GPIO1
  dump: all
  tolerance: 25%
  filter: 50us

The code for automation…

alias: Transmit IR Code
description: ""
triggers:
  - entity_id: sensor.ir_trigger
    to: "on"
    trigger: state
actions:
  - data:
      data: >-
        0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012 0015 0012 0015
        0012 0015 0012 0015 0012 003B 0012 0015 0012 0015 0012 0015 0012 0015
        0012 0015 0012 0015 0012 0015 0012 0015 0012 0015 0012 0014 0012 00AF
        0011 003C 0011 003C 0012 003B 0012 0015 0012 003B 0012 0015 0012 0015
        0012 0015 0012 0015 0011 0015 0011 0015 0012 0015 0012 0015 0011 003C
        0012 003B 0012 003B 0012 003B 0012 003B 0012 003B 0012 003B 0012 0181
    action: esphome.ir_blaster_send_pronto
mode: single
1 Like

Good old AI - that’s the old syntax - luckily I believe it won’t be removed - but if it ever is you will need to change it to the action: syntax.

1 Like

I’ll ask it for that syntax and test. Thanks for the heads up.

It gave me this, which appears to work…

# Enable Home Assistant API
api:
  encryption:
    key: !secret IRBlasterapi
  actions:
    - action: send_pronto
      variables:
        data: string
      then:
        - remote_transmitter.transmit_pronto:
            data: !lambda "return data;"

ota:
  - platform: esphome
    password: !secret IRBlasterota

captive_portal:

remote_transmitter:
  id: my_remote_transmitter
  pin: GPIO0
  carrier_duty_percent: 50%

remote_receiver:
  id: my_remote_receiver
  pin: GPIO1
  dump: all
  tolerance: 25%
  filter: 50us

With automation…

alias: Transmit IR Code
description: ""
triggers:
  - entity_id: sensor.ir_trigger
    to: "on"
    trigger: state
actions:
  - data:
      data: >
        0000 006D 0028 0000 001C 0009 0087 00AE 0012 0015 0012 0015 0012 0015
        0012 0015 0012 0015 0012 003B 0012 0015 0012 0015 0012 0015 0012 0015
        0012 0015 0012 0015 0012 0015 0012 0015 0012 0015 0012 0014 0012 00AF
        0011 003C 0011 003C 0012 003B 0012 0015 0012 003B 0012 0015 0012 0015
        0012 0015 0012 0015 0011 0015 0011 0015 0012 0015 0012 0015 0011 003C
        0012 003B 0012 003B 0012 003B 0012 003B 0012 003B 0012 003B 0012 0181
    action: esphome.ir_blaster_send_pronto
mode: single

Full project page - Michelle Knight's Home Assistant Pages

Interesting…

I flashed a Tuya IR Blaster (has a BK7231N CB3S microcontroller) to use with ESPHome.

Just a couple of things I’d like to mention -

First, the ESP32 is a powerhouse compared to the BK7231N. On my device, I use a “script:” block to hold the pronto codes and use the “button:” block to execute the scripts directly on the device. The ESPHome HA integration will show any buttons I program under the device. It looks like this -

script:
  # Family Room TV Controls
  - id: family_room_tv_power
    mode: queued
    then:
      - remote_transmitter.transmit_pronto:
          data: "0000 006D 0022 0002 0154 00A9 0014 0014 0014 0014 0014..."

and the matching button -

button:
  # Family Room TV Controls
  - platform: template
    name: "Family Room TV Power"
    on_press:
      - script.execute: family_room_tv_power

Second, I reviewed your link. Great work! I can confirm that you need to use the Broadlink app to setup the device, BUT I would like to mention that Broadlink devices, once integrated into HA, can be removed from the Broadlink app and you can use your router’s firewall to block it from the WAN, making it fully local. I’ve done this with the RM mini 3 and the RM4 Pro.

1 Like

Thanks for that. Yes, the ESP32 has a lot more going for it.

When I initially bought the blasters, I was intending to do just that. However, the maximum wifi password length is 32 characters, and I’m using the max. So I was somewhat stuck :slight_smile: