Set an MQTT-Entity with MQTT-publish in an automation as difference of another MQTT-Entity and a fixed value (both temperatures, float)

Hi to all,

I’m just starting with YAML, so I need some help. I have asked AI several times, but nothing was working, I also searched in the Internet - nothing helped.

I’m struggling with the following:

  • controlling of our heating system
  • I have a ‘Benthe.temperature.target’ as MQTT-Entity
  • I have a ‘Benthe.temperature.target.old’ as MQTT-Entity
  • I want to store the ‘Benthe.temperature.target’ to ‘Benthe.temperature.target.old’, so I can restore that in the morning to the former target. This is already working!
  • over the night I want to reduce this ‘Benthe.temperature.target’ by for example 3.0 °C
  • that should happen in an automation every evening at time 22:00
  • the automation is configured and running
  • but I cannot subtract the fixed value from the Entity :frowning:
  • I have tried a lot, but didn’t succeded
  • first I tried to do the subtraction in the YAML of the Automation/MQTT-Publish in a lot of different ways
  • Now I have a template-sensor for the arithmetic - doesn’t word

Can I do that directly in the automation in the YAML of MQTT-Publish?
Or do I need the template sensor or anything else?

Here is the actual code from automation.yaml:

  • id: ‘1764426127712’
    alias: BentheSetTarget
    description: ‘’
    triggers:
    • trigger: time
      at: ‘14:01:00’
      weekday:
      • mon
      • tue
      • wed
      • thu
      • fri
      • sat
      • sun
        conditions:
        actions:
    • action: mqtt.publish
      metadata: {}
      data:
      qos: ‘1’
      topic: Benthe.temperature.target.old
      payload: ‘{{ states(’‘sensor.benthe_temperature_target’‘) }}’
    • action: mqtt.publish
      metadata: {}
      data:
      qos: 0
      topic: Benthe.temperatur.target
      payload: ‘{{ states(’‘sensor.Benthe.temperature.target.night’‘) }}’
      mode: single

an this is my template sensor from configuration.yaml

template:
- sensor:
- name: Benthe.temperatur.target.night
unit_of_measurement: “°C”
state: >
{% set sensor_value = states(‘Benthe.temperatur.target’) | float(0) %}
{{ sensor_value - 3.0 }}

Hope You can help me
Holger

Hello Holger Ihle,

Thanks for coming here and asking a question.
Would you be so kind as to adjusting the format of your code so that we can read it properly & check the YAML spacing, etc. Editing your original is the preferred way. It is very hard for us to tell what is what when the text formatter jumbles everything like that.
You can use the </> button like this… How to format your code in forum posts
OR… Here is an example of how to fix formatting from the site FAQ Page.
How to help us help you - or How to ask a good question.

Hallo Sir_Goodenough,

is it ok to post screenshots?

I’m working on a formatted reply …

next try :slight_smile:

automation.yaml:
----------------
- id: '1764426127712'
  alias: BentheSetTarget
  description: ''
  triggers:
  - trigger: time
    at: '14:01:00'
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    - sat
    - sun
  conditions: []
  actions:
  - action: mqtt.publish
    metadata: {}
    data:
      qos: '1'
      topic: Benthe.temperature.target.old
      payload: '{{ states(''sensor.benthe_temperature_target'') }}'
  - action: mqtt.publish
    metadata: {}
    data:
      qos: 0
      topic: Benthe.temperatur.target
      payload: '{{ states(''sensor.Benthe.temperature.target.night'') }}'
  mode: single
  
  
configuration.yaml:
------------------- 
  template:
    - sensor:
        - name: Benthe.temperatur.target.night 
          unit_of_measurement: "°C"
          state: >
            {% set sensor_value = states('Benthe.temperatur.target') | float(0) %}
            {{ sensor_value - 3.0 }}

That is not an entity. it is an MQTT topic.
Look up in the docs how to extract the value of a topic using MQTT.

Hello Sir_Goodenough,
thx for Your reply. Since three days I’m trying to solve my ‘problem’. I have read in the docs, I searched by Google, I asked Copilot and ChatGPT - I have tried at least 20 different ways and I’m a little bit on the edge. So I hoped to get some serious help.
Can You pls give advice?
best regards and thank You in advance,
Holger

I will simplyfy my question:

  • I want to set a MQTT-Topic to it’s actual value - 3.0
  • I’m using the UI of Automation in HA
  • I’m using the MQTT-Publish Function

As far as I understood, I can do this directly (without a template sensor) and it should look like this

{{ states(''sensor.Benthe.temperature.target'') - 3.0 }}

That is ‘sensor.Benthe.temperature.target’ the temperature value MQTT topic and I want to subtract 3.0 fixed value. I always get errors when saving the settings or errors in activities. Always complaining that this are incompatible data types.

How do I have to subtract the fixed-value from the MQTT-topic?

Holger

That will be a sensor to get your value out of MQTT.

Is there some reason you are storing stuff in MQTT? Do you use these values outside of this automation? Otherwise just use sensors to store stuff in works without the MQTT complication.

Hello Hello Sir_Goodenough,

thanks for Your reply!

And - yes, there is a reason to store the temperature-values in MQTT. My thermostates in the rooms are about 10 Pi-Picos with display for actual-temperature, target-temperature and humidity and they have buttons for “target +” and “target -” to set an new value. Also they can retrieve data from MQTT. I can also set the target for a room in HA. Also there are 2 more Pi-Picos retrieving several more data from the house, all are stored in MQTT. I have programmed the Picos in MicroPython and they are working just fine.

I will now try to fix my problem with an MQTT-Sensor and will report anyhow.

Thx, Holger

1 Like

My MQTT-Topics are already MQTT-Sensors:

mqtt:
    sensor:
        - name: Benthe.temperature.target
          state_topic: "Benthe.temperature.target"
          unit_of_measurement: '°C'
        - name: Benthe.temperature.target.old
          state_topic: "Benthe.temperature.target.old"
          unit_of_measurement: '°C'

I can retrieve them from MQTT, I can write them to MQTT. That is not my problem.

My problem is the calculation with these values.

  • there is a value for target-temperatur in “Benthe.temperature.target”
  • I want to subtract 3.0 (°C) from this value and restore that as new value in the same topic
  • And I want to do this in an automation with MQTT-publish

So, I still don’t understand how to solve my problem :frowning:

Thx, Holger