JavaScript with multiple triggers. Is it possible to determine which in action:script?

Consider this rule with 3 cron triggers.
Is it possible to determine which one(s) triggered?

configuration: {}
triggers:

  • id: “1”
    configuration:
    cronExpression: “* * * * * ? *”
    type: timer.GenericCronTrigger
  • id: “2”
    configuration:
    cronExpression: 0 0 * * * ? *
    type: timer.GenericCronTrigger
  • id: “3”
    configuration:
    cronExpression: 0 0 0 * * ? *
    type: timer.GenericCronTrigger
    conditions: []
    actions:
  • inputs: {}
    id: “4”
    configuration:
    type: application/javascript;version=ECMAScript-2021
    script: console.log(“Hi there!”)
    type: script.ScriptAction

I could not figure it out, and ended up with a 1s cron trigger and doing this:

var today = new Date

function isHourTick () {
  return (today.getMinutes() == 0 && today.getSeconds() == 0)
}

As you found, the answer is there ya nothing built in to tell you which triggered the rule. You can tell the difference between different types of triggers by checking to see if event is defined as be what members of event are defined, but that won’t help determine the difference between multiple Cron triggers.

Though one wonders if you are doing something different, why not use a different rule?

I’m writing a rule to calculate my house power consumption.
I have had an Item with my meters instantaneous KW consumption, updated every 2s, that I now want to do something useful with, incentive growing since electrical hourly spot power prices here in the southern part of Norway has almost quadrupled this nov/dec.

So, I want to keep track of my consumption. Already having the KWs value, I want the calculate the KWminute/hour/day/week and month values as well. Just seemed natural for me to do it in one rule.

I also have a scraper providing next day hourly spot prices, so I can automatically shut off heating during a particularly expensive period. (my electrical bill for november was north of $550)

It is not bad to raise the power cost awareness, so a HABpanel screen is next I guess. :slight_smile:

One unnatural side effect though is as configured, at the top of the hour the rule is triggered three times in a row, once for each cron trigger.

And I’m not sure what the every second trigger does for you. It might make more sense to change that to trigger on changes to your power Item instead.

Are you using Persistence to do the calculation? If not I suggested looking into that.

For simplicity sake, I’d probably split this into multiple rules. For UI rules, I find making rules that do one thing to be easier to maintain in the long run. If there is a lot of shared code, is out them into a library. I also make heavy use of conditions so by the time the script action runs all the error checking had been done.

Indeed, I’m running InfluxDB.

I ended up peristing kwh/min and derives the rest from that:
I was not able to find any good examples for the history.sumSince() on this forum, so someone might find that useful: My hourly cost calculations are still WIP.
Script called by cron every 1s.

    var today = new Date
    var instPower = parseInt(items.getItem("GTV7PowerConsumption").state, 10)
    var kwMinute

    var hasTickedMinutes
    var hourlyPrice

    if (isNaN(kwMinute)) {
      console.log("Rule starting");
      kwMinute = 0;
      kwHour = 0;
      hasTickedMinutes = false
      hourlyPrice = 0.0
    }

    function isMinuteTick () {
      return (today.getSeconds() == 0)
    }

    function isHourTick () {
      return (today.getMinutes() == 0 && today.getSeconds() == 4) // delay a bit so that the last persisted value becomes part of the sum
    }


    function isDayTick () {
      return (today.getHours() == 0 && today.getMinutes() == 0 && today.getSeconds() == 4) // delay a bit so that the last persisted value becomes part of the sum
    }

    function isWeekTick () {
      return (today.getDay() == 1 /* monday */ && isDayTick())
    }

    function isMonthTick () {
      return (today.getDate() == 1 && isDayTick())
    }

    function isYearTick () {
      return (today.getMonth() == 0 && isMonthTick()) // month 0 is January
    }

    function calcHourlyConsumptionCost(hourlyConsumption) {  
      // fetch NordPool hourly price for this hour interval
      switch (today.getHours()) {
        case 0:
          hourlyPrice = parseFloat(items.getItem("toDay24_01").state)
          break
        case 1:
          hourlyPrice = parseFloat(items.getItem("toDay01_02").state)
          break
        case 2:
          hourlyPrice = parseFloat(items.getItem("toDay02_03").state)
          break
        case 3:
          hourlyPrice = parseFloat(items.getItem("toDay03_04").state)
          break
        case 4:
          hourlyPrice = parseFloat(items.getItem("toDay04_05").state)
          break
        case 5:
          hourlyPrice = parseFloat(items.getItem("toDay05_06").state)
          break
        case 6:
          hourlyPrice = parseFloat(items.getItem("toDay06_07").state)
          break
        case 7:
          hourlyPrice = parseFloat(items.getItem("toDay07_08").state)
          break
        case 8:
          hourlyPrice = parseFloat(items.getItem("toDay08_09").state)
          break
        case 9:
          hourlyPrice = parseFloat(items.getItem("toDay09_10").state)
          break
        case 10:
          hourlyPrice = parseFloat(items.getItem("toDay10_11").state)
          break
        case 11:
          hourlyPrice = parseFloat(items.getItem("toDay11_12").state)
          break
        case 12:
          hourlyPrice = parseFloat(items.getItem("toDay12_13").state)
          break
        case 13:
          hourlyPrice = parseFloat(items.getItem("toDay13_14").state)
          break
        case 14:
          hourlyPrice = parseFloat(items.getItem("toDay14_15").state)
          break
        case 15:
          hourlyPrice = parseFloat(items.getItem("toDay15_16").state)
          break
        case 16:
          hourlyPrice = parseFloat(items.getItem("toDay16_17").state)
          break
        case 17:
          hourlyPrice = parseFloat(items.getItem("toDay17_18").state)
          break
        case 18:
          hourlyPrice = parseFloat(items.getItem("toDay18_19").state)
          break
        case 19:
          hourlyPrice = parseFloat(items.getItem("toDay19_20").state)
          break
        case 20:
          hourlyPrice = parseFloat(items.getItem("toDay20_21").state)
          break
        case 21:
          hourlyPrice = parseFloat(items.getItem("toDay21_22").state)
          break
        case 22:
          hourlyPrice = parseFloat(items.getItem("toDay22_23").state)
          break
        case 23:
          hourlyPrice = parseFloat(items.getItem("toDay23_24").state)
          break
        default:
          hourlyPrice = 0.0
          break      
      }
      console.log("hourlyPrice=(NOK) " + (hourlyPrice*hourlyConsumption)/1000)
    }

    if (isMinuteTick()) {
      if (!hasTickedMinutes) { // sample a full interval
        hasTickedMinutes = true
      } else {
        kwMinute /= 60    
        //console.log("kWmin= " + kwMinute);
        items.getItem("kWHminuteConsumption").postUpdate(kwMinute)
      }
        kwMinute = 0
    } else {
        kwMinute += instPower
    }

    if (isHourTick()) {  
        var kwPeriod = items.getItem("kWHminuteConsumption").history.sumSince(new Date(new Date().setHours(new Date().getHours()-1))) / 60
        console.log("kWh= " + kwPeriod);
        calcHourlyConsumptionCost(kwPeriod)
        items.getItem("kWHhourlyConsumption").postUpdate(kwPeriod)
    }

    if (isDayTick()) {
        var kwPeriod = items.getItem("kWHhourlyConsumption").history.sumSince(new Date(new Date().setDate(new Date().getDate()-1)))    
        console.log("kwhday=" + kwPeriod)
        items.getItem("kWHdaylyConsumption").postUpdate(kwPeriod)
    }

    if (isWeekTick()) {
        var kwPeriod = items.getItem("kWHdaylyConsumption").history.sumSince(new Date(new Date().setDate(new Date().getDate()-7)))
        console.log("kwhday=" + kwPeriod)
        items.getItem("kWHweeklyConsumption").postUpdate(kwPeriod)
    }

    if (isMonthTick()) {
        var kwPeriod = items.getItem("kWHhourlyConsumption").history.sumSince(new Date(new Date().setMonth(new Date().getMonth()-1)))
        console.log("kwhmonth=" + kwPeriod)
        items.getItem("kWHmonthlyConsumption").postUpdate(kwPeriod)
    }

    if (isYearTick()) {
        var kwPeriod = items.getItem("kWHhourlyConsumption").history.sumSince(new Date(new Date().setMonth(new Date().getMonth()-12)))
        console.log("kwhyear=" + kwPeriod)
        items.getItem("kWHyearlyConsumption").postUpdate(kwPeriod)
    }