ESPhomelib, Native API, publish sensor value back

tl;dr,
I can change a homeassistant sensor value in esphomelib, but how can that state be published back to home assistant? This is homeassistant sensor using the native api, not mqtt

Using this PR (thanks OttoWinter),
homeassistant native api sensors now work again.

sensor:
   - platform: homeassistant
     entity_id: input_number.temp
     id: hass_temp

I can send state updates with the input slider in hass and they directly change on my esphomelib display. Good.
I can directly read and minupulate the sensor state with functions like:

          - if:
              condition:
                lambda: |-
                  return (id(hass_temp).state -0.5 >= x); 
              then:
                - switch.turn_on: therm_switch

and

    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - lambda: |-
            id(hass_temp).state--;

What does not work is actually changing the state of the sensor in hass when those buttons are clicked. The state only changes on the esp. I can only find references to updating and publishing new statuses with MQTT, but this is using the Native API.
How can I publish a sensor update back to home assistant?

try:

id(hass_temp).publish_state('value');

I have tried that, yes, but it does not change anything. No errors in either hass log or esphomeyaml debug log.
That may be only for MQTT? Besides, it’s my understanding that state changes are published automatically anyway?

I’m still not sure how to do this, or if the native api with homeassistant can be used for two-way publishing.
In the meantime, I was able to sucesfully get my situation working by replacing the native api with mqtt.
HOWEVER, this now exposing my entities twice unless I disable the native api for everything else. Any suggestions?

Something like this:

- alias: temp_slider_moved
  trigger:
    platform: state
    entity_id: input_number.temp
  action:
    service: mqtt.publish
    data_template:
      topic: 'thermostat/temp'
      retain: false
      payload: "{\"temperature\":\"{{ states('input_number.temp')|int }}\"}"

- alias: set_temp_slider
  trigger:
    platform: mqtt
    topic: 'thermostat/temp'
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.temp
      value: "{{ trigger.payload }

and then in esphomelib:

mqtt:
  on_json_message:
    - topic: thermostat/temp
      then:
        - lambda: |-
            int new_temp = 50; 
            if(x.containsKey("temperature"))
              new_temp = x["temperature"];
            id(set_point) = new_temp;
binary_sensor:
......
    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - lambda: |-
            id(set_point)--;
        - mqtt.publish:
            topic: thermostat/temp
            payload: !lambda |-
              return esphomelib::to_string(id(set_point));

Yes these sensors are one-way only - and that won’t change because Home Assistant doesn’t support anything else.

You were quite close though. You can use the homeassistant.service action:

on_click:
# ...
- homeassistant.service:
    service: input_number.set_value
    data_template:
      entity_id: input_number.temp
      value: "{{ set_point }}"
    variables:
      set_point: 'return id(set_point).state - 1;'
2 Likes

In case anyone ends up on this thread looking for how to publish an increase/decrease in Brighness back to HA , this is what I’ve now got working - the above was a good pointer, but an integer is required.:

- homeassistant.service:
    service: light.turn_on
    data_template:
      entity_id: "light.bulb_01_test_light"
      brightness: "{{ brightness_1 | int }}"
    variables:
      brightness_1: 'return id(bulb_01_test_light_bright).state + 5;'
2 Likes

Thanks, @DeanoX. I’m trying to use that approach and am getting errors. Any idea what I’m doing wrong, please?

  - homeassistant.service:
      service: light.turn_on
      data_template:
        entity_id: "light.study1"
        brightness: "{{ brightness_1 | int }}"
      variables:
        brightness_1: 'return id(study1_bright).state - 5;' 

When I try to validate that I get errors (don’t know how to use the colours to show errors unfortunately so will attach screenshot, too):

INFO Reading configuration...
Failed config

binary_sensor.gpio: 
  platform: gpio
  pin: 
    number: 4
    mode: INPUT_PULLUP
    inverted: True
  name: Down
  device_class: door
  on_multi_click: 
    - timing: 
        - min_length: 1 s
          state: True
          max_length: 2 s
        - min_length: 500 ms
          state: False
      then:  [source /config/esphome/test2.yaml:64]
        - logger.log: 
            format: Down Single Long Clicked
            args: []
            tag: main
            level: DEBUG
        - homeassistant.service: 
            service: light.turn_on
            data_template: 
              entity_id: light.study1
              brightness: {{ brightness_1 | int }}
            variables: 
              
              Couldn't find ID 'study1_bright'
              brightness_1: !lambda |-
                return id(study1_bright).state - 5;
    - timing: 
        - min_length: 0 ms
          state: True
          max_length: 1 s
        - min_length: 500 ms
          state: False
      then: 
        - logger.log: 
            format: Down Single short Click
            args: []
            tag: main
            level: DEBUG
        - homeassistant.service: 
            service: light.toggle
            data: 
              entity_id: light.study1

Any help gratefully received. Thanks.

Sure hope a solution can be found for this @RGN01
Been looking for weeks to find a way to do this.
Well done for your efforts. Unfortunately I am still learning so cannot offer any assistance with code sadly.
Following at present :slight_smile: