Changing the duty_cycle from within HA

Can anyone help me with challenge Ive gotten this far with Discord just need a little bit more help.
Im using a slow_pwm signal to adjust a dosing pump and would like to adjust the speed of the pump within Home Assistant.
So far i have added this to ESPhome node
Screen Shot 2021-05-01 at 5.08.49 pm

esphome:
  name: sprinkler_controller
  platform: ESP32
  board: esp32-poe-iso
  on_boot:
    priority: -10
    then:
      - output.turn_on: dose_feedrate
      - output.set_level:
          id: dose_feedrate
          level: "50%"
ethernet:
  type: LAN8720
  mdc_pin: GPIO23
  mdio_pin: GPIO18
  clk_mode: GPIO17_OUT
  phy_addr: 0
  power_pin: GPIO12
  manual_ip:
    static_ip: 192.168.1.52
    gateway: 192.168.1.1
    subnet: 255.255.255.0

#captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

power_supply:
  - id: 'power_supply1'
    pin: 
      number: 13
      inverted: true
    enable_time: 20ms
    keep_on_time: 60min

    
switch:
  - platform: output
    name: "Kerb Sprinklers"
    output: 'valve_1'
  
  
  - platform: output
    name: "House Side Sprinklers"
    output: 'valve_2'
 
  - platform: gpio
    pin:
      number: 5
      inverted: true
    name: "Spare"  
 
  - platform: output
    name: "Dose feedrate"
    output: "dose_feedrate"

output:
  - platform: slow_pwm
    pin:
      number: 15
    id: dose_feedrate
    period: 4s
  
  
  - platform: gpio
    pin:
      number: 2
      inverted: true
    id: valve_1  
    power_supply: power_supply1

    
  - platform: gpio
    pin:
      number: 4
      inverted: true
    id: valve_2 
    power_supply: power_supply1  
                      
    
    
    
    
    

This was the example code i was given Thank you @MauriceM#9564

api:
  services:
    - service: set_duty_cycle
      variables:
        new_duty_cycle: float
      then:
        - output.set_level:
            id: my_thingy
            level: !lambda return new_duty_cycle;
    - service: set_period
      variables:
        new_period: int
      then:
        - lambda: id(my_thingy).set_period(new_period);

Added this to Config.yaml file in HA

api:
  services:
    - service: set_duty_cycle
      variables:
        new_duty_cycle: float
      then:
        - output.set_level:
            id: dose_feedrate
            level: -lambda return 0.25
    - service: set_period
      variables:
        new_period: int
      then:
        - lambda: id(dose_feedrate).set_period(500);

Restarted HA server
In the services drop down i cant find either of these service calls so ive tried to enter it manual

ScreenShot 2021-05-01 at 4.19.48 pm
Screen Shot 2021-05-01 at 4.53.54 pm


and just keep getting this error I think I’m way in over my head here.
Any help would be great and i thank you in advance.

Before I sent you that services code, I tried adding it to a development device After flashing the device, those services popped up right away in my service drop-down and I was able to call them. Conclusion: you shouldn’t have to manually type them in.

Having said that, I do not know what could be the issue when they don’t show up.
Anything showing up in the device’s logs after a restart/firmware update, explaining why it wouldn’t expose those services?

Hi Maurice We meet again!
I will try a couple of things (reflash the node)
Should I do a full HA reboot?
And was i meant to change the new_duty_cycle; and the (new_period); in my example or was meant to leave it the way you typed it.
Thanks again
Nick

I did not need a reboot of HA. But try a reflash and try removing and re-adding the device in HA. Also keep an eye on the logs.of the device during boot.

The new_duty_cycle and new_period are simply parameter names for the service call. You could call those anything you like. No need to change them.

1 Like

Hi Maurice
HA doesn’t like the ! in front of the lambda.

level: !lambda return new_duty_cycle;

Screen Shot 2021-05-02 at 7.51.01 am

And Im still not seeing the service call in the list ive tried reflashing, removing and adding the esphome node.
Maybe I have not setup the slow_pwm component properly in esphome .
Could show me your setup when you added it to your development device.
Thanks Mate

I rolled back that setup, so I don’t have it anymore.
The code that I pasted to you was literally from my setup.

But if you can show what you’ve got so far, then I will check if I can find an issue with it.

HA wouldn’t mind the !lambda part. That is fully in the domain of ESPHome, and should result in compiled code. At what point do you get that error?

I’ve being entering the api code into the HA configuration.yaml is that right?
Ok thank you what can I send you some the config file

WOW I feel a bit silly my bad I was putting the code in the wrong place.
Because there’s a api: entry in the configuration.yaml too.
Sorry to waste your time Maurice.

Haha, don’t you worry. We’re all here to learn. I’m glad you found out where it needed to go!

More importantly: does it work?

Ok I’m going to have a play with it tomorrow I’ll keep you posted.

Works like a charm thanks Maurice for your patience! :grinning:


Screen Shot 2021-05-04 at 7.21.20 pm