MQTT, AC Dimmer, ESPHome, without Home Assistant

Hi, I’m new inside the ESPhome thing. I discovered ESPHome while searching for an adaptor to control the speed of an AC circulation pump. ESPHome provides such a solution with the “AC Dimmer” platform.

I checked out the basic functionality of these AC Dimmer boards with a simple Arduino sketch, provided on the manufacture’s website (Robotdyn comp.).

So, what is my problem with ESPHome?

The task seems to be comparable simple: NodeRed calculates a value representing the desired speed of the pump (i.E. 0-100% or 0.0-1.0). This value is sent as a MQTT payload to the ES32 (in my case).
I do not use HASS or IO-Broker.
This so far and other things like connecting PT100 via MAX31865 or OLED displays are working.
At this point many thanks to the ESPHome.io website which is really extended and helpful.

Unfortunately, I am not able to find a way to “connect” this MQTT payload with the “AC Dimmer” platform. I’m pretty sure, that the solution is simple, but it drives me to despair.

Attached, you’ll find a (not working) YAML file cleaned up from stuff, not belonging to this topic. I would be very glad if someone could take some time to solve this.
Thank you in advance!

esphome:
  name: "solarregler"

esp32:
  board: esp32doit-devkit-v1
  framework:
    type: arduino

# Enable logging
logger:


ota:
  password: ""

wifi:
  ssid: "xxx.xxx.xxx.xxx"
  password: "xxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Solarregler Fallback"
    password: "xxxxx"

captive_portal:
    
mqtt:
  broker: .xxx.xxx.xxx.xxx
  on_message:
    topic: S_Pumpe/Leistung
    then:
      - output.set_level:
        id: dimmer1
        level: payload #number doesn't work either

sensor:
   - platform: mqtt_subscribe
    name: "Drehzahl Solarpumpe"
    id: rpm_Solarpumpe
    topic: S_Pumpe/Leistung  
   
    
output:
  - platform: ac_dimmer
    id: dimmer1
    gate_pin: GPIO4
    zero_cross_pin:
      number: GPIO2
      mode:
        input: true
      inverted: yes

MQTT requires a broker - i.e. a middle man that acts as a post office for your messages. It is not a peer-peer protocol.

You could install one yourself, or use one of the free public ones (not recommended).

Thank you very much for response. Obviously, I must apologize for the insufficient description of my problem. I will do my best to make it more comprehensible:
Of course, there is a MQTT broker running at my home.
I have no problems with receiving topics/payloads published by ESPHome (I.E: PT100 probe → Max31865 chip → Max31865 platform → MQTT broker → NodeRed).
Also sending data doesn’t cause any headache (NodeRed → MQTT broker → [text]sensor; platform: mqtt_subscribe).

What I can’t figure out is, how to link a value to the gate_pin of the AC Dimmer platform (NodeRed → MQTT broker → platform: mqtt_subscribe ???->??? platform: AC Dimmer → gate_pin → Robotdyn AC Dimmer board).

Or in other words, what kind of YAML code is necessary to control the gate_pin of the AC Dimmer platform via a MQTT message.

I hop that this description points out my problem. I fear, that the solution is so easy, that you can’t imagine that someone is struggling with it.

I mostly use MQTT for sensors - i.e. published to the broker or with HA which handles the subscribe etc, so haven’t played with this.

MQTT on ESPHome has an on_message: trigger that can be used to subscribe to a topic, then perform actions using the standard actions available for your output - e.g:

mqtt:
  broker: x.x.x.x
  username: !secret mqtt_username
  password: !secret mqtt_password
  # etc for the rest of your mqtt config
  on_message: 
    - topic: whatever/your/topic
      then:
        - output.turn_on: relay_1
          # or possibly
        - output.set_level:
            id: output_1
            level: 50%

output:
  - platform: gpio
    pin: D1
    id: relay_1

  - platform: ac_dimmer
    id: output_1
    gate_pin: D7
    zero_cross_pin:
      number: D6
      mode:
        input: true
      inverted: yes

Now hopefully the experts will step in here if I have anything wrong, but that’s how I would try it.

Also just read your yaml - after I posted the above:

  1. If you want to use a numeric value as the level - use lambda. There is an example in the mqtt doco. HOWEVER: reading output_set_level I don’t see it’s supported for ac_dimmer, but then it must be as that output has no actions of it’s own??

  2. I am not sure if you need to use mqqt_subscribe if you use on_message - but if you do your indentation is wrong. Edit: of course you have to use mqqt_subscribe, on_message is for when you source your data locally on the ESP and just want to do something with it on command. In fact you don’t need on_message at all.

sensor:
  - platform: mqtt_subscribe
    name: "Drehzahl Solarpumpe"
    id: rpm_Solarpumpe
    topic: S_Pumpe/Leistung  
    on_value:
      then:
        - lambda:
            id(dimmer_1).set_level(x);  # whatever maths needed here to get a value between 0.0 and 1.0

Lambda is your main answer here - it looks like you have all the rest and it’s just presenting the received topic in a format that set_level expects, which is done using lambda.

Hi, thanks a lot for all these information and tips. I will try out all of them in the evening when I have access to my home server. Of course, I report the results afterwards.

For tonight I surrender. I handn’t any sucess. I think, I have to get informed about Lambda before I go ahead. At this moment it is only waste of time. Though I get incrementials of 1 inside ESPHome via MQTT there is no change of the gate_pin. I’ll reply if there are news.

I did some Googling before breakfast. First seems there are issues with the ac_dimmer, the end of this post shows that they didn’t use it but instead went with a custom component:

The second thing I found is that I should have looked closer at the doco example (doco is rubbish for this component). You need to define an output as a dimmable light and apply the ;level to that:

output:
  - platform: ac_dimmer
    id: dimmer_light
    gate_pin: GPIO18
    zero_cross_pin:
      number: GPIO19
      mode: INPUT
      inverted: yes

light:
  - platform: monochromatic
    output: dimmer_light
    id: solarpumpe

sensor:
  - platform: mqtt_subscribe
    name: "Drehzahl Solarpumpe"
    id: rpm_Solarpumpe
    topic: S_Pumpe/Leistung  
    on_value:
      then:
        - light.turn_on:
            id: solarpumpe
            brightness: !lambda |-
              return x;  # whatever maths needed here to get a value between 0.0 and 1.0

Hi Daryl,
good news at first: Your first link was successful! With the custom component it is possible to control those Robotdyn Dimmer boards via MQTT.
The supposedly easy way (making everything with preformed ESPHome components; your second hint) causes a strange behaviour of the board: The motor only starts after unplug and replug the Pump. It runs only at 100% though the logfile shows the correct input percentages of “brightness” like 0-100% (MQTT payload 0-1). Probably the Dimmer module is really buggy.
If so, it would be great if it would be fixed in the future because the options (half cycle and triac management) could be extremely helpful.
Maybe there are similar options in the manufactures library, but this is a task for tomorrow. It’ way after midnight here…