Cannot publish via mqtt sensor state

Hi all,
I’m trying to publish via mqtt the following:

      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[5].txt"
          payload_template: '"  {{ state("sensor.multisensor_bedroom_air_temperature") | float | round(0) }}°"'

but it shows 0° instead of the desired temperature. What am I doing wrong?

payload_template: '"{{ states("sensor.multisensor_bedroom_air_temperature") | float | round(0) }}°"'

Try this. This will send the value as a string.

payload_template: {{ states("sensor.multisensor_bedroom_air_temperature") | float | round(0) }}

Will send the temp as a number

Your second example is a single-line template so it needs outer quotes.

payload_template: '{{ states("sensor.multisensor_bedroom_air_temperature") | float | round(0) }}°'

Is this what you mean with ’ ’ or " "?

I was just looking at it in Template tool without the ’ or ". Good to know.

Documentation: Important Template Rules

Replace state with states.

There’s no state function so the float receives nothing useful and reports a default value (zero).

Be advised that you should specify the default value to be used by float. Change it to this float(0)

Also, what is this unprintable character at the beginning of the string?

payload_template: '"  {{ state("sensor.
                    ^
                    |
            What is this for?

I tried with states instead of state with no success
@123 it is a FontAwesome symbol being displayed on HaspOne.

I mean it works but it shows 0°

payload_template: "{{ states("sensor.multisensor_bedroom_air_temperature") | float(0) | round(0) }}°"

Have you tried it with the " " and float(0).

Do you get anything if you put

"{{ states("sensor.multisensor_bedroom_air_temperature") | float(0) | round(0) }}°"
in the Developer tools Template even?

Did you execute Reload Template Entities after making the change? (Or restart Home Assistant)

If you did, then copy-paste this into the Template Editor and let us know what it reports:

{{ state("sensor.multisensor_bedroom_air_temperature") | float(0) }}

Ok i fixed this but broke the rest of the code :smiley:

Your responses lack details so we are left wondering what you mean when you say “I fixed this” (How? What exactly did you do? Was any of it related to what has been suggested?) and “broke the code” (What other code and how is it broken?).

#     - service: mqtt.publish
 #       data:
 #         topic: "hasp/displ_entrance/command/p[4].b[5].txt"
 #         payload_template: '"  {{ states( sensor.multisensor_bedroom_air_temperature) | float | round(0) }}°"'

this is the code I have, however it broke the rest of the code (it’s very long but simple):

##############################################################################
# Automations for handling climate controls on page 4

automation:
  # Set font and text labels for scene buttons on device connection

  - alias: hasp_displ_entrance_p4alternative_HVACInit
    trigger:
      - platform: state
        entity_id: "binary_sensor.displ_entrance_connected"
        to: "on"
      - platform: homeassistant
        event: start
    action:
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/json"
          payload_template: >-
            ["p[4].b[5].font=7",
            "p[4].b[7].font=7",
            "p[4].b[6].font=8",
            "p[4].b[8].font=8",
            "p[4].b[9].font=7",
            "p[4].b[11].font=7",
            "p[4].b[10].font=7",
            "p[4].b[12].font=7"]
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[7].txt"
          payload_template: '"{{ states("climate.ac_bedroom")|replace("heat_cool","")|replace("fan_only","")|replace("dry","")|replace("cool","")|replace("heat","")|replace("off","")|title }} {{ state_attr("climate.ac_bedroom", "temperature") | int }}°"'
 #     - service: mqtt.publish
 #       data:
 #         topic: "hasp/displ_entrance/command/p[4].b[5].txt"
 #         payload_template: '"  {{ states( sensor.multisensor_bedroom_air_temperature) | float | round(0) }}°"'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[6].txt"
          payload_template: '""'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[8].txt"
          payload_template: '""'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[11].txt"
          payload_template: '" AVG."'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[9].txt"
          payload_template: '" EXT."'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[10].txt"
          payload_template: '"{{ state_attr("sensor.outside_average_temperature", "mean") | float | round(0) }}°"'
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[12].txt"
          payload_template: '"{{ state_attr("sensor.home_average_temerpature", "mean") | float | round(0) }}°"'

  - alias: hasp_displ_entrance_p4alternative_UpdateHVACMode1
    trigger:
      - platform: state
        entity_id: "climate.bedroom"
    action:
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[7].txt"
          payload_template: '"{{ states("climate.ac_bedroom")|replace("heat_cool","")|replace("fan_only","")|replace("dry","")|replace("cool","")|replace("heat","")|replace("off","")|title }} {{ state_attr("climate.ac_bedroom", "temperature") | int }}°"'

  # Catch either of the "mode" buttons, iterate through the list of supported modes, and
  # select the next one in the list or loop back to the beginning if it's the last item
  - alias: hasp_displ_entrance_p4alternative_CycleHVACMode1
    trigger:
      - platform: mqtt
        topic: "hasp/displ_entrance/state/p[4].b[7]"
        payload: "ON"
    action:
      - service: climate.set_hvac_mode
        entity_id: "climate.ac_bedroom"
        data_template:
          hvac_mode: >-
            {% for mode in state_attr("climate.ac_bedroom", "hvac_modes") %}
              {% if mode == states("climate.ac_bedroom") %}
                {% if loop.last %}
                  {{ state_attr("climate.ac_bedroom", "hvac_modes")[0] }}
                {% else %}
                  {{ state_attr("climate.ac_bedroom", "hvac_modes")[loop.index] }}
                {% endif %}
              {% endif %}
            {% endfor %}
  
  # Display current HVAC temp on HASP when updated in Home Assistant
  - alias: hasp_displ_entrance_p4alternative_UpdateHVACTemp1
    trigger:
      platform: state
      entity_id: "climate.ac_bedroom"
    condition:
      condition: template
      value_template: '{{ trigger.from_state and trigger.to_state.attributes.current_temperature != trigger.from_state.attributes.current_temperature }}'
    action:
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[5].txt"
          payload_template: '"  {{ state_attr("climate.ac_bedroom", "current_temperature") | int}}°"'

  # Display HVAC temp set point on HASP when updated in Home Assistant
  - alias: hasp_displ_entrance_p4alternative_UpdateHVACTempHigh1
    trigger:
      platform: state
      entity_id: "climate.ac_bedroom"
    condition:
      condition: template
      value_template: '{{ trigger.from_state and trigger.to_state.attributes.temperature != trigger.from_state.attributes.temperature }}'
    action:
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[7].txt"
          payload_template: '"{{ states("climate.ac_bedroom")|replace("heat_cool","")|replace("fan_only","")|replace("dry","")|replace("cool","")|replace("heat","")|replace("off","")|title }} {{ state_attr("climate.ac_bedroom", "temperature") | int }}°"'

  # Increment HVAC temp set point when p[4].b[6] is pressed
  - alias: hasp_displ_entrance_p4alternative_SetHVACTempPlus1
    trigger:
      - platform: mqtt
        topic: "hasp/displ_entrance/state/p[4].b[6]"
        payload: "ON"
    action:
      - service: climate.set_temperature
        entity_id: "climate.ac_bedroom"
        data_template:
          temperature: '{{ state_attr("climate.ac_bedroom", "temperature") + 1 | int}}'


  # Decrement HVAC temp set point when p[4].b[8] is pressed
  - alias: hasp_displ_entrance_p4alternative_SetHVACTempMinus1
    trigger:
      - platform: mqtt
        topic: "hasp/displ_entrance/state/p[4].b[8]"
        payload: "ON"
    action:
      - service: climate.set_temperature
        entity_id: "climate.ac_bedroom"
        data_template:
          temperature: '{{ state_attr("climate.ac_bedroom", "temperature") - 1 | int}}'

  # Turn off HVAC when p[4].b[5] is pressed
  - alias: hasp_displ_entrance_p4alternative_SetHVACOff1
    trigger:
      - platform: mqtt
        topic: "hasp/displ_entrance/state/p[4].b[5]"
        payload: "ON"
    action:
      - service: climate.set_hvac_mode
        entity_id: "climate.ac_bedroom"
        data_template:
          hvac_mode: "off"

I don’t recall anyone telling you to comment out the service call. You did that without telling anyone thereby making it difficult for anyone to understand how or what “broke your code”.

The lack of details and clear description of what steps you have performed make it very challenging to assist you. I suggest you review the FAQ to understand what is needed to help others help you.

Good luck.

I commented it out to make the rest of the code work. Thanks for your time anyway!

Ok, if you want to try and continue, try this. Copy the below line to the Developer Tool Template and tell me what value shows in the top right hand corner of your screen like I did for one of my sensor entities in point 4. If we go through it with smaller steps then we may understand each other.

"{{ states("sensor.multisensor_bedroom_air_temperature") | float(0) | round(0) }}°"

I see "24°"

I think I found the problem: the code is clashing with the following piece of code that I don’t know how to edit in order to make it update the temperature based on the sensor.multisensor_bedroom_air_temperature:

  # Display current HVAC temp on HASP when updated in Home Assistant
  - alias: hasp_displ_entrance_p4alternative_UpdateHVACTemp1
    trigger:
      platform: state
      entity_id: "climate.ac_bedroom"
    condition:
      condition: template
      value_template: '{{ trigger.from_state and trigger.to_state.attributes.current_temperature != trigger.from_state.attributes.current_temperature }}'
    action:
      - service: mqtt.publish
        data:
          topic: "hasp/displ_entrance/command/p[4].b[5].txt"
          payload_template: '"  {{ state_attr("climate.ac_bedroom", "current_temperature") | int}}°"'

That’s good. Now try this in the developer tools . Make sure you tick box just in front of payload template.

Does the new senor temp show on the haspone screen when you press call service?

Yes it worked!