Setting up a basic PID controller using a temperature sensor, and a power switch & PWM

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: Openhabian
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: openHAB 4.3.2
  • Issue of the topic: Cant find a step by step guide for a simple PID rule

Hello!

I am new to openHAB and home automation in general.

I got some shelly power switches and esphome based temperature sensors.

After playing around with Modeling and Plots i would like to try out the PID controller
add on. My understanding from the documentation is that in order for the PID to work
i need to have a action that accepts more then just ON/OFF. The way to do that would
be with a PWM item.

I just couldnt find any example/tutorial on how to implement that.

Any suggestions and links are welcome. Im stuck for 2 days now and dont seem to get
any closer to even test the PID addon.

thanks for any feedback!

First of all: Welcome to the very best home-automation community.
Btw: Trying for 2 days is quite a lot. You can ask here earlier and the best way is to describe your goal in detail and what you already tried (e.g. paste your code (use code fences!) or a screenshot of your rule).
I currently don’t use PID or PWM controllers but this is how I think it should work:

  • Install the add-ons (I guess that’s done)
  • Create a rule
    – chose ID and label as you like
    – select PWM as your template
    – save (ignore the error)
  • click PWM-trigger and chose your parameters
  • click add action and select your actuator and save the rule

Repeat the same for the PID-rule and chose your PWM-item as the action item.

Hi Lohtek

welcome to the openHAB world of wonder where anything is possible. Just sometimes a bad idea.

Please give all the information you can you will get your best results.

EG- Using a shelly to control temp in reptile enclosure with epshome temp sensor.

Advise you will cook your reptile when something happens you don’t understand happens and the heater stays on. Use a store bought heating solution.

Hi!

Thanks for the nice welcome!

For now i just want to use a 40W lightbulb on a power switch & a Temperature sensor
in a shoe box to test the capabilities of the PID controller and my ability to configure it.

If i manage that i want to replace the thermostat in my small workshop with a openhab
based control solution.

What i did so far is:

Create all the necessary items and channels. That worked like a charm!
Put up nice plots on the Overviewpage. Also very intuitive and very nice to look at.

For my PID experiment i installed the PID Controller and the PWM Controller Add-ons.

Then i created a String item with the Tags Setpoint and Temperature and put a control widget on the overview page for easy access.

After that i created a heaterpwm rule. And last but not least the PID rule.

Since my last post i fixed some errors and the PID is now triggering the PWM and the lamp/heater goes on. it overshoots and the openhab rules page is not responsive. but thats just
tuning issues i asume.

I guess now comes the tuning part. ill come back when i have finished playing around with the pwm dutycycle and other parameters and the PID.

Hello again!

Had some time to continue playing with PID and PWM so here is an update.

created the following items:

type: Number, class: Setpoint, semantic property: temperature
testSetpoint - this is used as the target temperature

type: Number, class: Equipment_HVAC
PwmDutyCycleItem - this one gets the value from the PID and is used by the PWM controller

type: Number, class: Point
PIDpInspector - Name of the inspector Item for the current P-part
PIDiInspector - Name of the inspector Item for the current I-part
PIDdInspector - Name of the inspector Item for the current D-part
PIDeInspector - Name of the inspector Item for the current regulation difference (error)

created a rule PWM controller:

configuration: {}
triggers:
  - id: 8ce443da-d45a-4202-8558-e98b2f472189
    label: PWM Trigger
    configuration:
      dutycycleItem: PWMdutycycleItem
      minDutycycle: 0
      equateMinToZero: false
      deadManSwitch: 10000
      interval: 60
      maxDutycycle: 100
      equateMaxToHundred: true
    type: pwm.trigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      command: ON
      itemName: shelly1pmminig33030f9ec8240__10101020_Power
    type: core.ItemCommandAction

created a rule PID controller:

configuration: {}
triggers:
  - id: 719fd807-bd8a-4907-9de3-7e6992e6a442
    label: PID Controller Trigger
    configuration:
      iInspector: PIDiInspector
      setpoint: testsetpoint
      kp: 1
      pInspector: PIDpInspector
      kdTimeConstant: 1
      dInspector: PIDdInspector
      integralMaxValue: 100
      input: Test_1_Temperature
      eInspector: PIDeInspector
      kd: 1
      integralMinValue: 0
      ki: 1
      loopTime: 1000
    type: pidcontroller.trigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      itemName: PWMdutycycleItem
    type: core.ItemCommandAction

still not working properly. it switches on below testsetpoint, but does not switch off.
even if the PWMdutycycleItem is 0 or , and i dont know how this can be is -1.

but then again i only defined a ON in the PWM Rules as command.

any insights are welcome. but i am in no hurry and confident that i can figure this out within a month or something like that…

cheers

Hi Lohtek

I don´t use the PWM Controller only the PID controller, but i guess your interval: 60 is to big.

http://10.0.0.97:8080/addons/automation/automation-pwm#:~:text=Yes-,interval,Yes,-minDutyCycle

Start with 1 sec. and adjust while testing

Hi!

This was not the fault, just changes the “reaction time” to duty cycle changes.

Well, i still dont know why PWM does not switch of when dutycyle is 0, or and thats strange sometimes -1 or -2.

I think i can build a scheduled script that checks if dutycycle is 0 and switches
off the power to the lamp/heater. but thats a workaround, id rather find out
what i am missing here with the PWM. At least i think i have a PWM problem
since my PID sets the dutycyle as i want it to, except for the small negative values
which should not matter since the PWM rounds up evrything below 0 to zero.
At least thats what the documentation in the GUI says.

Any ideas anyone?

I just reviewed your PWM code and i think found the reason why it’s not working.

You defined a command in your item action but the command should be empty.

Pulse Width Modulation (PWM) Automation - Automation | openHAB.

BINGO!

Thanks, that was the problem.
i almost got it a few days ago, regarding the defining ON but not OFF.

case closed.

working pwm config:

configuration: {}
triggers:
  - id: 8ce443da-d45a-4202-8558-e98b2f472189
    label: PWM Trigger
    configuration:
      dutycycleItem: PWMdutycycleItem
      minDutycycle: 0
      equateMinToZero: true
      deadManSwitch: 10000
      interval: 60
      maxDutycycle: 100
      equateMaxToHundred: true
    type: pwm.trigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      itemName: shelly1pmminig33030f9ec8240__10101020_Power
    type: core.ItemCommandAction