Data_template position cover

Can someone plase help me with this.
It is so simple but i just can’t get it working.

   trigger:
     - platform: event
       event_type: deconz_event
       event_data:
         id: lumisensor_switch_6
         event: 1002

     - platform: event
       event_type: deconz_event
       event_data:
         id: lumisensor_switch_6
         event: 1004


   action:
     service: cover.set_cover_position
     entity_id: cover.markis_sovrum_level
     data_template:
       position: >
        {%- if trigger.event == "1002" -%}
         100
        {%- elif trigger.event == "1004" -%}
         50
        {%- endif -%}

hass accept it but nothing happeds.

Try:

     data_template:
       position: >
        {% if trigger.event.data.event|int == 1002 %}
         100
        {% else %}
         50
        {% endif %}

Or even:

     data_template:
       position: "{{ 100 if trigger.event.data.event|int == 1002 else 50 }}"

Hi. Thank you for your answer.
I works perfekt. Thank you.
I want 3 different positions so I did it like this.

Is it possible to put a condition (state dondition from door switch) in here only allow 100 position, not 50 or 0 if door i open?

     data_template:
       position: >
        {% if trigger.event.data.event|int == 1002 %}
         100
#is it possible to put a condition here? A state condition för a doorswitch? Only allow  100 but not 50 or 0.
        {% elif trigger.event.data.event|int == 1004 %}
         50
        {% elif trigger.event.data.event|int == 1005 %}
         0
        {% endif %}

Sure. If you want to move cover.markis_sovrum_level to 100 percent in this case, then:

  trigger:
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1002
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1004
  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% if trigger.event.data.event|int == 1002 or
              is_state('binary_sensor.my_door','on') %}
          100
        {% elif trigger.event.data.event|int == 1004 %}
          50
        {% elif trigger.event.data.event|int == 1005 %}
          0
        {% endif %}

Or if you want to ignore the trigger in this case and not move cover.markis_sovrum_level:

  trigger:
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1002
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1004
  condition:
    condition: state
    entity_id: binary_sensor.my_door
    state: 'off'
  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% if trigger.event.data.event|int == 1002 %}
          100
        {% elif trigger.event.data.event|int == 1004 %}
          50
        {% elif trigger.event.data.event|int == 1005 %}
          0
        {% endif %}

Not sure what you’re using to sense if the door is open or not. But assuming it’s a binary_sensor, then a state of ‘on’ typically means open. If you’re using something else, then adjust accordingly.

But looking at the template in your action, you really should not use an if statement without an else. If the event ever somehow wasn’t one of the ones you were expecting, this wouldn’t work and it would cause an error. You should just simply replace your last elif with an else. There’s really no reason for the last one to be an elif, and there’s really a good reason for it not to be.

Also, you could do something like this instead:

    data_template:
      position: >
        {% set positions = {1002: 100, 1004: 50, 1005: 0} %}
        {% set event = trigger.event.data.event|int %}
        {% if event in positions %}
          {{ positions[event] }}
        {% else %}
          100
        {% endif %}

Thank you.
I like this alternative.

    data_template:
      position: >
        {% if trigger.event.data.event|int == 1002 or
              is_state('binary_sensor.my_door','on') %}
          100
        {% elif trigger.event.data.event|int == 1004 %}
          50
        {% elif trigger.event.data.event|int == 1005 %}
          0
        {% endif %}

Then i can choose witch position getting triggered depending the door.
But hass doesn’t like it. Getting an error.

What error? My guess would be you don’t have an entity named binary_sensor.my_door. That was just a placeholder I put in because you didn’t say what that entity was.

Sorry. My bad.
I created a “package” for this. But when testing i moved the automation to automation.yaml because it is easier to reload automations when testing. The error was because i missed to # before some lines in my package-file.

Now i got what i want.
Is this ok for the else-part you mentioned?

 - alias: Knapp för markis sovrum
   trigger:
     - platform: event
       event_type: deconz_event
       event_data:
         id: lumisensor_switch_6
         event: 1002

     - platform: event
       event_type: deconz_event
       event_data:
         id: lumisensor_switch_6
         event: 1004

     - platform: event
       event_type: deconz_event
       event_data:
         id: lumisensor_switch_6
         event: 1005

   action:
     service: cover.set_cover_position
     entity_id: cover.markis_sovrum_level
     data_template:
       position: >
         {% if trigger.event.data.event|int == 1002 %}
           100
         {% elif trigger.event.data.event|int == 1004 and
               is_state('binary_sensor.altandorr_sovrum', 'off') %}
           30
         {% elif trigger.event.data.event|int == 1005 and
               is_state('binary_sensor.altandorr_sovrum', 'off') %}
           0
         {% else %}
           none
         {% endif %}

I guess I need elif for all of them to get the condition?

No, that won’t work. You can’t call the cover.set_cover_position service with a position value of None. If binary_sensor.altandorr_sovrum is ‘on’, you either need to skip calling the service in the first place, or you need to call it with some particular value (e.g., 100.) I showed you both ways. Is there some reason you don’t think they’ll work or do what you want?

Hi. Been away for some time.
I understand I can’t use position value of none, but how to use conditions?
I only want one automation for this. Can I use condition for the “else”?
This automation works as I want but I understand it is wrong.

- alias: Knapp för markis sovrum
  trigger:
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1002

    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1004

    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1005

  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% if trigger.event.data.event|int == 1002 %} # This, I always want to allow
          100
        {% elif trigger.event.data.event|int == 1004 and
              is_state('binary_sensor.altandorr_sovrum', 'off') %} # This, only if door is closed
          30
        {% elif trigger.event.data.event|int == 1005 and
              is_state('binary_sensor.altandorr_sovrum', 'off') %} # This, only if door is closed
          0
        {% else %}
          none
        {% endif %}

If I understand you correctly, I already gave you the solution. I’ve updated it with your newer information, but it’s basically as before:

- alias: Knapp för markis sovrum
  trigger:
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1002
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1004
    - platform: event
      event_type: deconz_event
      event_data:
        id: lumisensor_switch_6
        event: 1005
  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% if is_state('binary_sensor.altandorr_sovrum', 'on') or
              trigger.event.data.event|int == 1002 %}
          100
        {% elif trigger.event.data.event|int == 1004 %}
          50
        {% else %}
          0
        {% endif %}

The first option (100) will be used no matter which trigger occurs if binary_sensor.altandorr_sovrum is ‘on’ (which I presume equates to open.) If binary_sensor.altandorr_sovrum is ‘off’ (which I presume equates to closed), then the value will be 100 if the event is 1002, 50 if the event is 1004, or 0 otherwise (e.g., event 1005.)

Is that what you’re looking for?

EDIT: I updated the first if to make it clearer, and I changed the last elif to else, which is what it should be.

Actually, I think this automation can be made even simpler:

- alias: Knapp för markis sovrum
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: lumisensor_switch_6
  condition:
    condition: template
    value_template: "{{ trigger.event.data.event|int in [1002, 1004, 1005] }}"
  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% if is_state('binary_sensor.altandorr_sovrum', 'on') or
              trigger.event.data.event|int == 1002 %}
          100
        {% elif trigger.event.data.event|int == 1004 %}
          50
        {% else %}
          0
        {% endif %}

I didn’t get it to work like a wanted your way. Like i said, I know it is wrong but it works.
I always want 1002 to trigger. Doesnt matter if binary sensor is on or off.
But for 1004 and 1005 I only want to allow if binary sensor is off

Ok, I think I misunderstood what you wanted when it comes to binary_sensor.altandorr_sovrum. I thought you wanted that to force 1004 and 1005 to set the cover position to 100. But I think now you want that to simply not allow 1004 and 1005 to do anything.

So if I understand correctly now, I would suggest this:

- alias: Knapp för markis sovrum
  trigger:
    platform: event
    event_type: deconz_event
  condition:
    condition: template
    value_template: >
      {% set event = trigger.event.data.event|int %}
      {{ trigger.event.data.id == 'lumisensor_switch_6' and
         (event == 1002 or
          event in [1004, 1005] and
          is_state('binary_sensor.altandorr_sovrum', 'off')) }}
  action:
    service: cover.set_cover_position
    entity_id: cover.markis_sovrum_level
    data_template:
      position: >
        {% set event = trigger.event.data.event|int %}
        {{ {1002: 100, 1004: 30, 1005: 0}[event] }}

Can anyone help me set the position of my garage door? Anytime mimolite_sensor_2 is “on” i’d like to set the position to 100…anytime it’s off I’d like it set to 0.

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        icon_template: >-
          {% if is_state('binary_sensor.fortrezz_mimolite_sensor_2', 'on') %}
            mdi:garage-open
      {% else %}
        mdi:garage
      {% endif %}

If you want help then you need to post your code properly formatted so we can read it. Please follow the directions at the top of the page.

Thanks for letting me know…first time on the forums. I’ve formatted my code in my original post. Thanks again.

1 Like

Can anyone help me out with using a binary sensor to set the cover position?

The binary sensor is “binary_sensor.fortrezz_mimolite_sensor_2”

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        value_template: "{{ states(binary_sensor.fortrezz_mimolite_sensor_2')|float > 0 }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        icon_template: >-
          {% if is_state('binary_sensor.fortrezz_mimolite_sensor_2', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

I thought this might work…but it doesn’t

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        value_template: "{{ states(binary_sensor.fortrezz_mimolite_sensor_2')|float > 0 }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.fortrezz_mimolite_switch_2
        data template:
        position: >
          {% if is_state('binary_sensor.fortrezz_mimolite_sensor_2', 'on') %}
            50
          {% else %}
            0
          {% endif %}          
        icon_template: >-
          {% if is_state('binary_sensor.fortrezz_mimolite_sensor_2', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

Can you tell us something about how switch.fortrezz_mimolite_switch_2 controls the garage door? What happens when it is turned on? What makes it go back off?

Also, what does binary_sensor.fortrezz_mimolite_sensor_2 indicate? I.e., what causes binary_sensor.fortrezz_mimolite_sensor_2 to be on, and what causes it to be off?

For what it is worth, I’m not sure you’ll be able to set the door’s “position” with only a binary sensor. At best, if you can start the door closing, or start the door opening, and stop it, and you know how fast it moves, then you might be able to estimate position. But since you seem to turn the same switch on for opening, closing and stopping, it’s not clear how your system works.

When I turn on “switch.fortrezz_mimolite_switch_2” the door it momentarily sends a signal to the garage door opener. So if I turn the switch on it opens the door, next time I turn the switch on it closes the door.

When the garage door opens “binary_sensor.fortrezz_mimolite_sensor_2” switches to “on”. “off” when the door is closed.

I’m not really concerned about the exact position…I just want the door to know when it’s open or closed. This works fine until I use the garage remote in my car…then the cover loses track if the door is open or close.