openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

PWM / PID controller rule from template in js syntax #117

Closed JacekKac closed 2 years ago

JacekKac commented 2 years ago

Hi, i need your help with syntax for PWM and / or PID controller rule creation using JSRule.

  1. My goal is to create such rule (snippset of code after creation in GUI) using JSRule
    
    configuration: {}
    triggers:
    - id: aa3cd1af-21b2-4ea2-b04b-bdcb4912ad93
    label: PWM Trigger
    configuration:
      deadManSwitch: 50000
      dutycycleItem: HeaterSwitch_dutycycle
      minDutycycle: 0
      interval: 600
      maxDutycycle: 100
    type: pwm.trigger
    conditions: []
    actions:
    - inputs: {}
    id: "1"
    configuration:
      itemName: HeaterSwitch
    type: core.ItemCommandAction
    - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
    type: script.ScriptAction

2. My first attemp is: 
rules.JSRule({
  name: "Termostat x PWM rule",
  id:   "Termostat_x_PWM",
  tags: ['Roomx','_DYNAMIC_'],
  overwrite: "true",
  description: "PWM rule for heating actuator",
  triggers: [triggers.pwm('RadiatorACx_homekit_TargetHeatingCoolingMode')],
  execute: data => {

  }
of course it is now working as expected. 
Can somebody help me how to define the triggers with items in configuration 
JacekKac commented 2 years ago

in addition i was able to do it in

scriptExtension.importPreset("RuleSupport");
scriptExtension.importPreset("RuleSimple");
var sRule = new SimpleRule() {
        execute: function(module, input) {
            print("input:" + input); // input contains all the output values
        }
    };
    sRule.setName('PWM' + RadiatorACx);
    sRule.setTags(java.util.Set.of("PWM"));
    sRule.setTriggers([
        TriggerBuilder.create()
            .withId(RadiatorACx)
            .withLabel(RadiatorACx)
            .withTypeUID("pwm.trigger")
            .withConfiguration(
                new Configuration({
                "deadManSwitch" : RadiatorACx + '_PWM_deadManSwitch',
                "minDutycycle"  : RadiatorACx + '_PWM_minDutycycle',
                "dutycycleItem" : RadiatorACx + '_PWM_dutycycleItem',
                "interval"      : RadiatorACx + '_PWM_interval',
                "maxDutycycle"  : RadiatorACx + '_PWM_maxDutycycle'
                })).build()
        ]);
    sRule.setActions([
        ActionBuilder.create()
        .withId("1")
        .withTypeUID("core.ItemCommandAction")
        .withConfiguration(
                new Configuration({
            "itemName" : RadiatorACx + '_Relay'
                })).build()
        ]);
    automationManager.addRule(sRule);
JacekKac commented 2 years ago

any idea how to do this ? it's urgent for me

florian-h05 commented 2 years ago

Yes, I have an idea, I am taking a look into this at the moment.

florian-h05 commented 2 years ago

Hello @JacekKac,

I have a solution, two PRs are open now. I have to wait for @digitaldan for review, but I can give you some impression hot it will work:

rules.JSRule({
  name: "Termostat x PWM rule",
  triggers: [
    triggers.PWMTrigger('pwm_dimmer', 10)
  ],
  execute: (event, input) => {
    items.getItem('pwm_switch').sendCommand(input.command.toString());
  }
});