[SOLVED]Template switch that can be on/off/unavailable?

Hey guys,
I ve been trying to create a template switch that would also show me if the actual switch is unavailable apart from on/off.

Something like:

switch:
  - platform: template
    switches:
      template_switch_name:
        value_template: >-
          {% if is_state('switch.mqtt_switch', 'on') %}
          'on'
          {% elif is_state('switch.mqtt_switch', 'off') %}
          'off'
          {% elif is_state('switch.mqtt_switch', 'unavailable') %}
          'unavailable'
          {% endif %}
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.mqtt_switch
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.mqtt_switch

But the above does not work, probably because i have formatted something wrong :frowning:

Is this possible?
I would also like to apply this to template Light entities (turn my mqtt switches to lights but keep the unavailable option visible)

Thanks in advance.

edit: had some formatting error in my code, it works now :smiley:

Don’t you find the value_template to be somewhat redundant?

If on then on else if off then off else if …

Why test the current state if all you plan to do is report the current state?

switch:
  - platform: template
    switches:
      template_switch_name:
        value_template: >-
          {% if is_state('switch.mqtt_switch', 'on') %}
          'on'
          {% elif is_state('switch.mqtt_switch', 'off') %}
          'off'
          {% else %}
          'unavailable'
          {% endif %}
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.mqtt_switch
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.mqtt_switch

You mean like this?

Agreed on the new syntax, plus found the mistake in my original code, had some extra " that would mess everything up :slight_smile:

It’s working now

I may be wrong but I think this template does the same thing. It simply reports the current state of the entity:

        value_template: "{{ states('switch.mqtt_switch') }}"

I don’t have a switch.mqtt_switch defined on my system so when I run the template, in the Template Editor, it reports unknown.

Screenshot%20from%202019-04-06%2010-36-27

1 Like

Mine works as a template:


But it gives me an error as a switch:

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/packages/garden/garden_extra_template_switches_renaming.yaml", line 5, column 9
expected <block end>, but found '<block mapping start>'
  in "/config/packages/garden/garden_extra_template_switches_renaming.yaml", line 6, column 11
switch:
  - platform: template
    switches:
      zone1_valve:
        value_template: "{{ states('switch.xxx') }}"
          service: switch.turn_on
          data:
            entity_id: switch.xxx
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.xxx

Any ideas?

Shouldn’t it have the following line before the first service call?

        turn_on:

You’re right, i’ve spent too much time in front of the pc today, i should take a break :smiley:

Hi!
I am in the exact same situation where I would need to get the status of a Sonoff Basic including unavailable for when there is no power to that Sonoff device. I’ve read the thread but being new to all this don’t understand what needs to be added where. Could someone kindly indicate what needs to be done?

switch:
  - platform: template
    switches:
      new_name_switch:
        value_template: "{{ states('switch.xxx') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.xxx
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.xxx

This should give you on, off and unavailable.

You can also dig deeper by using this

ok. So I added that into my configuration.yaml file, restarted and while I do have a new switch I can add it stays off all the time and when I click on it, it reverts to off after a few seconds. I don’t see any unavailable neither. What am I missing?

Thank you

Sounds like you have not entered the correct switch, did you replace switch.xxx with the correct entity?
Check in Developer tools -> States for a list of the entities available

That’s the behavior you get when value_template is incorrect.

value_template is used to report the template switch’s state. If you instruct a template switch to turn on and its state reverts to off, it means value_template failed to evaluate to true.

Post your template switch’s configuration so we can help you fix the problem.

Thanks for the help. I’ve narrowed down the issue and somehow fix the errors I was getting however {{ states(‘switch.xxx’) }} gives me “on” even when the Sonoff is unplugged (well it was on before - when off before unplugging it was off when unplugged).
I’ve noticed however that Wifi signal becomes “unknown” so I might use this instead to tell me when the Sonoff is unavailble.
If you have any idea of what I’m missing please let me know.