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 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:
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.
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.
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:
You’re mixing 2 template formats. Legacy and new. Use one or the other.
‘on’ or ‘off’ or true or false should be the output of the template for binary sensors.
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.
Attributes can be any type. number (float), text (str), true/false (bool), etc. You don’t need to convert them.