Reading a homeassistant sensor value in a template

I’m trying to read a homeassistant sensor value in a template and then pass this back to HomeAssistant but can’t figure out the syntax.

The bit I am struggling with is brightness: '{{sensor.ha_study_brightness - 5}}' Any help would be greatly appreciated.

sensor:
  - platform: homeassistant
    name: "ha_study_brightness"
    entity_id: sensor.study_brightness

binary_sensor:
  - platform: gpio
    pin:
      number: 00
      mode: INPUT_PULLUP
      inverted: TRUE
    id: Up
    name: "Up"
    device_class: light
    on_press:
      while:
        condition:
          binary_sensor.is_on: Up
        then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: light.study
              brightness: '{{sensor.ha_study_brightness - 5}}'
        - logger.log: "Up pressed"
        - delay: 0.05s

The corresponding sensor in HA is as follows:

  - platform: template
    sensors:
      study_brightness:
        friendly_name: "Study Brightness"
        value_template: "{{ state_attr('light.study', 'brightness') }}"

More detail: This is an attempt to use the ESP8266 as a dimmer for a light on HA, not on the ESP. In the code above, the ha_study_brightness sensor pulls the brightness of a light from HA - this is working as it shows in the logs. The line brightness: '{{sensor.ha_study_brightness - 0.5}}' is intended to subtract 5 from that value for it to be sent back to HA to set the light brightness.

Try:

        - homeassistant.service:
            service: light.turn_on
            data_template:
              entity_id: light.study
              brightness: "{{states('sensor.ha_study_brightness') |int - 5}}"

Note the use of data_template instead of data and the casting of the sensor state as an integer (instead of the default, which is a string).

1 Like

Thank you, @tom_l - that is helpful.

Unfortunately it does not change the light brightness. Being a beginner with this, I miss being able to debug and see values of variables etc. so use a trial and error approach that leads to lots of questions!

I’m certainly no expert either. I haven’t tried this sort of complexity. I just noted this from the esphome docs:

data_template ( Optional , mapping): Optional template data to pass along with the service call. This is evaluated on the Home Assistant side with Home Assistant’s templating engine.

From here: Native API Component — ESPHome

As it says it is evaluated on the HA side I formatted the template as I would do if using a template in HA.

Thanks again, @tom_l - every little helps, so they say, and you have got me closer to solving this and helped me understand that aspect so I really do appreciate your help!

Another part of the puzzle: this needs to be int or float before you subtract I believe.

{{sensor.ha_study_brightness|int - 5}}

Yes, converting to an integer was included in my answer:

brightness: "{{states('sensor.ha_study_brightness') |int - 5}}"

In Home Assistant, the default range for brightness is a value from 0 to 255. Subtracting 5 from brightness represents a change of 2%.

That’s a very small change. What is the need for such a small decrease in brightness?

"{{states('sensor.ha_study_brightness') |int - 5}}"
1 Like

Good question , @123! Actually I hadn’t given the value much thought - at this stage I am simply trying to get the logic sorted - I’ll amend the values to suit if / when I get it working properly.