MQTT sensor, how to use with attributes?

- name: Bedroom Minibar Ambient Battery
    state_topic: "tele/ZigBee_Bridge/0253/SENSOR"
    unit_of_measurement: "%"
    device_class: battery
    value_template: >
      {% if is_number(value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0)) %}
      {{ value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0) }}
      {% else %}
      {{ states('sensor.bedroom_minibar_ambient_battery') }}
      {% endif %}
    json_attributes_topic: tele/ZigBee_Bridge/0253/SENSOR
    json_attributes_template: >
      {% if is_number(value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0)) %}
      {% set t = value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0) %}
      {% else %}
      {% set t = states('sensor.bedroom_minibar_ambient_battery') %}
      {% endif %}
      { "string_state": 
        "{{ 'low' if t <= 30 else
            'normal' if t > 30 else
            this.attributes.string_state | default('unknown') }}" }

This is what it shows

Edit: I think it is working, but it should first receive a value, than the state will show up :smiley:

Thanks!

I didn’t change anything in value_template. It’s probably reporting unknown because it hasn’t received a payload yet.

1 Like

Exactly!

And, can I do something to show a value if no payload received yet? Maybe show the last known value?

Edit: I have solved it by making the device publish the value sensor as retained. It is a good solution or I can do something from this code?

The MQTT Sensor’s value_template is processed only when a payload is received.

1 Like

I still got some issues, because if I would receive a different sensor’s value from the same device, the BatteryPercentage would act like it had no payload… so i did this to solve it:

  - name: Bedroom Minibar Ambient Battery
    state_topic: "tele/ZigBee_Bridge/0253/SENSOR"
    unit_of_measurement: "%"
    device_class: battery
    value_template: >
      {% if value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] %}
      {% if is_number(value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0)) %}
      {% set var = value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0) %}
      {% else %}
      {% set var = states('sensor.bedroom_minibar_ambient_battery') %}
      {% endif %}
      {{ var }}
      {% endif %}
    json_attributes_topic: tele/ZigBee_Bridge/0253/SENSOR
    json_attributes_template: >
      {% if value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] %}
      {% if is_number(value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0)) %}
      {% set var = value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0) %}
      {% else %}
      {% set var = states('sensor.bedroom_minibar_ambient_battery') %}
      {% endif %}
      { "string_state": 
        "{{ 'low' if var <= 30 else
            'normal' if var > 30 else
            this.attributes.string_state | default('unknown') }}" }
      {% else %}
      { "string_state": "{{this.attributes.string_state}}" }
      {% endif %}

Kinda a big code for just a sensor :frowning: Do you know any other solution, with a smaller code? :smiley: I want to optimize how the code looks and make it smaller

If the state_topic can receive payloads meant for more than one sensor, you should consider implementing Strategy #2 described in the following topic: