At a loss to know how to get a yaml sensor template to control a switch

I’m somewhat out of my depth here.

I have two thermometers in different rooms that I would like to control a switch which simply turns the boiler ON or OFF.
If (a) one or other temperature has not met the preset temperature and (b) an overriding control switch is turned on, the switch is activated. So 3 criteria are being assessed.
I have written a simple template in the developer template editor which satisfies the criteria and writes “Turn boiler ON” or “Turn boiler OFF” appropriately.

My question is: How do I get this to produce switch activation? Thanks for any help.

{% if states('sensor.temp_current_office_temperature') and states('sensor.temp_current_kitchen_temperature')| float < 20.0 and is_state('input_boolean.control_switch','on')%} 
    Boiler ON
  {% else %}
    Boiler OFF
{% endif %}

So your IF statement is saying …
Does this exist?
states(‘sensor.temp_current_office_temperature’)
and
Is this less than 20
states(‘sensor.temp_current_kitchen_temperature’)| float < 20.0
and
Is this switch on
is_state(‘input_boolean.control_switch’,‘on’)

Is that what you were after?

Thanks for replying. I obviously haven’t made myself clear.

The template in the developer template editor works perfectly. If either temperature falls below 20C and the switch is on, the message shows as Boiler ON (and the reverse works too). I’m happy with the logic of the template.

But what I’m trying to do is find a way for the statement Boiler ON or OFF to activate / deactivate a separate switch (which I haven’t named for simplicity’s sake).

Hi nbraude,

Leaving stuff out that we don’t know about does not make it easier. Trust me.
Put the whole thing here. Don’t hive us your private URL or anything like that, just the automation you are working.

2 Likes

Ok, sorry if my attempt to simplify has inadvertently made my request for help more complicated!

My home heating setup is a boiler controlled by a hall thermostat with individual zigbee controlled radiator valves (TRV’s). I also have zigbee temperature sensors in several rooms.

After hours of trying I have created a template switch in the configuration file (below) which effectively switches the boiler on and off (by setting the hall temperature thermostat to 23C or 13C). Combining this - with individual zigbee room temperature sensors using generic thermostats (also below) - I can control the temperature in ONE room. I have automations that turn the TRV’s off in the non-heated rooms when single room heating is activated. This works really well and allows me to turn the heating down in unoccupied rooms and heat one occupied room.

My aim now is to be able to heat TWO rooms with the same features. If I combine the two generic thermostats in the two rooms in a single automation, the boiler heating “switch” turns off when the first room has reached the required temp rather than when both have achieved their temperatures. I had intended to use the TRV’s to close down the heating in the room that had first reached required temperature but for the boiler to continue until the second thermostat had reached the required temperature.

My thought was to create the logic for this with an AND statement in a template (which I have done in the editor) and use this to control the boiler “switch”. That was where I got stuck. Perhaps there is a way of combining two generic thermostats so that both have to reach set temperatures before turning off heating (i.e. an AND rather than an OR relationship)?

switch:
  - platform: template
    switches:
      boiler_on_off:
        value_template: "{{ is_state_attr('climate.your_thermostat', 'temperature', 23) }}"
        turn_on:
          service: climate.set_temperature
          target:
            entity_id: climate.your_thermostat
          data:
            temperature: 23
        turn_off:
          service: climate.set_temperature
          target:
            entity_id: climate.your_thermostat
          data: 
            temperature: 13

  - platform: generic_thermostat
    name: Office
    heater: switch.boiler_on_off
    target_sensor: sensor.temp_current_office_temperature
    min_temp: 18
    max_temp: 24
    ac_mode: false
    target_temp: 21.3
    cold_tolerance: 0.2
    hot_tolerance: 0
    min_cycle_duration:
      seconds: 5
    keep_alive:
      minutes: 3
    initial_hvac_mode: "off"
    precision: 0.1

try putting this in your value_template sectioon of the switch code:

value_template: "{{ states('sensor.temp_current_office_temperature') | | float < 20.0 and states('sensor.temp_current_kitchen_temperature') | float < 20.0 and is_state('input_boolean.control_switch','on') }}"

Thanks. That’s really helpful and solves the problem I presented. Like so many things afterwards, it seems obvious now.

I’ve combined it with the original boiler control data using an OR function. This seems to work, although I haven’t tested all possibilities. I presume AND and OR can be mixed in the same statement?

value_template: "{{states('sensor.temp_current_office_temperature') | float < 20.0 and states('sensor.temp_current_kitchen_temperature')| float < 20.0 and is_state('input_boolean.dummy_switch','on')  or is_state_attr('climate.your_thermostat', 'temperature', 23)}}"

Thanks very much once more.

Yes ‘and’ and ‘or’ can be mixed but beware of the logic results and order of operations.

‘()’ has precedence over ‘not’ has precedence over ‘and’ has precedence over ‘or’.

It’s hard to tell which things you want to ‘or’ together from the template but based on the order of operations it looks like you will get this:

( 
  states('sensor.temp_current_office_temperature') | float < 20.0 
  and 
  states('sensor.temp_current_kitchen_temperature')| float < 20.0 
  and 
  is_state('input_boolean.dummy_switch','on')
)

or 

is_state_attr('climate.your_thermostat', 'temperature', 23)

If that’s what you want then your template is fine.

if it’s not then you may need to force order of operations by including ‘()’

so let’s say you want to ‘and’ the first two and then ‘and’ that result with an ‘or’ of the last two.

logically it would look like this:

( 
  states('sensor.temp_current_office_temperature') | float < 20.0 
  and 
  states('sensor.temp_current_kitchen_temperature')| float < 20.0 
)
and 
(  
   is_state('input_boolean.dummy_switch','on')
   or 
   is_state_attr('climate.your_thermostat', 'temperature', 23)
)

the new template would look like this:

value_template: "{{ states('sensor.temp_current_office_temperature') | float < 20.0 and states('sensor.temp_current_kitchen_temperature')| float < 20.0 and ( is_state('input_boolean.dummy_switch','on')  or is_state_attr('climate.your_thermostat', 'temperature', 23) ) }}"

So the template again depends on the logic you want in the end.

I like to force order of operations using parenthesis so there is no ambiguity in my templates so I would probably even put ‘()’ around the first two operands:

value_template: "{{ ( states('sensor.temp_current_office_temperature') | float < 20.0 and states('sensor.temp_current_kitchen_temperature')| float < 20.0 and is_state('input_boolean.dummy_switch','on') )  or is_state_attr('climate.your_thermostat', 'temperature', 23) }}"

or if using the second template:

value_template: "{{ ( states('sensor.temp_current_office_temperature') | float < 20.0 and states('sensor.temp_current_kitchen_temperature')| float < 20.0 ) and ( is_state('input_boolean.dummy_switch','on')  or is_state_attr('climate.your_thermostat', 'temperature', 23) ) }}"

but that might just be me. :wink:

That’s fantastic - answers all my queries. Thanks so much for your helpful advice.

1 Like