Using Home Assitant sensor state as value for QR

Hi gurus,

First a disclaimer, I am no programmer, and I am probably doing some basic mistakes. But I have issues finding good documentation on how to re-use data from Home Assistant within ESPHome.

A short objective. I have a sensor in Home Assistant that consists of a URL. I import this into my ESPHome device as a text sensor, and want to use that as value to create a QR code using the qr module.

The issue is that my generated QR code does not contain the URL, it either contains the name of the HA sensor, the id from the ESPHome text-sensor or something like that.

Here is a small part of my config, just the parts that I think is relevant:

substitutions: #substitute your own values in this section
  untappd : sensor.tap_1_untappd #entity from Home Assistant

text_sensor:
  - platform: homeassistant
    id: beer_untappd
    entity_id: $untappd
    internal: true

qr_code:
  - id: untappd_qr
    value: beer_untappd

display:
    platform: waveshare_epaper
    model: 2.13in-ttgo-dke
    lambda: |-
      // Print QR code for Untappd
      it.qr_code(20, 150, id(untappd_qr), Color(255,255,255), 4);

I think I have tried all the different options that does not work. Anyone that can point me in the right direction to get the URL output as input value to the qr code generation?

Oh, and I can confirm that I do get the value from Home Assistant. Here is from the logs:

[14:33:44][D][homeassistant.text_sensor:017]: 'sensor.tap_1_untappd': Got state 'https://untappd.com/qr/brewery/510427'
[14:33:44][D][text_sensor:067]: 'beer_untappd': Sending state 'https://untappd.com/qr/brewery/510427'

Thanks

::Rune

Looking at the documentation: Display Component — ESPHome I don’t think it is possible to have a dynamic value for the QR-code, because the value is a fixed string (the documentation doesn’t say the value is templatable and that is what is needed if you want to fill it on runtime).

Configuration variables:

  • id (Required, ID): The ID with which you will be able to reference the QR-code later in your display code.
  • value (Required, string): The string which you want to encode in the QR-code.

Thanks for your info.

Do you have any workarounds that might work for me? It is really just a string, but it is stored in a variable. I will try to add it in a local variable instead, and see if that can work.
But you are probably right, I would need to raise a feature request

No, I don’t have a work around, you could create a custom qr-code component yourself. :grinning:

I just tried it with this

qr_code:
  - id: untappd_qr
    value: !lambda return id(beer_untappd).state;

however as expected I got an error:

Failed config

qr_code: [source test-esp32.yaml:112]
  - id: untappd_qr
    
    This option is not templatable!.
    value: !lambda |-
      return id(beer_untappd).state;
1 Like

Before giving up completely (no, I am not even remotely able to create my own QR code implementation) I have one more info to give. I did try with a global variable, but no luck yet at least.

In the config in my original post, the qr code points to the value, so it gives me the result “beer_untappd”

If I set the value: in the qr_code module to $untappd I actually get “sensor.tap_1_untappd” as the link on the QR code. So it can actually read the content of the variable, just not the substitution. I have also tried with $untappd.state, but that gives me “sensor.tap_1_untappd.state”. And I have tried with single quotes, curly brackets etc.

Don’t know if that makes it possible for anyone to help me get the correct value into the qr.

Hi Rune,

I did a small dive into the code for the qr-component and found a workaround, just add the following line to the display lambda

display:
    platform: waveshare_epaper
    model: 2.13in-ttgo-dke
    lambda: |-
      // Print QR code for Untappd
      untappd_qr->set_value(id(beer_untappd).state.c_str()); // <-- this line fills the value from the text sensor 
      it.qr_code(20, 150, id(untappd_qr), Color(255,255,255), 4);
6 Likes

Hi,

That worked!! Amazing. Thank you so much for looking into it.

You are now officially my hero