Dummy switch activated when two temperatures differ

Hey there, i am hoping to find some help.

I am looking for a template switch that changes state when two temperatures differ, so that i can realize a relay state. I have a floor heating system controlled by room thermostats and electric flow valves.

I have the room thermostats flashed with a custom firmware, transmitting status changes via MQTT to HA. Sadly, the thermostats are missing the relay state reading unless modding the hardware, so i thought i am able to simulate the relay state (relay opens the valves when temperature is lower than the setpoint). I am able to read the values for the current room temperature and the setpoint temperature.

eg: sensor.temp_living_now lower than sensor.temp_living_setpoint should turn the dummy switch switch.relay_floor_heater to on.

Can someone point me to the right direction please?
Is it possible to do that without an automation as i have six rooms and i don’t want to have six automations for that?

Thanks for your help - Andre

template binary sensor

Thanks petro, i already have read the template documentation, but i am not sure if i need to create the template sensor first or if i can do everything together in one sensor template definition.

If its that what i think, it should be something like that:

  - platform: template
        value_template: "{{ ('sensor.temp_living_now') < ('sensor.temp_living_setpoint) }}"
          service: switch.turn_on
          service: switch.turn_off

but then i am missing the target, which is not created yet?

Your syntax is wrong, you aren’t grabbing the state. You need to use the states method. Second, all states are strings, so if you want to compare the numbers, you have to convert the numbers.

You can do everything in 1

Ha? I don’t understand that - why do i need to use states?
Just want to compare two numbers…

Sadly i am not very good at programming…
Do you mind giving me an example config?

There are examples on how to get states out of the your sensor in the docs. Your sensor’s state is the number but represented as text, not a number that can be used with math. Right now, you just wrote 'sensor.xyz' which is the entity_id for the sensor. You need to get the state for that sensor, just writing out the will not return it’s state. Have you looked at the template docs? It explains how to get states or attributes from a sensor.


I suggest you sit with the docs, trying examples in the docs I linked inside the template editor. (developer tools → template). Keep in mind you have to convert the values from text (string) to a number (float) using the float filter or method.

hmm… okay i will see - thanks for pointing me the way, altough i know i will get lost there…

it’s really simple, I believe you’re putting it on a pedestal. The code you posted here is like 12 letters away from being correct.

All you need to know is how to actually get the states which I directly linked to. And I know you haven’t clicked on said link because I can see that.

EDIT: But you want to create a binary_sensor, not a switch.

well, i have read it - seriously - but as stated my knowledge is too small.
I have tried it with that code, but am not confident that is correct?

-platform: template
      - name: "GF Valve State"
        state: >
          {% if states('climate.rt_wohnzimmer.current_temperature')|float < states('climate.rt_wohnzimmer.temperature')|float %}
          {% else %}
          {% endif %}

Is that what you mean?

that’s very close, accept binary_sensors only accept a state of ‘on’ or ‘off’. You can also omit ‘on’ or ‘off’ in your template because if the resolution of the template returns true or false, it will translate it to on or off. If you specify a device_class, that will further translate your state when in the UI, but under the hood it will still be treated as on or off.

Here are fixes made:

  1. You’re mixing 2 template formats. Legacy and new. Use one or the other.
  2. ‘on’ or ‘off’ or true or false should be the output of the template for binary sensors.
  3. You’re comparing attributes. states will get you the primary state on a sensor. state_attr will get you a secondary attribute on a sensor. current_temperature is an attribute, not the main state.
  4. Attributes can be any type. number (float), text (str), true/false (bool), etc. You don’t need to convert them.
  - binary_sensor:
      - name: "GF Valve State"
        device_class: opening
        state: >
          {{ state_attr('climate.rt_wohnzimmer', 'current_temperature') < states('climate.rt_wohnzimmer', 'temperature') }}
1 Like

THANK YOU! That worked.
Glad you helped me out.