Send hexadecimal content from input_text

Hi guys,

I have an esp-home device with a CAN-bus controller connected.
I currently use this to read data from my heat pump.

For some basic data I already know the correct commands to receive a nice answer and hardcode the CAN-bus command in the ESPhome device like this:

          - canbus.send:
              data: [ 0x31, 0x00, 0xfa,0x09,0x28,0x00,0x00 ]
              can_id: 0x680
          - delay: 500ms

I can also read any answers from the pump with the following code

    - can_id: 0x180
      then:
        - lambda: |-
              int val0 = int(x[0]);
              int val1 =int(x[1]);
              int val2 =int(x[2]);
              int val3 =int(x[3]);
              int val4 =int(x[4]);
              int val5 =int(x[5]);
              int val6 =int(x[6]);
              ESP_LOGI("main", "answer from 180 hex: %x %x %x %x %x %x %x", val0, val1, val2, val3, val4, val5, val6);

Now for troubleshooting, if I want to send commands dynamically to change parameters in the heat pump or read other data from it. I want to send commands from an home assistant input_text field and see what’s happening in the log.

I created an home assistant input_text in the config.yaml:

input_text:
  dynamic_input:
    name: dynamic_input1
    initial: default

and in ESP-home I already can receive the content and print it in the log, using this:

text_sensor:
  - platform: homeassistant
    name: "dynamic_input1"
    entity_id: input_text.dynamic_input1
    id: HASSinput

switch:
  - platform: template
    name: "print dynamic_input1"
    id: p_input
    turn_on_action:
      lambda: |-
        std::string val = to_string(id(HASSinput).state);
        ESP_LOGI("main", "Value of my sensor: %s", val.c_str());
    turn_off_action:
      - delay: 500 ms

This prints the input of my home assistant sensor in the log of the ESP-home device, every time the switch is activated in home assistant.

But how can I convert an input string like “0x31,0x00,0xfa,0x09,0x2a,0x00,0x00” or “\x31\x00\xfa\x09\x2a\x00\x00” into code that can be sent over the can-bus as typed in?

Alright, I’m kind of getting a clue, how to get there:
I can send a string of ASCII-data using the ALT+Num method.
So I convert my hex data package - let’s say 31 00 fa 09 2a 00 00 into decimals: 49 0 250 9 42 0 0
Then I type this using Alt+Numkey: 1 ·○*
This data can be written in an integer variable.
There are two further questions:

  1. How could I handle the fact that “00” delivers no content?
  2. How can I pass the integer values into the canbus_send command?

I also thought already of using node red, rather then an input_text. But I don’t know how to start with this either.

Almost there:

I can send integer values over the can-bus using this code-snippet:

      - canbus.send: 
          data: !lambda
            return {(uint8_t) id(val0),(uint8_t) id(val1),(uint8_t) id(val2),(uint8_t) id(val3), (uint8_t) id(val4),(uint8_t) id(val5),(uint8_t) id(val6)};
          can_id: 0x680

I created 7 input_text sensors for the payload.
Now I’m only missing, how I could transport integer numbers instead of the assumed string-content.

Alright, just got another clue.
I can convert integer values from string to int now with the following code.
I’m using val0 to val6 as a global variable, that I declared in the top of the code, so that it is available for the canbus.send command.

So at least I can calculate from hex to decimal values, put the decimal values in the input_text of homeassistant and send it to the can-bus.
What would be really nice now, would be something like parse_hex, to parse the string directly into hex-values. But using parse_hex doesn’t work.
Does anyone know a different function, like atoi (converts string to integer), but for hex-code?

  - platform: homeassistant
    name: "eingabefeld1"
    entity_id: input_text.feld1
    id: HASSeingabe1
    filters:
     - lambda: |-
          int eingabe1=atoi(x.c_str());
          if (x == "null") {
            id(val1)=0x00;
            return x;
          } else {
            id(val1)=eingabe1;
            return x;
          }

Just finished it.
Some Helper inputs take the number in hex and a home-assistant script converts from hex to int now.
In the end, when I push the switch, the command is sent over the can-bus.
Maybe it is not a gorgeous solution, but it is doing the job.

hex_to_int:
  alias: hex_to_int
  sequence:
  - service: input_text.set_value
    data:
      value: "{{ states('input_text.h0')|int(base=16) }}"
    target:
      entity_id: input_text.feld0

1 Like