Template so that input_bool and shelly switch mimic each other?

What sort of template setup do I need to make an input_bool follow the state of a shelly switch and vice versa?

EG: If I turn on/off the bool the shelly switch turns on/off. But if I turn on/off the switch via the shelly cloud, the bool must also reflect the on/off state of the switch

The below is easy enough to do as a switch but how can I do it with a bool?

switch:
  - platform: template
    switches:
      sw1:
        value_template: "{{ is_state( 'switch.shelly_8' , 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.switch.shelly_8
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.switch.shelly_8

Is it possible to do this in one template? I DO not want to do this in an automation (lots of bools)

Why do you want to duplicate a switch like this?

I have my reasons…

I should be using a template switch instead of a bool right???

Or maybe you are just missing a piece of information that would mean you don’t actually have to do this. See https://xyproblem.info/

answered my own question.

Why keep it a secret? Someone else with a similar question might be interested to know how you answered it.

Simple version - I changed all my bools to template switches.

OK further to this and I wanted to share one of the nuanced issues with HA…

Justification:
The reason for doing this is that HA IS TERRIBLE when it comes to replacing devices seamlessly when they become faulty OR a new technology comes along and you replace that device with different tech. If you replace that device with something different, you have to edit, lovelace, custom cards, scripts and automations and relace the old entity with the new one. Additionally it becomes problematic if you have “other” temlplates that reference that “real device” entity and do math on that entity to form a new one, because now you have to hunt down that math template and change it.

Im sure that anyone with a LARGE system with lots of templates has seen this and then wanted to just kill themselves:


"Do you also want to rename the entity IDs of your entities?

This will not change any configuration (like automations, scripts, scenes, dashboards) that is currently using these entities! You will have to update them yourself to use the new entity IDs!"


Therefore, for the last 5 years Ive taken a leaf out of @frenck book from many years ago (I dont rememeber which ‘choo choo’ video he mentioned it) and replaced all my physical entities with template entities that mimic the real devices.

So therefore it is easier to make a device template that represents a real device

EG:
sensor.shelly1_9899229989898_temperature = sensor.outside_temp_template

In Automations, scripts, lovelace, other entity templates(non-negotiable), climate controllers, blueprints, custom componenents, custom, cards (custom-button card templates also non-negotiable), node-red (I can keep going) etc you ONLY NEED to reference, sensor.outside_temp and youre golden. in EVERY PART of your HA instance.

.

  • HERES THE KICKER!!!

If you HAVE to change the physical device from a shelly to a xiaomi/modbus/someobscurething in the future, all you need to do is change 1 line in your template and EVERYTHING WORKS!!! Automations, Templates, Custom Cards, custom templating within custom cards, scripts, blueprints etc etc etc because everything in your instance references:

sensor.outside_temp_template

We have always had templates - and templates create an easy way around this for larger installs that need reliability, rather than hours of hobbyist tinkering. Templates are not foolproof, but for the lack of basic global variables as entities within HA (perhaps in V2026.12.5) , in the meantime they are a good way of creating global entities.

NOTE: when I say"larger installs" - as a reference - the biggest of my lovelace dashboards is 38k lines long (I have 6 of them) and my automations.yaml is close to 30k and I use template cards to reduce the size of my lovelace code. there is also a real reason why my dashboards are so big, but that is a discussion for another post to do with lack of user admin, caching and preloading.

As long as your automations don’t reference device based triggers, can’t you just make the (in this case) switch entity name of the new device, match that of the old one ?

doesnt work with custom cards with something as complex as this: (anything that references fv1t)

type: custom:vertical-stack-in-card
style: |
  ha-card {
  border: solid 1px gray; 
  box-shadow: 5px 10px 10px  rgba(0,0,0,.4);
  }                            
cards:
  - type: markdown
    content: <h3>BATCH NO:&nbsp;{{states("input_text.fv1_batchno") }} </h3>
    style: |
      ha-card {
      border: none !important;
      background: none !important; 
      box-shadow: none !important;
      margin: 2px 0px -20px 10px  !important;  
      }
  - type: custom:button-card
    show_entity_picture: true
    show_name: true
    tap_action:
      action: navigate
      navigation_path: /dashboard-tablet1/fv1/
    name: FV1
    hold_action:
      action: none
    entity: switch.fv1t_solenoid
    state:
      - entity_picture: /local/icons/cyan/fermenter_active.png
        value: 'on'
      - entity_picture: /local/icons/cyan/fermenter_grey.png
        value: 'off'
    styles:
      entity_picture:
        - width: 100%
        - height: 100%
        - object-fit: contain
      card:
        - width: 350px
        - height: 220px
        - background-color: transparent
        - border-radius: 1px
        - padding: 5%
        - color: ivory
        - font-size: 18px
        - text-shadow: 0px 0px 5px black
        - text-transform: capitalize
      grid:
        - grid-template-areas: '"f9 i f11" "f10 i f12" "f1 i f2" "f3 i f4" "f5 i f6" "f7 n f8"'
        - grid-template-columns: 50px 2fr 50px
        - grid-template-rows: 50px 1fr 1fr 1fr 1fr 15px
      name:
        - font-size: 23px
        - color: white
        - align-self: start
        - justify-self: middle
      img_cell:
        - justify-content: middle
        - align-items: end
        - margin: none
      custom_fields:
        f1:
          - align-self: end
          - justify-self: start
        f2:
          - align-self: end
          - justify-self: right
        f3:
          - align-self: start
          - justify-self: start
          - font-size: 23px
        f4:
          - align-self: start
          - justify-self: right
          - font-size: 23px
          - color: >-
              [[[ if (states["input_boolean.fv1_cleaning"].state == 'on') return
              "crimson";
                  if (states["input_boolean.fv1_cleaning"].state == 'off') return "grey";
                  if (states["input_boolean.fv1_auto"].state == 'off') return "grey"; 
                  if (states["input_boolean.fv1_auto"].state == 'on') return "aquamarine";

                  ]]]
        f5:
          - align-self: end
          - justify-self: start
        f6:
          - align-self: end
          - justify-self: right
        f7:
          - align-self: start
          - justify-self: start
          - font-size: 23px
          - color: >-

              [[[ if (states["input_boolean.fv1_auto"].state == 'off') return
              "grey"; 
                  if (states["sensor.fv1t_temp"].state <= 10) return "dodgerblue"; 
                  if (states["sensor.fv1t_temp"].state <= 16) return "blue";
                  if (states["sensor.fv1t_temp"].state <= 22) return "lime";
                  if (states["sensor.fv1t_temp"].state <= 30) return "orange"; 
                  if (states["sensor.fv1t_temp"].state <= 40) return "red"; 
                  ]]]              
        f8:
          - align-self: start
          - justify-self: right
          - font-size: 23px
          - color: >-
              [[[ if ( states['climate.fv1'].attributes["hvac_action"] ==
              'idle') return "royalblue"; 
                  if ( states['climate.fv1'].attributes["hvac_action"] == 'cooling') return "lime";
                  if ( states['climate.fv1'].attributes["hvac_action"] == 'off') return "grey"; ]]]
        f9:
          - align-self: end
          - justify-self: start
        f10:
          - align-self: start
          - justify-self: start
          - font-size: 23px
        f11:
          - align-self: end
          - justify-self: right
        f12:
          - align-self: start
          - justify-self: right
          - font-size: 23px
          - color: >-
              [[[ if (states["sensor.fv1t_gravity"].state <= 1.005) return
              "red"; 
                  if (states["sensor.fv1t_gravity"].state <= 1.010) return "green";
                  if (states["sensor.fv1t_gravity"].state <= 1.015) return "skyblue";
                  if (states["sensor.fv1t_gravity"].state > 1.015) return "white";              
                  ]]]                                  
    custom_fields:
      f1: 'Set Temp:'
      f2: 'Mode:'
      f3: |
        [[[ return `${states['sensor.fv1t_set_temp'].state}` ]]]
      f4: |
        [[[ if (states["input_boolean.fv1_auto"].state == 'on') return `Auto`;
            if (states["input_boolean.fv1_cleaning"].state == 'on') return `CLEAN`;
              else;
            return 'off'; ]]]
      f5: 'Tank Temp:'
      f6: 'State:'
      f7: |
        [[[ return `${states['sensor.fv1t_temp'].state}` ]]]   
      f8: |
        [[[ return `${states['climate.fv1'].attributes["hvac_action"]}` ]]]
      f9: 'Press: (bar)'
      f10: |
        [[[ return `${states['sensor.fv1t_pressure'].state}` ]]]
      f11: 'Grav: (SG)'
      f12: |
        [[[ return `${states['sensor.fv1t_gravity'].state}` ]]]

ESPECIALLY if you have lots of them and dont have hobbyist time

change a physical device:

"Do you also want to rename the entity IDs of your entities?

This will not change any configuration (like automations, scripts, scenes, dashboards) that is currently using these entities! You will have to update them yourself to use the new entity IDs!"

@onepointbrewing

Hi…

I’m still not sure I’m getting the issue.

if I (say) rename and old device and prefix _donotuse onto the device name, then say ‘yes’ to rename entities, then each entity connected to that device will look something like:

somedomain.originalentitynamebit_donotuse

As you have mentioned, this does not change the referenced entity names in automations/dashboards etc, thus breaking them… In this case, that is a good thing.

Now I add a new device, and deliberately rename its entities to match those that the old device had.
Doesn’t everything start working again ?

yes

It is. You could vote for this: Add a method of replacing devices

And read this advice: What happens if I have to replace a device - #2 by tom_l

have done @tom_l

In the meantime, my poor excuse for system wide replacements for entities that ‘may’ change - templates

If you follow the advice in the second link you don’t need to do all that. It makes it very easy to replace a device. Just edit the new entity ids to match the old ones when replacing the device. In one place. Takes no time at all.