Rules again follow-up

Moderators: grovkillen, Stuntteam, TD-er

Message
Author
Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Rules again follow-up

#1 Post by Dick60 » 13 Feb 2023, 10:07

Latest firmware on a ESP8266
I wanted to PWM my ledstrip what works fine but on a regular base the leds are flickering. I found out that the reason of the flickering is the checks of all the rows in my script.
Is the solution to splitup in individual events or is there another solution.

Code: Select all

On serre_lichtmeting#Analog do
  If %eventvalue1%<50 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
  	PWM,16,0
GPIO,10,0
  Endif
  If %eventvalue1%>50 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
  	PWM,16,30
GPIO,10,1
  Endif
  If %eventvalue1%>80 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    PWM,16,120
GPIO,10,1
  Endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#2 Post by TD-er » 13 Feb 2023, 10:19

I think the flickering may be caused by re-setting the value while it hasn't changed.

This can of course be handled in the rules, but I think it should be the responsibility of the PWM command to not update if there wasn't any change.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#3 Post by TD-er » 13 Feb 2023, 10:21

Oh and one other thing.... could it be the analog input is maybe sometimes reading a higher value?
For example, if it is near the threshold you're checking for.

Do you use "oversampling" on the ADC task? If not, please do add a checkbox there.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#4 Post by Dick60 » 13 Feb 2023, 10:22

I agree, that is the problem but how can I prevent that check if no changes were made?

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#5 Post by TD-er » 13 Feb 2023, 10:23

Ah one other thing I notice....
you're using GPIO-16.
That's a rather special one on ESP8266.
This pin cannot use interrupts or low-level code to interact with it.
So better use one of the other pins as this 'special state' of the pin may also cause issues.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#6 Post by TD-er » 13 Feb 2023, 10:35

You do have a syntax error in your rules:

Code: Select all

On serre_lichtmeting#Analog do
  If %eventvalue1%<50 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    PWM,16,0
    GPIO,10,0
  Endif
  If %eventvalue1%>50 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    PWM,16,30
    GPIO,10,1
  Endif
  If %eventvalue1%>80 and [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    PWM,16,120
    GPIO,10,1
  Endif // <<< was missing
Endon
And since you're testing for the same pin states, you can also wrap it in the same single call simplifying the rules a lot.

Code: Select all

On serre_lichtmeting#Analog do
  if [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<50
      PWM,16,0
      GPIO,10,0
    Endif
    If %eventvalue1%>50
      PWM,16,30
      GPIO,10,1
    Endif
    If %eventvalue1%>80
      PWM,16,120
      GPIO,10,1
    Endif
  endif
Endon
This does make it clear what is actually happening...
When the eventvalue > 80, you set the PWM twice.

So simply test for >80 first and then an else with the check for > 50.

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<50
      PWM,16,0
      GPIO,10,0
    Else
      GPIO,10,1
      If %eventvalue1%>80
        PWM,16,120
      Else
        PWM,16,30
      Endif
    Endif
  Endif
Endon

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#7 Post by Dick60 » 13 Feb 2023, 11:28

OK that clear but I have more measurements moments, does that give no problems with the number of ELSE rows?

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#8 Post by TD-er » 13 Feb 2023, 11:51

Not sure about the exact stack depth for rules parsing, but if you hit that limit, I guess you should have another good look at the code :)

An else statement does actually make less compare checks, and the rules parser can skip some lines in the rules.
So I think it is the preferred way here.

But feel free to ask again if you're running into issues again :)

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#9 Post by Dick60 » 13 Feb 2023, 13:21

OK but does not the number of ELSE (in my case 6)affect the limitations of ESPEASY?

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#10 Post by TD-er » 13 Feb 2023, 13:25

The nr. of else statements doesn't matter.
It matters how "deep" you'll go.

For example:

Code: Select all

if ...
  if ...
    if ...
    endif
  endif
endif
This has a "depth" of 3. (I think the max. is 10)
The nr. of else statements doesn't add "depth" as the "else" is at the same level as the "if", since you can only have a single "else" per "if".

You can have multiple "elseif" per "if", but those also are at the same level as the "if".

See: https://espeasy.readthedocs.io/en/lates ... lseif-else

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#11 Post by Dick60 » 13 Feb 2023, 13:54

I'll give it a try

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#12 Post by Dick60 » 13 Feb 2023, 18:11

this is the result, the flickering has stoped but the script stop setting the PWM on the 2end level (Elseif %eventvalue1%>70). The pwm level is set to 120 but al the other mesurements do not change the PWM level

Code: Select all

On serre_lichtmeting#Analog do

  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1

    If %eventvalue1%<60

      PWM,16,00

      GPIO,10,0

    Elseif  %eventvalue1%>70

        PWM,16,120

       GPIO,10,1

      Elseif %eventvalue1%>95

       PWM,16,200

        GPIO,10,1

     Elseif %eventvalue1%>130

       PWM,16,300

        GPIO,10,1

      Elseif %eventvalue1%>170

       PWM,16,500

        GPIO,10,1

Elseif %eventvalue1%>200

       PWM,16,650

        GPIO,10,1

Elseif %eventvalue1%>300

       PWM,16,850

        GPIO,10,1

Elseif %eventvalue1%>400

       PWM,16,1000

        GPIO,10,1

      Endif

    Endif

Endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#13 Post by TD-er » 13 Feb 2023, 18:19

And that's perfectly explainable :)

First a quick cleanup of the code to make it readable

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<60
      PWM,16,00
      GPIO,10,0
    Elseif  %eventvalue1%>70
      PWM,16,120
      GPIO,10,1
    Elseif %eventvalue1%>95
      PWM,16,200
      GPIO,10,1
    Elseif %eventvalue1%>130
      PWM,16,300
      GPIO,10,1
    Elseif %eventvalue1%>170
      PWM,16,500
      GPIO,10,1
    Elseif %eventvalue1%>200
      PWM,16,650
       GPIO,10,1
    Elseif %eventvalue1%>300
      PWM,16,850
      GPIO,10,1
    Elseif %eventvalue1%>400
      PWM,16,1000
      GPIO,10,1
    Endif
  Endif
Endon
"400" > "300".
So if you first check for >300, then there will be no check for >400 in the else check.

So you must sort the checks in the opposite direction, or change the sign (and the appropriate values for the PWM of course)

This probably makes more sense when processing it :)

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<60
      PWM,16,00
      GPIO,10,0
    Elseif  %eventvalue1%<95
      PWM,16,120
      GPIO,10,1
    Elseif %eventvalue1%<130
      PWM,16,200
      GPIO,10,1
    Elseif %eventvalue1%<170
      PWM,16,300
      GPIO,10,1
    Elseif %eventvalue1%<200
      PWM,16,500
      GPIO,10,1
    Elseif %eventvalue1%<300
      PWM,16,650
       GPIO,10,1
    Elseif %eventvalue1%<400
      PWM,16,850
      GPIO,10,1
    Else
      PWM,16,1000
      GPIO,10,1
    Endif
  Endif
Endon

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#14 Post by Dick60 » 13 Feb 2023, 18:27

ahh, I've got it, your solutution is a much better approch. Thanks for the support and activate the sript immediately.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#15 Post by TD-er » 13 Feb 2023, 20:48

It can even made simpler:

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<60
      GPIO,10,0
    Else
      GPIO,10,1
    Endif
	
    If %eventvalue1%<60
      PWM,16,00
    Elseif  %eventvalue1%<95
      PWM,16,120
    Elseif %eventvalue1%<130
      PWM,16,200
    Elseif %eventvalue1%<170
      PWM,16,300
    Elseif %eventvalue1%<200
      PWM,16,500
    Elseif %eventvalue1%<300
      PWM,16,650
    Elseif %eventvalue1%<400
      PWM,16,850
    Else
      PWM,16,1000
    Endif
  Endif
Endon
And if there is a simple ratio between PWM value and eventvalue, it could be made even simpler.

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=1 and [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<60
      GPIO,10,0
    Else
      GPIO,10,1
    Endif
	
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
    Else
      PWM,16,1000
    Endif
  Endif
Endon
I did enter the values in Excel and noticed there was quite an outlier for <200 setting PWM to 500.
The trendline given by Excel was y = 2.5105x - 111.67
But I changed it into y = 2.5x - 125, to make it nicely cross 1000 at an eventvalue of 450.
So the PWM is now a near continuous linear line for eventvalue1 = 60 ... 1000

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#16 Post by Dick60 » 17 Feb 2023, 09:16

Thanks TD, I just saw your calculated solution. I am curious how it looks like this evening. I still struggle with I issue in this script. GPIO 13 is a power source. I turn it on and off in the script but if the check is done

Code: Select all

If [Plugin#GPIO#Pinstate#2]=1 or [Plugin#GPIO#Pinstate#0]=1
The measurements start for the lights-on steps but if something is changes in

Code: Select all

If [Plugin#GPIO#Pinstate#2]=1 or [Plugin#GPIO#Pinstate#0]=1
(both are 0). the GPIO 13 will not change to 13,0. I cannot get it work. Can you give it a try? I hope I was clear in my explanation.

Code: Select all

On serre_lichtmeting#Analog do  
  If [Plugin#GPIO#Pinstate#2]=1 or [Plugin#GPIO#Pinstate#0]=1
    If %eventvalue1%<60
      GPIO,10,0
      GPIO,13,0
    Else
      GPIO,10,1
      GPIO,13,1
    Endif
	
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
    Else
      PWM,16,1000
    Endif
  Endif
Endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#17 Post by TD-er » 17 Feb 2023, 09:25

I did move this check with the 2 pinstates to the start of your rules block, based on the previous version of the rules you gave.

So maybe you can tell me what the interpretation of the pinstates for GPIO 0 and 2 is?
What do they represent and what does it mean when either of them is 0 or 1?

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#18 Post by Dick60 » 17 Feb 2023, 09:39

0 is a pir, if no movement, turn of the light. 2 Is manual mode. I am not at home but once in a while I want to start the illusion that someone is at home.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#19 Post by TD-er » 17 Feb 2023, 09:48

OK, so this part should also be executed when GPIO-2 = 0, regardless of the state of GPIO-0?

Code: Select all

    If %eventvalue1%<60
      GPIO,10,0
      GPIO,13,0
    EndIf
So something like this:

Code: Select all

On serre_lichtmeting#Analog do
  If [Plugin#GPIO#Pinstate#2]=0
    // Not in manual mode
    If %eventvalue1%<60
      // Turn off lights
      GPIO,10,0
      GPIO,13,0
    Endif
  Endif

  If [Plugin#GPIO#Pinstate#2]=1 or [Plugin#GPIO#Pinstate#0]=1
    // Manual mode enabled, or motion detected.
    If %eventvalue1%<60
      // Turn off lights
      GPIO,10,0
      GPIO,13,0
    Else
      // Turn on lights    
      GPIO,10,1
      GPIO,13,1
    Endif
    
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
    Else
      PWM,16,1000
    Endif
  Endif
Endon
Not sure what the system should do in "manual mode".
Should it still allow to turn off the lights when in manual mode?
Should there be a timer when not in manual mode, to turn off the lights after some time when no motion is detected?

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#20 Post by Dick60 » 17 Feb 2023, 10:12

Not sure what the system should do in "manual mode".// In manual mode it must start GPIO 13 (power source) so the ligts turn on again depending on the LDR measurement. Turning off Manual mode will interupt the power source.
Should it still allow to turn off the lights when in manual mode?// Scripts still running but the power source (MOSFET in) is interrupted
Should there be a timer when not in manual mode, to turn off the lights after some time when no motion is detected?// there is already a rule that turn off the Manual mode on a certain time.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#21 Post by TD-er » 17 Feb 2023, 10:21

OK, so in other words, the "manual mode" just overrides the PIR?

In other words:
- Manual mode on: Only react to the light intensity
- Manual mode off: When PIR is active, turn lights on depending on light intensity

If so, then I think my last rules I posted should work like this.

However, since the events act on a measurement of the analog input, there may be a delay to switch on the lights.
Maybe it is better to act on the PIR and then look at the light intensity, instead of act on a light measurement and then look at the PIR state.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#22 Post by Dick60 » 17 Feb 2023, 14:37

I addapted to the PIR switch. Is this what you ment?
If so, I can test it this evening. FYI the GPIO 10 gives the status (1,0) of the leds.

Code: Select all

On Beweging#State do
If [Plugin#GPIO#Pinstate#2]=1 or  [Plugin#GPIO#Pinstate#0]=1
If %eventvalue1%<60
      GPIO,10,0
     GPIO,13,0
    Else
     GPIO,10,1
      GPIO,13,1
    Endif

    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
    Else
      PWM,16,1000
    Endif
  Endif
Endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#23 Post by TD-er » 17 Feb 2023, 14:57

No, I literally gave the rules.
You now moved all within the check for GPIO-2 OR -0 being 1, which isn't working.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#24 Post by Dick60 » 17 Feb 2023, 15:08

OK, misunderstanding. I move it and test the solution this evening👍

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#25 Post by Dick60 » 17 Feb 2023, 17:42

I tested the script and still not the behavior I expected. The light intensity is >60 and manual mode is off and movement is on. The leds turn on but if I turn off the movement, the leds do not turn of. So both Manual mode and Movement are off and light intensity is 80.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#26 Post by TD-er » 17 Feb 2023, 17:47

Can you write down what you would expect in these situations:

Manual mode: enabled
- Should there be a reaction to the motion sensor?
- Should the light be turned off when light intensity is too low?
- Should the PWM be depending on the light intensity meter?

Manual mode: disabled
- Should there be a reaction to the motion sensor?
- Should the light be turned off when light intensity is too low?
- Should the PWM be depending on the light intensity meter?

What should be the "turn off" value from the light intensity sensor?

How long should the light be kept on when manual mode is disabled and motion sensor sends a signal?

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#27 Post by Dick60 » 17 Feb 2023, 18:03

Manual mode enabled
-No reaction needed from the motion sensor
-Lights turn on if the light intensity is >60 but also turn off <60
-If lights on, PWM will in (de)crease the intensity of the leds.

Manual mode diabled
-Reaction of the motion sensor is required
-Lights turn on if the light intensity is >60 but also turn off <60
-If lights on, PWM will in (de)crease the intensity of the leds.
-IF BOTH MANUAL MODE AND MOTION SENSOR ARE OFF gpio 10 AND 13 NEED TO BE TURN OFF

No "turn off" value is needed for the light intensity sensor is needed

The pir activation need to be active for 30min

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#28 Post by TD-er » 17 Feb 2023, 18:22

OK, I think this last sentence of yours is where your problem is, which makes it slightly more complex.

I will write the rules, after dinner.
-If lights on, PWM will in (de)crease the intensity of the leds.
Meaning the PWM is determined by the light sensor?

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#29 Post by Dick60 » 17 Feb 2023, 18:43

Oh, I understand what the problem is.I did not realize. The function of the pir is, if leave the house or went to bed, the lights will turn off after finishing the 30 min pir activation. Enjoy your DINER !

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#30 Post by TD-er » 17 Feb 2023, 20:31

Not yet tested, but I think it should be something like this:

Code: Select all

on System#Boot do
  Monitor GPIO,0 // PIR sensor
  Monitor GPIO,2 // Manual mode
endon


on GPIO#0=1 do
  // PIR motion detected
  if [Plugin#GPIO#Pinstate#2]=0
    // Manual mode is not active, so we must act on the motion sensor
    timerSet,1,1800 // 30 minutes timer
    let,1,1 // Set variable #1 to indicate we had some motion detected  
    asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
  endif
endon

on GPIO#2 do
  // Change of "manual mode"
  asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
endon


On serre_lichtmeting#Analog do
  asyncevent,UpdateLightIntensity=%eventvalue1%
endon

on Rules#Timer=1 do
  // 30 minutes has past with no motion detected
  let,1,0 // Clear variable #1 to indicate no motion was detected.
  asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
endon

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
  else
    // Either manual mode is active, or recently detected motion
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
    Else
      PWM,16,1000
    Endif
  endif
endon
In short, there are 4 events which create a new event "UpdateLightIntensity" with as eventvalue the analog reading: [serre_lichtmeting#Analog]

- If PIR sensor detects something with "manual mode" disabled
- If "manual mode" changes
- When there is a new analog sample read
- 30 minutes after the last PIR motion detection.


At boot, I enable monitoring for the PIR sensor and the manual mode pin.
This allows me to receive events when those pins change.
Since we get an event immediately on PIR detection, the lights can be updated immediately and we don't have to wait for a new analog sample from the light intensity meter.

When motion is detected, a timer is being set to 30 minutes.
Every time a new motion is detected, this timer is being reset to 30 minutes.
Thus the timer will only expire 30 minutes after the last detected motion.
I also set a flag in variable #1 to keep track of the "recently motion detected" state we're in.

The actual work is being done when processing the "UpdateLightIntensity" event.
If manual mode is not active and no recent motion detected, turn off the lights.

Otherwise, change the PWM based on the measured light intensity.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#31 Post by Dick60 » 17 Feb 2023, 20:37

Wauw, that was more complicated as expected. Thanks for the explanation and I test the script functionality and try to understand every part of it.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#32 Post by TD-er » 17 Feb 2023, 20:40

Well it can be slightly less elaborate, but that would make it harder to understand.
Now it is clearly split into when we want to update the light and under what conditions.

It will also react more quickly on the detected motion, which is what you probably want :)

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#33 Post by Dick60 » 17 Feb 2023, 21:00

Yes it make sense for me now. For a long time I wanted to do more with Variables, now it is time to read the documentation for that item.
It will test the script. Everything is in place and restarted the unit, so lets hope. :)

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#34 Post by Dick60 » 21 Feb 2023, 07:47

This weekend I tested your solution but the first evening, I had a major disaster, the unit worked and on a given moment it start flickering and my unit stuck in a loop. Could not restart it so I refreshed the firmware and started over again. This morning first start and it works great. One remark: There was an error (Too many arguments: cmd=let Arg1=1 (fixed) Arg2=2.5*318 - 125 ExtraArg3=- ExtraArg4=125 lineLength=19) and corrected it by removing the spaces.This is the correct line. let,1,2.5*318-125.
ONE QUESTION: is there a way to check the value of Variable#1? I couldn't find it in the documentation.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#35 Post by TD-er » 21 Feb 2023, 07:55

If you look at the system variables page, you can also see any allocated variable and its current value.
Another way can be to write it in the logs, when you calculate it in the rules.

Code: Select all

LogEntry,"var#1: [var#1]"

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#36 Post by Dick60 » 21 Feb 2023, 09:22

Thanks for the extra info.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#37 Post by Dick60 » 22 Feb 2023, 07:26

I found that the var1 is not set and that the GPIO10 and 13 are not set to 1 if "Beweging " has the status of 1.
I do not want to mess-up my partly working script but tried to solve it to add the GPIO,10,1 and GPIO,13,1 but that did not work and would not solve the NOT storing the VAR.
Here the part of the script where the GPIO are added.

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
  else
    // Either manual mode is active, or recently detected motion
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<480
      let,1,2.5*%eventvalue1%-125 // Approximation of the curve
      PWM,16,[int#1]
      gpio,13,1
      gpio,10,1
    Else
      PWM,16,1000
    Endif
  endif
endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#38 Post by TD-er » 22 Feb 2023, 08:48

When should GPIO10 and 13 be set to 1?

1) Every time you set the PWM value?
2) Only when PWM value isn't 0?

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
  else
    // Either manual mode is active, or recently detected motion
    // Option #1
    If %eventvalue1%<60
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
      // Option #2
    Else
      PWM,16,1000
      // Option #2
    Endif
  endif
endon
I added 3 comments in the original piece of code, to indicate locations where you might want to set the GPIO 10 and 13 state to 1.
If option #1 applies, then set both GPIO commands to that line.
If option #2 applies, then set both GPIO commands to both comment lines indicating "Option #2"

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#39 Post by Dick60 » 22 Feb 2023, 11:35

I think this is the final one. I added comment just behind your OPTIONS comment.Can you check if this is correct?

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
  else
    // Either manual mode is active, or recently detected motion
    // Option #1 Will turn off 10 and 13 because %eventvalue1%<60
    If %eventvalue1%<60
    GPIO,10,0
    GPIO,13,0
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
     GPIO,10,1  // Option 2 Will turn on 10 and 13 because %eventvalue1%<60
     GPIO,13,1
        Else
      PWM,16,1000
     GPIO,10,1  // Option 3 Will turn on 10 and 13 because %eventvalue1%<60
     GPIO,13,1
 // Option #3
    Endif
  endif
endon

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#40 Post by TD-er » 22 Feb 2023, 12:00

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
  else
    // Either manual mode is active, or recently detected motion
    If %eventvalue1%<60
      // Option #1 Will turn off 10 and 13 because LightIntensity <60
      GPIO,10,0
      GPIO,13,0
      PWM,16,00
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
      GPIO,10,1  // Option 2 Will turn on 10 and 13 because LightIntensity >= 60
      GPIO,13,1
    Else
      PWM,16,1000
      GPIO,10,1  // Option 3 Will turn on 10 and 13 because LightIntensity >= 450
      GPIO,13,1
    Endif
  endif
endon
My only 'fix' is the correct indentation (to make it easier to read) and corrected the comments.
So functional there is no change with what you had.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#41 Post by Dick60 » 22 Feb 2023, 12:29

Thanks, I'm gonna test it this evening.

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#42 Post by Dick60 » 22 Feb 2023, 17:36

at this moment, the light intencity is 66 so I expected gpio 10 and 13 to turn on (1) but no reaction. Beweging is 1 so indicators are ok but the gpios do not switch. any idea?
Also no set for variables
%v1% 0 0

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#43 Post by TD-er » 22 Feb 2023, 17:43

Is the "manual" state set to 1?

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#44 Post by Dick60 » 22 Feb 2023, 17:51

Manual is active but in the log I also see no pwm settings. I turned on 13,1 manualy and leds are off. Manual http://xx.xx.xx.xxx/control?pwm=GPIO,16,100 and leds turn on. So I think that the PWm is not set, gpio 10 en 13 are not set to 1 if the lightintencity in >60.

At this moment situation: Lightintencity >60 and Beweging is 1. Turned off Mannual mode because it is Manual mode OR Beweging to turn on the leds.
Last edited by Dick60 on 22 Feb 2023, 17:54, edited 1 time in total.

TD-er
Core team member
Posts: 8739
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Rules again follow-up

#45 Post by TD-er » 22 Feb 2023, 17:53

With manual mode active, the PIR will not be registered as we set in the rules:

Code: Select all

on GPIO#0=1 do
  // PIR motion detected
  if [Plugin#GPIO#Pinstate#2]=0
    // Manual mode is not active, so we must act on the motion sensor
    timerSet,1,1800 // 30 minutes timer
    let,1,1 // Set variable #1 to indicate we had some motion detected  
    asyncevent,UpdateLightIntensity=[serre_lichtmeting#Analog]
  endif
endon

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#46 Post by Dick60 » 22 Feb 2023, 17:57

In answere of your last message, that is correct. If manual mode is active, no PIR activity will turn on the leds. Perhaps some explanation, GPIO 13 is a relay that turn on the external power source for the ledstrip, G{IO 10 is a switch which is an indication if the ledstrip is On or Off.
Because I do not see any PWM commands in the log, could something being wrong in this row "PWM,16,[int#1]"?

User avatar
Ath
Normal user
Posts: 3488
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Rules again follow-up

#47 Post by Ath » 22 Feb 2023, 19:52

Dick60 wrote: 22 Feb 2023, 17:57 Because I do not see any PWM commands in the log, could something being wrong in this row "PWM,16,[int#1]"?
For [serre_lichtmeting#Analog] 66 the calculated PWM value [int#1] is 40, is that enough to light up the LED strip?

You could add a couple of LogEntry lines in the rule to see what is the expected value

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
    LogEntry,"Intensity: %eventvalue1% pwm: n/a gpio: 0  A"
  else
    // Either manual mode is active, or recently detected motion
    If %eventvalue1%<60
      // Option #1 Will turn off 10 and 13 because LightIntensity <60
      GPIO,10,0
      GPIO,13,0
      PWM,16,00
      LogEntry,"Intensity: %eventvalue1% pwm: 0 gpio: 0  B"
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
      GPIO,10,1  // Option 2 Will turn on 10 and 13 because LightIntensity >= 60
      GPIO,13,1
      LogEntry,"Intensity: %eventvalue1% pwm: [int#1] gpio: 1  C"
    Else
      PWM,16,1000
      GPIO,10,1  // Option 3 Will turn on 10 and 13 because LightIntensity >= 450
      GPIO,13,1
      LogEntry,"Intensity: %eventvalue1% pwm: 1000 gpio: 1  D"
    Endif
  endif
endon
(Marked the LogEntry lines with A..D to easily find the active code path)
/Ton (PayPal.me)

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#48 Post by Dick60 » 22 Feb 2023, 20:05

The log could help me to get more info what is happening. But still, I just copied the acual log serre_lichtmeting#Analog=483 and after this message I expect more like changing gpio10 and 13 and a PMW action . But nothing. I find that strange. I am not in coding but what I can extract from the script that something should happen.

Extra info, None of the Log rules ypu added to get a log entry, what is happening. None of them pops up in my log.

User avatar
Ath
Normal user
Posts: 3488
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Rules again follow-up

#49 Post by Ath » 22 Feb 2023, 20:58

Can you copy all rules as you currently have on your ESP in a code block, here?
/Ton (PayPal.me)

Dick60
Normal user
Posts: 243
Joined: 11 Feb 2018, 17:35
Location: The Netherlands

Re: Rules again follow-up

#50 Post by Dick60 » 22 Feb 2023, 21:15

There is no more scripting in my project so this it is but I was playing around and found this part what I changed and after that changed it worked after movement:

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
    LogEntry,"Intensity: %eventvalue1% pwm: n/a gpio: 0  A"
  else
But this change is not clear for me why it worked in that case, It is not logic for me.

This is the one you add the loging.

Code: Select all

on UpdateLightIntensity do
  if [Plugin#GPIO#Pinstate#2]=0 and [int#1]=0
    // Manual mode is not active, and no recent motion detected.
    // So we must turn off the lights.
    gpio,10,0
    gpio,13,0
    LogEntry,"Intensity: %eventvalue1% pwm: n/a gpio: 0  A"
  else
    // Either manual mode is active, or recently detected motion
    If %eventvalue1%<60
      // Option #1 Will turn off 10 and 13 because LightIntensity <60
      GPIO,10,0
      GPIO,13,0
      PWM,16,00
      LogEntry,"Intensity: %eventvalue1% pwm: 0 gpio: 0  B"
    Elseif %eventvalue1%<450
      let,1,2.5*%eventvalue1% - 125 // Approximation of the curve
      PWM,16,[int#1]
      GPIO,10,1  // Option 2 Will turn on 10 and 13 because LightIntensity >= 60
      GPIO,13,1
      LogEntry,"Intensity: %eventvalue1% pwm: [int#1] gpio: 1  C"
    Else
      PWM,16,1000
      GPIO,10,1  // Option 3 Will turn on 10 and 13 because LightIntensity >= 450
      GPIO,13,1
      LogEntry,"Intensity: %eventvalue1% pwm: 1000 gpio: 1  D"
    Endif
  endif
endon

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 44 guests