Rules - begginer

  • Platform information:
    • Hardware: bannapi pro
    • OS: Armbian 21.02.2 Bionic with Linux 5.10.16-sunxi
    • Java Runtime Environment: (build 11.0.10+9-Ubuntu-0ubuntu1.18.04)
    • openHAB version: openHAB 3.0.1
  • Issue of the topic: rules not working

Hello,
I have a esp2688 that I am monitoring the serial and its serial is listening for mqtt msgs and printing them to serial. The messages it is listening to is topic -> pump0, valve0, valve1,…

I have mosquito broker installed and working and can send the messages to esp2688 either from linux terminal or openhab. I have setup and configured openhab (apt install) for testing. I can flip switches to send command start/stop to topic pump0 and it displays in the esp2866 serial monitor, example [pump0] start, [valve3] close… Great! seems to work great. When I add logic rules in openhab nothing happens. example a rule to send “start” to topic pump0 only if valve0 is open. I can still flip switch in openhab and rules get ignored.

Switches work in openhab but the rules are never followed, even if I keep it simple and put “only if between 00:00 - 00:01” in the rules. To me the switch should not work because its outside the allowed time. What am I doing wrong? Should the logic be on code in esp2866? Been at this for a couple days.

Welcome! Please show us your rules so that we can help identify what’s wrong with them.

triggers:

  • id: “1”
    configuration:
    itemName: valve0
    command: open
    type: core.ItemCommandTrigger
    conditions: []
    actions:
  • inputs: {}
    id: “2”
    configuration:
    itemName: valve0
    command: open
    type: core.ItemCommandAction
  • inputs: {}
    id: “3”
    configuration:
    itemName: cistern_pump0
    command: start
    type: core.ItemCommandAction

When i flipped the switch to on in openhab, valve0 opened but pump0 never started according to serial monitor.

Can you also show us the entries from your openhab log? I should have asked for that as well. Your serial monitor doesn’t matter if the commands are not being sent by openhab.

sure, I cleared the logs, I changed the rules to:
1)when valve0 opens, then open valve0 and valve1.

Then on openhab I go to items → valve0 → turn it to on (open) (valve1 switch is closed)

esp2688 only gets “[valve0] open”

2021-03-02 07:29:55.923 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'valve0' received command ON
2021-03-02 07:29:55.928 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'valve0' predicted to become ON
2021-03-02 07:29:55.941 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'valve0' changed from OFF to ON
2021-03-02 07:29:58.521 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'valve0' received command OFF
2021-03-02 07:29:58.527 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'valve0' predicted to become OFF
2021-03-02 07:29:58.543 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'valve0' changed from ON to OFF
re

openhab.log still empty after I cleared it.

I’m just wondering if this is expected behavior on fresh install or if something went wrong.
The rules are supposed to be easy to use right?
I may put openhab on a raspi 4 using the openhab image file. I just thought the rules should effect the items so I person could make logic rules. Like I said though I am very new to home automation.
I could be misunderstanding something.

one last thing, after I installed openhab I had the esp2866 send hello world to some topic, I was able to set a trigger mqtt and add a rule that would send a msg on openhab and reply back to esp2866. That did work.

Not really it can get complicated quickly

Rules like that will not change the way another rule or mainUI will work.
eg if you get openHAB rule to do something like turn an item on then you goto sitemap and turn it off it will turn off.

A little advice from me about how you view and setup your automation and my philosophy. (I do industrial automation for a living)

You have an esp8266, get it to do as much of the automation as you can like when the valve turns on run pump for 5000ms

This way if something happens in your control path like Mosquitto or Openhab you thing will still work. Like a pushbutton wired to 8266 doesn’t have to turn on pump though talking to openHAB.
You program can still report the state of the pump to MQTT when the button is pushed.

Now for you rule when you press the blue play button in you pick dose the command start show up in MQTT

I use http://mqtt-explorer.com/

Or can you share your cistern_pump0 thing config

Thanks for the tip. I will have to rethink what my approach is then. I had originally wanted the Arduino or esp2866 to be the dummy operating relay switches while the brain (logic) is carried out in openhab. I think it is most nice to be able to open a web browser and flip switches. Most of the reason I wanted to do this is I don’t have time to learn c++ or other language right now. Just wanted to speed through a irrigation system in a couple days. lol
Do you think I should try some other software besides openhab? I was going to try home automation once I am ready to throw in the towel here. Last resort is just getting a basic sketch to work.

esp8622 is subscribed to all the topics. I can use linux terminal and type "mosquitto_pub -h 192.168.44.200 -t valve4 -m “close” and the serial of esp8622 will print it. It seems like the in openhab the rules do not apply to the “items”. Yes when I toggle a item swith the mqtt command goes out. That is working very nice. If I have a rule saying to also open valve2, valve3, valve4 when valve0 is opened the command does not go out.


//#include <NewPingESP8266.h> // Include  library used for measuring the distance using HC-SR 06 sonar sensor
#include <ESP8266WiFi.h> // The only wifi module you need it appears
#include <PubSubClient.h> // This library has the mqtt stuff needed

const char* ssid = "####";
const char* password = "#####";
const char* mqtt_server = "192.168.44.200";


WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("out", "hello world");
      // ... and resubscribe
      client.subscribe("in");
      client.subscribe("pump0");
      client.subscribe("valve0");
      client.subscribe("valve1");
      client.subscribe("valve2");
      client.subscribe("valve3");
      client.subscribe("valve4");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, "hello world", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("out", msg);
  }
}

Its just a a nothing right now, I just am listening to serial for messages to see whats going on. Your idea sounds good though. I may just do what you recommend.

Your approach is how most people would do it. I just think setting it up to always work is worth my effort. Each to their own if you want to set it up either way I will help.

You could use News - Tasmota to run all of your devices and you can also add your own rules. It uses its own schema for MQTT topics but that is easy to learn.

Put TASMOTA on 8266 using maybe GitHub - tasmota/tasmotizer: ESP... The time has come to... Tasmotize!
install mqtt explorer as an easy tool to grasp what is going on.

you send commands to the cmnd topic and the state will be shown on a different one

No, for non-technical people rules are going to be the hardest part. We have done a lot to make rules easier and are doing more all the time, but it’s going to be programming no matter what and programming is hard.

Assuming that 00:00-00:01 is a time that means the rule will only be allowed to run between midnight and one minute after midnight. I’m pretty sure that’s not what you want.

As James points out, that only controls the execution of the rule. It doesn’t do anything about the Items. You can update or command the Item at any time in a variety of ways such as from the sitemap.

The But only if… only prevents the things defined in the “Then” section of the rule from executing unless the conditions are met. That’s all.

With the one exception being that you can enable/disable/call another rule as an action in a rule.

Learning openHAB and rules coding in openHAB is just as big of a learning job, if not bigger.

Any home automation system is going to require you to learn you some programming. There is no way around that. Some relatively simple stuff can be done but as soon as you have anything more complicated than “when x happens flip the y relay” is going to require some programming.

Well, if you have a But only if… of between midnight and one minute after midnight that means that rule will only run when valve0 receives the command open sometime between 00:00 and 00:01. The rule won’t run if valve0 happens to already be open before 00:00. The rule won’t run if you send the command open to valve0 at any other time of day.

Also, the rule you posted doesn’t do anything like that. As written when valve0 receives the command open it sends the command open to valve0 (but valve0 already received an open command an now we enter an infinite loop) and it sends the command start to cistern_pump0.

I assume these are all String Items? Usually it’s best to use the openHAB Item that is the most near to what’s going on. So for a relay or a valve which can only be open or closed, a Switch Item would make the most sense to use in openHAB. If you used a Switch Item, you’d be using ON and OFF as the commands and what you’d look for as the State and use a transformation to convert ON to “open” and OFF to “closed” in the Thing’s Channel config.

Anyway, it would help to show us your attempt at creating the rule that didn’t work. It might also help to see your Items and Thing configs. All of this stuff works together. When something goes wrong you need to get a full picture of the config.

One last little bit, even if you don’t really know programming real well it can still be useful to see some logging in a rule. You can most easily do that in a “codeless” rule like this by adding a Script Action, choosing Blockly and then add a log block to log out the states of relevant Items and such.

If all you need is to see if the rule is running, you can bring up the developer sidebar (alt-shift-d or from the Developer Tools page) and start the event stream. That will show your Items updating, receiving command, changes, and it will show when your rules run.