Why don't my rules work?

Hi all,
I hope you can all help me with some debugging of the rules I’ve set because they’re not working as I think they should.

First, here are my things:
image
image
image
The two heater things are Wemos D1 Mini running with WebThing-Arduino to control a pilot wire to the heaters’ own mode selector.
image This one is virtual.
And I have the DateTime adapter installed too.

Now, my rules and the problems I have:

  1. If DateTime Hour is greater than 7, DateTime Hour is less than 23, and Heating system is on, set Ground Floor Heater 2 Setting to 3 and set Ground Floor Heater 1 Setting to 3.
  2. If DateTime Hour is less than 7, DateTime Hour is greater than 23, and Heating system is on, set Ground Floor Heater 1 Setting to 2 and set Ground Floor Heater 2 Setting to 2.
  3. If Heating system is not on, set Ground Floor Heater 1 Setting to 0 and set Ground Floor Heater 2 Setting to 0.

To my understanding these rules should work like a standard heater control timer: There is a master switch, if it’s off then the heating is completely off, if it’s on then the heating follows the timer rules. The master switch works to turn the heating off, but unfortunately the timer rules do not - when the time passes 23 or 7 then the heater settings should change, but they don’t. If I use the master switch to turn off the heating then turn it back on it should set the heating to the correct setting for the time of day, but it doesn’t the heating stays turned off.

I have also tried replacing If in rules 1 and 2 with While and this has different results. The on/off switch does work as expected and when turned on the heating goes to the setting defined by rules 1 and 2. However, at seemingly random times during the day the switch turns itself off! This then turns off the heating and the switch does not (AFAICT) turn itself back on. I have no rules to control the switch itself, so I cannot understand why it does this.

I tried using the logging experiment to debug, but as I mentioned in IRC the logs do not reflect the actual states of the heaters or the switch. Looking at the internal logs isn’t much help because they don’t tell me the state of the switch and only tells me the value of the heaters setting when I access the webapp, not when they change state.

So, what suggestions does anyone have for how to fix my rules and make it work? If I can’t make these rules work I’ll have to go back to the very simple rules I had before which at 7:00 and 23:00 changed the heater setting, but only at those specific times (no ranges used) and with a master switch.

Thanks :slight_smile:

The first thing I’m seeing is that “If DateTime Hour is less than 7, DateTime Hour is greater than 23, and Heating system is on, set Ground Floor Heater 1 Setting to 2 and set Ground Floor Heater 2 Setting to 2.” will never trigger since the hour never both less than 7 and greater than 23.

I think the most rock-solid way of designing the rules is to have the heating system change at midnight instead of 23:00. This works around the lack of advanced logic. The alternative would be to create a new virtual on/off switch for “daylight” or equivalent and then set its on/off based on “While DateTime Hour is greater than 7 and DateTime hour is less than 23”.

Here’s what I’d suggest (heater active between 00:00 and 07:59):

  1. If DateTime Hour is greater than 7 and Heating system is on, set Ground Floor Heater 2 Setting to 3 and set Ground Floor Heater 1 Setting to 3.
  2. If DateTime Hour is less than 7 and Heating system is on, set Ground Floor Heater 1 Setting to 2 and set Ground Floor Heater 2 Setting to 2.
  3. If Heating system is not on, set Ground Floor Heater 1 Setting to 0 and set Ground Floor Heater 2 Setting to 0.

This gets a bit into the internal workings of the rules engine but the expected behavior over the course of a day is

  • 01:00: DateTime hour is less than 7 and heating system is on, set to 2 and 2
  • 08:00: DateTime hour is greater than 7 and heating system is on, set to 3 and 3
  • 09:15: You turn the heating system off (maybe it’s especially sunny), “if heating system is not on” activates, set to 0 and 0
  • 10:00: DateTime hour is greater than 7 and heating system is off, nothing happens
  • 10:50: It’s cold, you turn the heating system on
  • 11:00: DateTime hour is greater than 7 and heating system is on, set to the 3 and 3

If you’re not averse to a bit of coding and setup, the pywot rule system is a nice way to create arbitrarily complex logic: https://github.com/twobraids/pywot/tree/master/demo/rule_system

I’ll be trying to replicate the issue you were describing in IRC locally with my own time-based setup. If you could file an issue at https://github.com/mozilla-iot/gateway/issues it would help me try to track that down.

1 Like

Hi James, thanks for your reply. Doh! How could I miss such obvious logical reason it didn’t work! Obviously I’m out of practice.

Good suggestion about the virtual switch for “daytime”, I’m using that for now, but I need to get around to implementing my heater levels as enums and then create a central mode selector with enums.

I intend to try out Micropython webthing at some point, but maybe I’ll try normal Python with that pywot which you linked. Thanks for the tip!

As for the logs experiment bug… I will leave the system running for a few more days without tinkering so I can try and give you some definitive feedback on that.

More bizarre for me was that the On/Off virtual switch turned off completely by itself. I had no rules to trigger this and I couldn’t find any record of why this happened in the internal logs. Weird.

Cheers :smile:

EDIT: your example of timings suggests that the rules engine only processes once per hour, is that right? Or were you just using hourly markers to illustrate time passing?

Sounds good, thanks! Yeah, the on/off switch turning off is bizarre.

The hour markers I used in the example are based on my incorrect assumption that the rules would only trigger on DateTime property changes. Every rule should react instantly without needing the hour to change. If the rules engine is requiring an hour change to update before taking an action it’s a bug and should be reported.

1 Like

Hi @hobinjk, I commented on your issue: https://github.com/mozilla-iot/gateway/issues/1829
I think this could be the reason the logs were showing me the wrong states.
Cheers :slight_smile: