Thing Variables Falling Through - Nano 33 IoT

Hi.
I got very basic knowledge of Arduino, and just started with Nano 33 IoT.
I am trying to control a relay with Pin 8. Generally, I seem to have achieved something; Dashboard buttons controlling the Relay On and Off. However, when first connecting the Nano to USB power, I always get those Thing variables (ex. Relay On and Relay Off) which reside outside Loop, to execute once, in the order they appear. Once that happens, I can correctly control through the dashboard.
Sounds quite silly, but I could not figure out the issue. Any one experienced something like this before? Thanks.

Please post the full sketch that you have a problem with

Thanks for looking into this. Some more info, if missed in initial post:

  • If I delete RelayOff only, the relay remains On.
  • If I delete RelayOff and RelayOn, then the relay remains Off (setup initialises pin with LOW)
  • If I digitalwrite to pin a HIGH in setup, with RelayOff/RelayOn deleted, then relay remains On
  • With no deletions, exactly as the code below, then when I power up the board, I physically see relay going On, then a minimal pause (feels similar to the delay below), then relay goes off. If I link variables to Dashboard, each a push button, pressing each button changes the status of the relay correctly. Board seems to continue to function properly.

Here is the code:

  • for improved readability, code in next post *

How did I know that you would do that ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Apologies @UKHeliBob There was no intention to frustrate you or anyone in the community.

Here is the code again. I removed some of the standard commentary to make it shorter. As always, many thanks for any support.

/* 
  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  CloudSwitch relayOff;
  CloudSwitch relayOn;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

//int buttonState = 0;
int buttonPin = 8; 

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 
  
  pinMode(buttonPin,OUTPUT);
  digitalWrite(buttonPin,LOW);
  
  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
}

void onRelayOnChange()  {
  delay(1000);
  digitalWrite(buttonPin,HIGH);
}

void onRelayOffChange()  {
  delay(1000);
  digitalWrite(buttonPin,LOW);
}