Pass value as int from HA to ESPhome?

Hi guys :slight_smile:
I have passed a sensor from HA to my ESP home, it works fine, it shows up in the esp32 logs as expected. But when I try to use the value in my esp32, i get this fault:

Expected integer, but cannot parse id(“sdm_630_address0”).state as an integer.

It’s defined as an Int in HA.
This is the import:

  - platform: homeassistant
    name: "sdm-630-address0"
    entity_id: sensor.sdm630address0
    id: "sdm_630_address0"

This i where I try to use it:

modbus_server:
  - id: modbuserver
    uart_id: smartmetersim
    address: 2 # slave address
    holding_registers:
      - start_address: 0
        default: id("sdm_630_address0").state # default value of register
        number: 1
        on_read: return value; 

I have also tried without .state and with ->state but same result.
Can anyone point me in the right direction? Thanks :slight_smile:

Is your sensor defined under sensor: and not text_sensor or other type?

Alternatively, if I have trouble trying to see what the sensor type? is, I use logging.

  - interval: 30s
    then:
      - logger.log:
          format: "sdm-630-address0 : %i"
          args: [ 'id(sdm_630_address0)' ]

If %i doesn’t work, try %.0f (for float) %s (for text).

I’m sure there is a better way, but this should work.

Also it might be that you are using double quotes … I’m reasonable sure that you don’t need them in the ‘setup’ part and only single quotes in the other part :slight_smile:

1 Like

It was under text-sensor, but i have now moved it to sensor:

  - platform: homeassistant
    name: "sdm-630-address0"
    entity_id: sensor.sdm630address0
    id: sdm_630_address0

And

modbus_server:
  - id: modbuserver
    uart_id: smartmetersim
    address: 2 # slave address
    holding_registers:
      - start_address: 0
        default: int(id("sdm_630_address0").state) # default value of register
        number: 1
        on_read: return value; 

Still makes the same error,
default: id(“sdm_630_address0”).state is also tried.

If I paste your code under logging like this:

logger:
  level: VERBOSE #makes uart stream available in esphome logstream
  baud_rate: 0 #disable logging over uart
  - interval: 30s
    then:
      - logger.log:
          format: "sdm-630-address0 : %i"
          args: [ 'id(sdm_630_address0)' ]

I just get logging errors because im using uart.

modbus_server:
  - id: modbuserver
    uart_id: smartmetersim
    address: 2 # slave address
    holding_registers:
      - start_address: 0
        default: int(id('sdm_630_address0'.state)) # default value of register
        number: 1
        on_read: return value;

All I changed there was the " to ’ , oh, I also moved a bracket.

I use (int)id(some_sensor) although, both may work :slight_smile:

I’m no expert btw, did I mention that?

interval:
  - interval: 1min

Interval doesn’t go under logger: it is it’s own component (but like you say you are using uart so it won’t work)

That didn’t work either, but the interval log gives this:

[21:20:24][D][main:261]: sdm-630-address0 : 1073429584

Without the interval inserted I get this:

[21:30:24][D][text_sensor:067]: 'sdm-630-address0': Sending state '231'

Which makes no sense to me, and no worries, I take all the input I can get, I’m new in this field :wink:

Yeah, I don’t know … but when you work it out, don’t forget to come back and let the rest of us know what you did :slight_smile:

Could it expect hex input perhaps? Just guessing.

Maybe, but it won’t solve my initial problem :slight_smile:

1 Like

No one else?

Seems you need to start with !lambda in order to reference id’s.

Can you try this?

  default: !lambda return id('sdm_630_address0').state;

What does that mean?

modbus_server:
  - id: modbuserver
    uart_id: smartmetersim
    address: 2 # slave address
    holding_registers:
      - start_address: 0
        default: !lambda return id('sdm_630_address0').state;
        number: 1
        on_read: return value; 

Just gives me this error: “This option is not templatable!.”

This means you cannot dynamically set a value for default and have to set a fixed, static value.

2 Likes

Try with:

modbus_server:
  - id: modbuserver
    uart_id: smartmetersim
    address: 2 # slave address
    holding_registers:
      - start_address: 0
        default: 1
        number: 1
        on_read: |
           return int(id('sdm_630_address0').state);