Switch from attribute state

Hi,
how can I create a switch from attribute state
I have a shelly 1 with a read swich on my garage door.

if the garage door is open, the status is (true)
if it is closed it is (false)

Thank you.

You mean a sensor that shows the state? If so, use the template binary sensor.

Or use the template cover to combine the switch and attribute into one garage door object.

Your value template will be:

value_template: "{{ is_state_attr('switch.shelly_shsw_1_ba2c28', 'switch', 'true') }}"
1 Like

Hi
How can I achieve this?
the examples are not with the attributes

By substituting your switch and the template I wrote above.

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        value_template: "{{ is_state_attr('switch.shelly_shsw_1_ba2c28', 'switch', 'true') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_ba2c28
        close_cover:
          service: switch.turn_off
          data:
            entity_id: switch.shelly_shsw_1_ba2c28

You have not actually told us how the shelly operates the garage door though, so I’m guessing that bit.

it’s like a button.
press once to open and press again to stop or close.
The button goes off after 0.5 seconds.


Then I think this is what you need:

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        value_template: "{{ is_state_attr('switch.shelly_shsw_1_ba2c28', 'switch', 'true') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_ba2c28
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_ba2c28

If “pressing the button” while moving stops the door you might also be able to add:

        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_ba2c28

thank you very much.

Hi tom
my configuration somehow doesn’t work properly.

  • platform: template ########################################################################### Garage
    covers:
    garage_door:
    device_class: garage
    friendly_name: “Garage Door”
    value_template: “{{ is_state_attr(‘switch.shelly_shsw_1_ba2c28’, ‘switch’, ‘true’) }}”
    open_cover:
    service: switch.turn_on
    data:
    entity_id: switch.shelly_shsw_1_ba2c28
    close_cover:
    service: switch.turn_off
    data:
    entity_id: switch.shelly_shsw_1_ba2c28
    icon_template: >-
    {% if is_state(‘sensor.garage_state’, ‘True’) %}
    mdi:garage-open-variant
    {% else %}
    mdi:garage-variant
    {% endif %}


image

  1. the status when closing or opening does not change.

  2. The up and down arrows don’t either.
    I created another sensor for the state because the (if is state_attr) for the icon_template doesn’t work, or I didn’t do it correctly.

  3. the status when closing or opening does not change.

  4. The up and down arrows don’t either.
    I created another sensor for the state because the (if is state_attr) for the icon_template doesn’t work, or I didn’t do it correctly.

can you help me?

  1. Can you please screen capture the shelly attributes when the shelly switch is on. Also when the door is open what does this produce in the developer tools template editor:
{{ is_state_attr('switch.shelly_shsw_1_ba2c28', 'switch', 'true') }}
  1. to 4. this is because of 1.

Also please read point 11 here and edit your post accordingly: How to help us help you - or How to ask a good question

now the garage door is open

And the template result?

template result?
where can I find this?

In my original post:

nothing happens to the status.
when i press the arrow key the gate opens or closes.
But the status does not change.
I don’t know how to use the developer tools template editor.
I’ve never worked with it.

i think i have it
is that right?

Yes that’s the one. Now try this in the template editor with the door open:

{{ is_state_attr('switch.shelly_shsw_1_ba2c28', 'switch', true) }}

when the door is open, it says True

Use that template in your cover.

that’s the same template as before

platform: template ########################################################################### Garage
covers:
garage_door:
device_class: garage
friendly_name: “Garage Door”
value_template: “{{ is_state_attr(‘switch.shelly_shsw_1_ba2c28’, ‘switch’, ‘true’) }}”
open_cover:
service: switch.turn_on
data:
entity_id: switch.shelly_shsw_1_ba2c28
close_cover:
service: switch.turn_off
data:
entity_id: switch.shelly_shsw_1_ba2c28
icon_template: >-
{% if is_state(‘sensor.garage_state’, ‘True’) %}
mdi:garage-open-variant
{% else %}
mdi:garage-variant
{% endif %}

Nope. true and 'true' are two very different things. One is a logic state the other is a string.

1 Like