ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 7
VIEWS: 103
BACK
REPLY TO THIS POST
HansaG
27 Nov 2024
Actions
Hi there,

How will I know if my actions have been executed?
Is there a trace log or a report view for it?

I have two actions (see attached images),
1. To notify if the device is offline for a certain period.
2. To switch off the lights (if they are on) during the daytime between 6:30 AM and 6:30 PM.

@Gizmo, thanks so much for what you have done here. So far I'm loving it.

Thanks


Attached images
Gizmo
27 Nov 2024
ACCEPTED

Hi dude,

uhm, right, let's see (this is going to be a loooong read):

- Unfortunately there is no log for the actions, it would take petabytes of storage per server if we were to log every users action execution. I have some actions that execute every 10sec to create a sort of mesh network so 3 devices are working as a single brain (one primary and two as remote satellite sensors) and that alone generates around 800,000 action calls per month! :-)

- But...why don't you enable the log on that outdoor light switch component? Just set it to log like 1 hour, you could probably have a log that spans a month or more on that component and you can check it any time. That would show you if the device is getting the instructions.

- If possible I would change the way you use the action. At the moment as you have figured you can't have the same device component as a trigger and target. That's simply to prevent infinite loop conditions (click/clack/click/clack....) that could get ChipChop engine to ban your device :-)

The way I do it involves a secondary device, it can be anything, any additional device that sends a regular heartbeat (if cost is an issue I'll send you one...have a ton of ESPs asking to be used :-)
So, the trick is to have an action that is "guaranteed" to execute on "every" heartbeat, that means that whatever the status of that secondary device "will be" you have to match and that usually means testing for a negative impossible condition!

For example

    - I use a temperature & humidity esp for that purpose, the action checks if the temperature is not != -1000 which it will never be so the IF statement will always be correct and the action will execute on every heartbeat
    or
    - you create a fake component on that secondary device, let's call it "pulse" that only do ChipChop.updateStatus("pulse",1)
    The action checks if pulse == 1 and executes on every heartbeat

    Then you simply target your outdoor lights switch 1 > light-01 to be OFF and same time/day settings
    The good thing doing it this way is that the action will first check if the outdoor lights switch 1 > light-01 == ON and only execute once in that time period (assuming it get's the status OFF confirmed through the heartbeat), that reduces a flood of commands sent to your light switch. Also, you don't have to add anything extra in your code to check for command target "any"

Another option

This is a more elegant option that I use a lot and will also work for some period of time even if the device looses wifi so hear me out.

    - If you've set the device timezone correctly the ChipChop library gets a correct timestamp back on every heartbeat and synchronises its internal clock.

    - You need some way to save a little "settings" file on the esp, you can use the PreferencesManager plugin from the ChipChop Code Builder or you can do it yourself with LittleFS or something like that

    - Create two extra components (time1_off, time2_off) and program the logic that when you send a command to change those times to save those values and set up some off_time variables
    and also on every restart to load the strings from the file and again adjust those variables
    That way you can re-program the schedule whenever you want without even using actions

    - In the loop you check what the time is from the ChipChop library and if it falls within your off schedule you switch the light off

Here is a quick and dirty example, you would need to generate a code templkate through the Code Builder first just to get the structure with the PreferencesManager and plugin manager




int light_pin = 2;
String light_swicth_1_state = "OFF";

void ChipChop_onCommandReceived(String target_component,String command_value, String command_source, int command_age){

//this would be using two components added to the device in the Dev Console
// the value will be in "minute of the day" format (hour * 60 + minute)
if(target_component == "off_1" || target_component == "off_2"){
int time = command_value.toInt();
PrefsManager.save("schedule",target_component,time);
}

if(target_component == "light-01"){
if(command_value == "ON"){
digitalWrite(light_pin, HIGH);
}else{
digitalWrite(light_pin, LOW);
}

light_swicth_1_state = command_value;
ChipChop.updateStatus("light-01",command_value);

}

}


//default setting that will potentially get overwritten but we need something to start
int time1_off = (6 * 60) + 30; //<< cheeky trick, convert the time to a "minute of the day"
int time2_off = (18 * 60) + 30;

void setup(){

///bla blah Wifi etc

String saved_off_1 = PrefsManager.get("schedule","off_1");
time1_off = saved_off_1.toInt();
String saved_off_2 = PrefsManager.get("schedule","off_2");
time2_off = saved_off_2.toInt();


}
void loop(){

if(ChipChop.Date.timestamp != 0){ // << will be zero before the first heartbeat is sent
int currTime = (ChipChop.Date.hour * 60) + ChipChop.Date.minute; //convert to minute of the day

if(currTime >= time1_off && currTime <= time2_off && light_swicth_1_state == "ON"){
//switch your light off
digitalWrite(light_pin, LOW);
ChipChop.updateStatus("light-01","OFF");
}
}


}





And now you are effectively re-programming your device without re-compiling or hardcoding or scripting.

This will also work if wifi goes off as once the ChipChop lib sends the first heartbeat and gets its first timestamp it keeps updating its internal clock every second up to the max integer (when millis() rollover to zero) or the device is restarted
The first thing I've implemented was the timezone timestamp (still have nightmares) so you don't need an RTC or NTP server and then have to do all timezone offsets, day light saving etc crap. ChipChop does it for you automatically (I've built a dedicated server that recalculates all timezones every minute, was cheaper than paying for API calls to some of the online services (frigging expensive stuff :-)


What do you think, would some of this work?


G
foo-bar
27 Nov 2024

@G

you could have mentioned concepts like "day-minute" six months ago 🤌😖☠️

days I've spent writing if(hour > xx && minute > xx && hour2 < xx && minute2 < xx) and saving as strings and splitting by ":"

I don't think we can be friends anymore 😥🖕

p.s. still can't work out the algo when time is between two days, maybe you can redeem yourself ;)
Gizmo
27 Nov 2024

Oh my dear @poo-bar

I've missed the class when they were teaching mind reading and if I did know you were messing with time code I'd be hiding as I hate the stuff.

Really don't get what confuses you



if( alarm_hour_1 < alarm_hour_2 || alarm_hour_1 == alarm_hour_2 ){ // the alarm time we are monitoring is inside today

        if( !(current_hour_minutes >= alarm_hour_minutes_1 && current_hour_minutes <= alarm_hour_minutes_2) ){

                // the current time is not within the alarm time range

        }else{
                // the current time is inside the alarm time
        }

}else if(alarm_hour_1 > alarm_hour_2){ // the alarm time we are monitoring is between today & tomorrow

        if( !(curr_hour_minutes >= alarm_hour_minutes_1 || curr_hour_minutes <= alarm_hour_minutes_2)){

            // the current time is not within the alarm time range
                
        }else{

            // the current time is inside the alarm time
        }

}




Enjoy !-)

foo-bar
28 Nov 2024

still don't get

}else if(alarm_hour_1 > alarm_hour_2){ // the alarm time we are monitoring is between today & tomorrow

makes no sense
Gizmo
28 Nov 2024

Lord, give me strength!

If we are checking if some time falls between 2PM and 4AM that's overnight between 14:00h and 04:00h, right
DON'T think in AM/PM it's ambiguous, you need to use the 24h clock !!!

That means that alarm_hour_1 == 22 and alarm_hour_2 = 4 so if hour_1 is greater than hour_2 the alarm time is between two days, only in that situation hour_1 can be greater than hour_2

- h1 = 10 and h2 = 16 - same day
- h1 = 12 and h2 = 12 - same day again
- h1 = 13 and h2 = 12 - trick question but h2 is in the next day
- h1 = 22 and h2 = 4 - h2 next day

got it?
HansaG
05 Dec 2024

I appreciate you taking the time to explain in such detail. I haven't seen anyone do that in other popular forums. Thanks again for the detailed explanation, Gizmo.
Gizmo
05 Dec 2024

Hi Hansa,

It's a pleasure, ChipChop is a big system and it's impossible to figure out without assistance every trick that can be used (it even surprises myself sometimes and I've written the damn thing :-)

Enjoy it and just ask if you need anything

G