Configurable time trigger

Got something :wink:

  • Flow 1 - Convert departure time to numeric time
    (To be able to enter the departure time as normal time 00:00 format)

.

  • Script:
// convertTime2NumericTime.js - Convert time to Numeric time (like: 15:30h -> 1550)

let timeIn = args[0]
let timeNumeric = 0;

if (timeIn == '' || timeIn == undefined) {
    console.log ('Missing argument');
    return(false);
} else {
  // If the hour is 1 - 9, add a leading 0 so time is formatted as f.i. 09:30
  var charCount = Array.from(timeIn.toString()).length;
  if (charCount < 5) {
    timeIn = "0" + timeIn;
  }

  // Split the hours and minutes
  hours = timeIn.slice(0,2);
  minutes = timeIn.toString().slice(-2);

  // Converting from minutes to Numeric hours
  hoursNumeric = Math.round(minutes / 0.6 ,0);
    // In case result = 0 -> make sure minutes are displayed as 00
  if (hoursNumeric < "1") {
    hoursNumeric = hoursNumeric + "0";
  }

  timeNumeric = hours + hoursNumeric;
  
  // Update Logics stringVar AB.timeNumeric
 await Homey.logic.updateVariable({id: 'Variable_ID_here', variable: { value: timeNumeric } });

}

return(true);

.

  • Flow 2 - When numeric time “Start Charging time” changed, convert it to normal time format 00:00

.

  • Script:
// convertNumeric2NormalTime.js - Convert numeric time to normal time (like: '1550' -> 15:30)

let timeNumeric = args[0]

// make sure timeNumeric it has no digits, round to zero digits
timeNumeric = Math.round(timeNumeric ,0)

let timeNormal = 0;

if (timeNumeric == '' || timeNumeric == undefined) {
    console.log ('Missing argument');
    return(false);
} else {
  // If the hour is 1 - 9, add a leading 0
  var charCountHrs = Array.from(timeNumeric.toString()).length;
  if (charCountHrs < 4) {
    timeNumeric = "0" + timeNumeric;
  }
  // Split the hours and numeric hours
  hours = timeNumeric.toString().slice(0,2);
  hoursNumeric = (timeNumeric).toString().slice(-2);
  // change f.i. 25 to 0.25
  hoursNumeric = hoursNumeric / 100;
  // converting from Numeric hours to minutes
  minutes = Math.round(hoursNumeric * 60 ,0);

  // In case result = 1 digit, then make sure minutes are displayed as 0x
  if (minutes < 10) {
    minutes = "0" + minutes;
  }

  timeNormal = hours + ":" + minutes;
 
  // Update Logics stringVar AB.timeNormal
  await Homey.logic.updateVariable({id: 'Variable_ID_here', variable: { value: timeNormal } });

}

return(true);

.

  • Variables:
    The ones starting with “A.” are the ones to edit or readout
    Screenshot from 2022-06-13 04-06-13
    .
    The ones starting with “AB.” are the other used Vars:
    Screenshot from 2022-06-13 04-06-54

.

  • Finding a variable ID & its details:
// Replace MyVar with your variable name
let varName = "MyVar"

const logicVars = await Homey.logic.getVariables()
for (var iLogicVar in logicVars){
  if (logicVars[iLogicVar].name == varName)
  log("Variable: " + logicVars[iLogicVar].name + "\nType:\t  " + logicVars[iLogicVar].type + "\nValue:\t  " + logicVars[iLogicVar].value + "\nID:\t  " + logicVars[iLogicVar].id)
}

1 Like