Fan MQTT Switch with value_template

Tags: #<Tag:0x00007fc412c1f0d8> #<Tag:0x00007fc412c1ef98>

I’m trying to use a MQTT controlled ventilator, which has different state and payload topics. The payload is a ‘cmd’ topic that must be called using ‘VENTMODE,LOW,0’ to set the ventilator to low. The state topic reports ‘1’ in the above case, 2 for middle speed, 3 for high, 11 for permanent low, 12 for permanent middle, 13 for permanent high and 0 for auto.

Now I’m trying to create a switch, which basically allows for toggling (for example) the middle speed, retains that state and changes the speed.
So far I have this:

- platform: mqtt
  name: "Ventilation 15 min middle"
  state_topic: "VENTILATION_GATEWAY/duco/Ventilationmode"
  command_topic: "VENTILATION_GATEWAY/cmd"
  payload_on: "VENTMODE,MIDDLE,0"
  payload_off: "VENTMODE,AUTO,0"
  value_template: >
    {% if value == 2 %}
      on
    {% else %}
      off
    {% endif %}
  state_on: "2"
  state_off: "0"
  optimistic: false
  qos: 0
  retain: true

Now, if I enable the switch, the speed of the fan goes to middle. But the switch immediately toggles off again. It appears it does not read the state topic correctly. I’ve tried other variations such as

  value_template: >
    {% if value == "2" %}
      on
    {% else %}
      off
    {% endif %}

But the switch will not retain the correct status. I’m doing something wrong, but not sure what exactly.

It’s unfortunate that mqtt.fan doesn’t allow more than ‘off, low, medium or high’ states or I would have been able to use that.

Hi,

I see your post is already a bit older, but I had some sort of the same issue and I solved it with making it all buttons.
See image below:

image

My dashboard code:

cards:
  - cards:
      - entity: null
        show_entity_picture: true
        show_name: true
        styles:
          name:
            - font-weight: bold
            - font-size: 20px
        state:
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '3'
              ]]]
            entity_picture: /local/Duco_high.png
            name: Ventilatie stand Hoog
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '2'
              ]]]
            entity_picture: /local/Duco_middle.png
            name: Ventilatie stand Midden
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '1'
              ]]]
            entity_picture: /local/Duco_low.png
            name: Ventilatie stand Laag
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '0'
              ]]]
            entity_picture: /local/Duco_auto.png
            name: Ventilatie stand Automatisch
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '11'
              ]]]
            entity_picture: /local/Duco_permanent_low.png
            name: Ventilatie stand Vast Laag
        tap_action:
          action: none
        type: 'custom:button-card'
    type: horizontal-stack
  - cards:
      - color_type: card
        show_icon: false
        name: High
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_high
        type: 'custom:button-card'
      - color_type: card
        show_icon: false
        name: Middle
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_middle
        type: 'custom:button-card'
      - color_type: card
        show_icon: false
        name: Low
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_low
        type: 'custom:button-card'
    type: horizontal-stack
  - cards:
      - color_type: card
        show_icon: false
        name: AUTO
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_auto
        type: 'custom:button-card'
      - color_type: card
        show_icon: false
        name: Permanent Low
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_permanent_low
        type: 'custom:button-card'
    type: horizontal-stack
type: vertical-stack

The top image is a button without function except showing the current state.
I’m using the custom Button Card to change icons into images.
My images are located in the folders as is described in the code.
If interested I can send them too.

1 Like

Yeah if you would be so kind to send those? :slight_smile:

Hi,

Can you also show me the config from the gateway where you configured the MQTT? And how you made the config in HA? i am struggling to get my DUCO running with the software from Arnemauer. I can control it from the Nodemcu but not via HA. https://github.com/arnemauer/Ducobox-ESPEasy-Plugin

This is my config from the esp8266 gateway, just check that you also have the “Send to Controller” checked on the Devices Tab per device. This means that the data is being send over MQTT if configured correctly.

A nice tool to check your MQTT messages from your gateway or any other devices is MQTTfx
https://mqttfx.jensd.de/

Thanks Jos, i missed that check box on " Send to controller" That did the trick!

Hey Jos,

I have MQTT data now, but i still can not make it work in HA. Can you share me the sensor or mqtt related data how you configured it in HA?

Thanks in advance.

Hi Joost,

Below the Configuration.yaml

# MQTT Duco Gateway Switch
switch:
  - platform: mqtt
    name: Duco Ventilation High
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,HIGH,0"
  - platform: mqtt
    name: Duco Ventilation Middle
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,MIDDLE,0"
  - platform: mqtt
    name: Duco Ventilation Low
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,LOW,0"
  - platform: mqtt
    name: Duco Ventilation Auto
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,AUTO,0"
  - platform: mqtt
    name: Duco Ventilation Permanent High
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,PERMANENTHIGH,0"
  - platform: mqtt
    name: Duco Ventilation Permanent Middle
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,PERMANENTMIDDLE,0"    
  - platform: mqtt
    name: Duco Ventilation Permanent Low
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
    command_topic: "VENTILATION_GATEWAY/cmd"
    payload_on: "VENTMODE,PERMANENTLOW,0"


# MQTT Duco Sensors
sensor:
  - platform: mqtt
    name: Duco Ventilation Status
    state_topic: "VENTILATION_GATEWAY/Ventilation_gateway_RF/Ventilationmode"
  - platform: mqtt
    name: Duco Gateway Status
    state_topic: "VENTILATION_GATEWAY/status/LWT"
  - platform: mqtt
    name: Duco CO2 PPM Meter
    state_topic: "VENTILATION_GATEWAY/External_CO2_Sensor/CO2_PPM"
  - platform: mqtt
    name: Duco Humidity Meter
    state_topic: "VENTILATION_GATEWAY/External_Humidity_Sensor/Relative_humidity"
  - platform: mqtt
    name: Duco Fan Speed
    state_topic: "VENTILATION_GATEWAY/Duco_Fan_Speed/Fan_Speed"

Below the Lovelace UI

cards:
  - cards:
      - entity: null
        show_entity_picture: true
        show_name: true
        styles:
          name:
            - font-weight: bold
            - font-size: 26px
        state:
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '3'
              ]]]
            entity_picture: /local/Duco_high.png
            name: Ventilatie stand Hoog
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '2'
              ]]]
            entity_picture: /local/Duco_middle.png
            name: Ventilatie stand Midden
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '1'
              ]]]
            entity_picture: /local/Duco_low.png
            name: Ventilatie stand Laag
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '0'
              ]]]
            entity_picture: /local/Duco_auto.png
            name: Ventilatie stand Automatisch
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '4'
              ]]]
            entity_picture: /local/Duco_away.png
            name: Ventilatie stand Afwezig
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '13'
              ]]]
            entity_picture: /local/Duco_permanent_high.png
            name: Ventilatie stand Vast Hoog
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '12'
              ]]]
            entity_picture: /local/Duco_permanent_middle.png
            name: Ventilatie stand Vast Midden
          - operator: template
            value: >
              [[[ return states['sensor.duco_ventilation_status'].state === '11'
              ]]]
            entity_picture: /local/Duco_permanent_low.png
            name: Ventilatie stand Vast Laag
        tap_action:
          action: none
        type: 'custom:button-card'
    type: horizontal-stack
  - cards:
      - color: 'rgb(66, 134, 244)'
        color_type: card
        show_icon: false
        name: Middle
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_permanent_middle
        type: 'custom:button-card'
      - color: 'rgb(66, 134, 244)'
        color_type: card
        show_icon: false
        name: High
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_permanent_high
        type: 'custom:button-card'
    type: horizontal-stack
  - cards:
      - color: 'rgb(66, 134, 244)'
        color_type: card
        show_icon: false
        name: Low
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_permanent_low
        type: 'custom:button-card'
      - color: 'rgb(66, 134, 244)'
        color_type: card
        show_icon: false
        name: AUTO
        styles:
          card:
            - font-size: 25px
            - font-weight: bold
        tap_action:
          action: toggle
        entity: switch.duco_ventilation_auto
        type: 'custom:button-card'
    type: horizontal-stack
type: vertical-stack

For the images I need to send them to your email if needed

I also have a gauge card to see the CO2 measured by the wired switch in the living room

type: gauge
entity: sensor.duco_co2_ppm_meter
min: 0
max: 1200
severity:
  green: 0
  yellow: 800
  red: 1000

To see any values in ESPEasy you need to put in the CO2 plugin and device id at 3, that’s my wired switch at least

Hello @Jozziej could you send me those pictures ? And did you maybe DIY-ed the Duco USB cable ( to use together with the Duco software ) So i can read / use the software and try to look for the correct external sensor node ( now just tried the - trial and error- method, without any result)

Hi Pim,

Sure!
Send me a PM with your email.

You have to use a CP2102 instead of FT232 which I you used.
The Duco software looks for the CP2102 chip, I just changed the hardware name.
If you want access for Service use the password “Stella”

Hi Jozziej,
is it possible to share the images?

Sure!

Send me a PM with your email