Intelligent Switch Template

Can we have a switch template that can trigger the switch. This would allow things like input selects to directly activate a switch without an automation. Couple switches together . Currently switch templates can take an initial value from another entity, but are not triggered by it.

I know there would be issues with cascades and loops , but a threshold option could stop those ? .

This would eliminate lots of small automation that I currently use to sync input selects to switches, and would keep the logic linked to the switch.

if you build the input_boolean into the switch template & turn_on/turn_off services, you don’t need an automation.

But this begs the question, why are you using an input_boolean to turn on or off a switch when you can just use the switch?

And as for input_select, if you build it into the switch template, you don’t even need the input_booleans. Maybe you should post your use case and I can help you optimize it?

I’m not explaining very well.

Let me give a simple problem . Hopefully you can see what I’m trying to say.

We have a dumb heater on a simple smart switch. Via Home assistant we can add a thermostat sensor.
By creating a simple automation we can get the heater to turn on and off when the temperature hits a set temperature. We now have a smart heater.

We can add the switch to Lovelace and we now have an interface. Brilliant.

Then comes the complications. If the user switches the heater off the automation may switch it back on again. So we need some rules in the automation or turn the automation off.
However we solve the problem we need some additional logic , almost certainly some virtual switches.

If we can tie all the logic to the virtual switch, it simplify the problem and reduces the number of additional components to solve the problem.

For instance if we turn off the automation for two hours when the switch turns off

To do this currently we need :-

switch.heater
input_boolean.heater_stop
sensor.temp
automation.heater
automation.heater_check

alias: Smart Heater
description: Turn on  heater < 19 degrees > 19 turn off
trigger:
  entity_id: sensor.temp
  platform: state
condition: 
  - condition: state
    entity_id: input_boolean.heater_stop
    state: 'off'
action:
  - service_template: >
      {% if states('sensor.temp') > 18 %}
	    switch.turn_off
	  {% else %}
	  	switch.turn_on
	  {% endif %}
	data:
	  entity_id: switch.heater
  - service_template: >
      {% if states('sensor.temp') > '18' %}
	    switch.turn_on
	data:
	  entity_id: input_boolean.heater_stop
alias: Smart Heater check
description: is heater check on
trigger:
  entity_id: input_boolean.heater_stop
  platform: state
  from: 'off'
  to: 'on'
delay: '02:00'
action:
  - service: switch_turn_off
    data:
	  entity_id: input_boolean.heater_stop

The above code is crude (if it works at all ) and the logic is difficult , but you get the idea.
I’m sure there are better way to code it. The point is there lots of components .

my idea is this, tie all the logic to the switch

switch:
  - platform: template
    switches:
      heat:
        value_template: "{{ is_state('switch.heater', 'on') }}"
        trigger_template: >
         {% if states('sensor.temp') > 18 %}
           'off'
         (% else %}
            'on'
         {% endif %}
        trigger_check: 00:05	
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.heater
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.heater
		  service: trigger_check
		  data:
		  	entity_id switch.heat
			check: 02:00

Basically we move the automation into the entity

You can use this switch in Lovelace, present it to Alexa and Google. If you don’t need it anymore,
when you delete it , all the logic gets deleted as well.

Not sure if the above completely breaks the Home assistant engine, or more likely my logic is faulty or there already way of doing this

Have you considered use the Generic Thermostat integration?

Minimum configuration would be:

climate:
  - platform: generic_thermostat
    name: Heater
    heater: switch.heater
    target_sensor: sensor.temp

It was just an example to illustrate the idea of an intelligent switch. The point being it would simplify scripting especially when timers are concerned , as the timer would be set against the entity not the automation.

I had not considered ( or even knew about !) the climate integration, just shows the power and breadth of HA. I should have picked a better example :slight_smile: