Mqtt cover with set_position_template to adjust linear behavior of position slider

Can someone help me with the configuration of MQTT covers.

My code works, until the set position template. What i try to accomplish is to linearize the movement of the cover with the position slider, via a mathematic equation. Otherwise the first 30% is used to make space between the seperate rows of the cover, and from 30 until 100 it will move up.

Anyway here is my yaml:

cover:
  - platform: mqtt
    name: 'MQTT Cover'
    command_topic: 'domoticz/in'
    payload_open: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 100 }'
    payload_close: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 0 }'
    state_topic: 'home/rolluik_tuin/state'
    state_open: 100
    state_closed: 0
    set_position_topic: 'domoticz/in'
    set_position_template: '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": {{ (position*(100-30)/100+30)|int }} }'
    optimistic: false

Here my set_position_template works, but as soon as I change it to:

cover:
  - platform: mqtt
    name: 'MQTT Cover'
    command_topic: 'domoticz/in'
    payload_open: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 100 }'
    payload_close: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 0 }'
    state_topic: 'home/rolluik_tuin/state'
    state_open: 100
    state_closed: 0
    set_position_topic: 'domoticz/in'
    set_position_template: >- 
      {%- if position > 0 -%}
        '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": {{ (position*(100-30)/100+30)|int }} }'
      {%- else -%}
        '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 0 }'
      {%- endif -%}
    optimistic: false

the template will not work anymore.

Can someone help me to get it working?

p.s. how can I nicely format my forum posts as readable yaml, because my code looks like shit in plain text…

I’m sorry for that

You need to format your code properly, use the blue link at the top of the post. Edit your original post. Do not copy from your original post and paste into a new post. The original formatting from your yaml will be lost if you do.

Somewhat separate question…For your MQTT cover, do you actually get a position slider in your “cover panel”? I don’t get a position slider with my mqtt cover, so was curious if you did.

Yes,I have that slider. As soon as you enable that position template, and you receive feedback over the position it works.

To get the position you need to subscribe to the state topic. Your device should publish its state as an integer value in that topic (between 0-100). Eventually you can make a new topic, and let an automaiton read the original topic, en republish an integer for the state topic of the MQTT cover.

I made it like this:

- alias: rolluik-tuin state
  trigger:
     - platform: mqtt
       topic: 'domoticz/out'
  condition:
    condition: template
    value_template: '{{ trigger.payload_json.idx == 9 }}'
  action:
    service: mqtt.publish
    data_template:
      topic: 'home/rolluik_tuin/state'
      payload_template: '{{trigger.payload_json.svalue1|int}}'

You have to do your template like this to keep it a json object:

cover:
  - platform: mqtt
    name: 'MQTT Cover'
    command_topic: 'domoticz/in'
    payload_open: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 100 }'
    payload_close: '{"command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": 0 }'
    state_topic: 'home/rolluik_tuin/state'
    state_open: 100
    state_closed: 0
    set_position_topic: 'domoticz/in'
    set_position_template: '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": {{ (position*(100-30)/100+30)|int if position > 0 else 0 }} }'
    optimistic: false

Petro,

If I inderstand you correctly, it is not possible to introduce if-else-statements in the set_position_template of the MQTT cover configuration ?

I should publish the position directly to an ‘in between topic’ as an integer, make an automation that will listen to that topic, do the mathematics, en publishes it as a JSON to the ‘domoticz/in’ topic ?

Templates return strings only. That string will not be parsed into any other object types. If you put the template inside the object, the object itself is still the object. If you place the object inside a template, the object is now a string, no matter what.

So this question misses the mark. You can have if statements if your set position template doesn’t require a complex structure. Yours requires a complex structure.

Yeah you could do that. Or you could just use the logic that was given.

If you really wanted to go complex, you could have done this too:

set_position_template: '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": {% if position > 0%}{{ (position*(100-30)/100+30)|int }}{% else %}0{%endif%} }'

But that’s more verbose and identical to this:

set_position_template: '{ "command": "switchlight", "idx": 9, "switchcmd": "Set Level", "level": {{ (position*(100-30)/100+30)|int if position > 0 else 0 }} }'

Thank you, Petro.

I’m going to try this tonight.

I copied your config.yaml at the beginning of this thread. I did an MQTT publish of the state topic and set position topic… and this is what shows up in my cover panel.
Unfortunately no slider.

image

set_position_topic and set_position_template are required to make the slider work. If you changed your current topic, you may need to refresh your cache in your browser for the UI to update.

Petro,

Do you know what is the function or purpose of the value_template option?

it allows you to parse info out of your json object so you can use it as the state of your device. If you are using a json object in your mqtt information

I figured out how to get the slider to show up :slight_smile:
Configuring set_topics is indeed needed but the slider doesn’t show
up until an MQTT message matching the state topic with value 0 to 100 and not matching “state_open” nor “state_closed”.
When I first tried it, I sent a state topic with value of 100. This updates the state to “open” (as 100 matches “state_open”) but nothing else. When I send a state topic with value say 30, then the slider shows up.

Ah, that seems like a bug. It should show up when you are at 100.

Petro,

Is it also possible to as a template in the topic variable of the mqtt publish action?

Topic: if condition -> topic 1 elif condition -> topic 2 else topic 3

Now I’m running three different automation, with the same mqtt trigger via domoticz /out, every automation different conditions, and they publish the json svalue to three different topics, it would be nice if could do this with 1 automation.

The payload template is in all three cases the same, only topic changes.

Tonight I can post the three automations to make things more clear.