ESPhome: Is it possible to get state of Home Assistant sensor with MQTT?

Hello, I would like to control one value in ESPhome from Home Assistant. I could set up input sensor in HA and send it to ESPhome “homeassistant” sensor, but I cannot do it.

I do not use HA api, I use mqtt. Is it possible to send input sensor value from HA to ESPhome over MQTT? I know I could construct mqtt topic, but I am not that good to compose mqtt message with correct topic and in ESPhome to decode it and strip topic value and assign it to variable or sensor. Is there an easier way how to do it? The reason why I do not use HA API is that sensor and Home Assistant are on different networks only connected over internet. MQTT works fine.

No. But we can help.

Use use mqtt publish service in an automation triggered to change on any state change of your sensor.

You can pick any topic you want.

Start by telling us the entity id of the sensor you wish to pass to ESPHome, and what sort of values it contains.

Thanks Tom. The ESPhome sensor checks sunlight and rolls external blinds up or down. It also check weather for upcoming wind, compares it against constant (10kt) and if the predicted wind is too strong, it pulls blinds up.

I want to be able to replace constant with number defined in Home Assistant by input_number.wind_threshold sensor. That is from 0 to 25. I would be fine if I receive it in ESPhome as variable, or as sensor. I found example to decode JSON, so I think I would be fine if I can construct JSON payload in HA in automation when state of input_number.wind_threshold changes. Is that the way to go?

If it is only a single number there’s no need for json, also the ESPHome mqtt subscribe sensor only supports numeric data. So, the Automation used to publish the state:

automation:
  - alias: "Publish Wind Threshold"
    id: publish_wind_threshold # any unique string
    trigger:
      platform: state # trigger on any change of state (or attribute change) of the input_number
      entity_id: input_number.wind_threshold
    condition:
      - "{{ trigger.from_state.state not in ['unknown', 'unavailable', 'none'] }}"
      - "{{ trigger.to_state.state not in ['unknown', 'unavailable', 'none'] }}"
      - "{{ trigger.from_state.state != trigger.to_state.state}}"
    action:
      service: mqtt.publish
      data:
        topic: homeassistant/variables/wind_threshold/
        payload_template: "{{ states('input_number.wind_threshold')|float }}"
        retain: true

The conditions prevent false triggering.

And in ESPHome:

sensor:
  - platform: mqtt_subscribe
    name: "Wind Threshold"
    id: wind_threshold
    topic: homeassistant/variables/wind_threshold/
    unit_of_measurement: "m/s" # or whatever it is
1 Like

Thanks, you are a genius, it worked on first try! Jan

1 Like