Switch that holds its own state

Hi there,

I need a switch that has its own state. I tried the following:

platform: template
switches:
  testswitch:
  value_template: "{{ is_state_attr('switch.testswitch', 'sensor_state', 'on') }}"
  friendly_name: 'Test'
    turn_on:
      service: switch.turn_on
      entity_id: 
        -  switch.testswitch
  turn_off:
    service: switch.turn_off
      entity_id: 
        -  switch.testswitch

But after a few secconds the switch turns off again. Now I have the idea to create a dummy entity to save the state. But I don’t know how and if this is the correct way.

Any ideas? Thanks in advance!

Do you need the switch to do anything? If not, try the input boolean

1 Like

Looks like you missed some indentation after testswitch: and a few other small formatting things id change:

platform: template
switches:
  testswitch:
    value_template: "{{ is_state_attr('switch.testswitch', 'sensor_state', 'on') }}"
    friendly_name: 'Test'
    turn_on:
      service: switch.turn_on
      entity_id: switch.testswitch
    turn_off:
      service: switch.turn_off
      entity_id: switch.testswitch

Seems insignificant but it may fix your issue.

Sorry to revive this thread but I am looking for the exact same thing but can’t figure it out on my own.

So I recently asked for help about activating a switch that runs a script (post) and it runs perfectly but the problem is this switch reads the state from the script but the script itself never reports its state so when I turn it on, it will immediately revert to OFF after it runs.

Here’s the switch in question:

- platform: template
  switches:
    lasko:
      value_template: "{{ is_state('script.fan_on', 'on')}}"
      turn_on:
        service: script.turn_on
        entity_id: script.fan_on
      turn_off:
        service: script.turn_on
        entity_id: script.fan_off

Anyone could be so kind to post an example of what I need to make this switch report and keep its state? I’ve never used an input boolean before as the answer in this topic suggests so its all new to me.

Thanks!

@junior466

The reason this switch doesn’t keep its state is it’s meant to be a toggle switch. It displays on while the script is running then turns off. Since most fans don’t have a discrete input for power on and power off, just power toggle, this was designed to act as such.

Input_booleans are essentially the same thing as switches, just manual switches. The other method I directed you to, basically figures the state of the fan by always turning the speed down to the minimum and then turning the power toggle off. It manages the state better but that’s if you “throw the remote away” and just use HA.

Is there a reason you can’t use input_booleans with HA_dashboard? If you can’t, then you could always add a template switch which follows the on/off of the input_boolean

1 Like

@Jer78 I understand it now. I can use input_booleans in HADashboard but the post you linked me is way above my head as I am just starting out with HA. I would prefer the suggestion in your last paragraph as I’m already half way there with the switch and script.

Could you provide an example how I could achieve that and any modifications I need to make to my existing code?

Thank you for your help so far.

@junior466 My suggestion was only if HADashboard couldn’t use input_booleans and you had the more complicated code working but just needed to use a switch. But since that’s not the case, here’s the easiest way to accomplish what you’re asking. You’d use an input_boolean and automation like this:

input_boolean:
  fan:
    name: Fan

And put this under your automation section:

  - alias: 'Fan'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_boolean.fan
    action:
      - service: remote.send_command
        entity_id: remote.living_room
        data:
          device: "YOUR_HARMONY_DEVICE_ID"
          command: "PowerToggle"
2 Likes

@Jer78 It works perfectly! I really appreciate you taking the time to provide all these examples!

Thank you!

1 Like

@junior466 You’re welcome! :slight_smile:

@Jer78 Figured it out! Sorry to bother. :slight_smile:

@junior466 No worries. Glad you got it figured out.

Hi sorry to bring this back -Im using tuya switches and they’ve been less that ideal since the api change.

The switches are not remembering their state similar to that of the others mentioned previously .

Ive grouped the switches together using a Template switch but still the same issue of remembering the state. Ive put together the code you’ve mentioned was curious if this would work or if im missing something to remember the off state.

  - alias: 'TV Lights'
    initial_state: off
    trigger:
      platform: state
      entity_id: input_boolean.tv_lights
    action:
      - service: switch.turn_on
        entity_id: switch.tv_lights