Do an action between two hours with RTC DS3231

Dear friends,
I am trying to develop a sketch to work with sensors (soil humidity, conductivity, ...) due to this device will operate with a solar panel, I included the narcoleptic library with a light sensor to save energy during the night hours and additionally, an output pin to supply voltage to the sensors during a specific moment of the time and reduce the current consumption.
But I am struggling with the definition of the "now.hour" to fix the desired time lapse.
I would appreciate so much your help.

Regards
Nicolás

#include <Narcoleptic.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"


char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

int sensorPin = A0;    // select the input pin for the sensor
int sensorValue = 0;  // variable to store the value coming from the sensor
const int ledPin = 13;
const int inputPin = 2; //sensorlight switch
const int outputPin = 8; // sensor power source
int initial_hour = 17;
int finish_hour = 18;


void setup ()
{
  pinMode (ledPin, OUTPUT);
  pinMode (inputPin, INPUT_PULLUP);
  pinMode (outputPin, OUTPUT);
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();
}

void energySaving ()
{
  if (digitalRead(inputPin) == LOW)
  {
    DateTime now = rtc.now(); //get the current date-time

    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.date(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(' ');
    Serial.print(weekDay[now.dayOfWeek()]);
    Serial.println();

    digitalWrite(13, HIGH);
    delay(200);

    digitalWrite(13, LOW);
    delay(5000);

  }
}
  void onOffSensor ()

  {
    while (initial_hour >= (now.hour) <= finish_hour)
    {
    digitalWrite(8, HIGH);
    delay (50);
    }
  }

sketch_sensor7.1.ino (1.37 KB)

You define a variable called now in the energySaving() function. That variable goes out of scope when the function ends.

In onOffSensor, you seem to think that the variable still exists, and that the hour() method has somehow morphed into a property. Neither is true.

Thanks PaulS