Automation triggering After turning on

I have A problem, Everytime I startup or restart HASSIO my automation get triggered. Also when I reload or turn on the automation it will also trigger. Do you guys know why this happens

  - action:
    - data:
        entity_id: switch.onairlamp_jayce
      service: switch.turn_on
    - data:
        entity_id: switch.jayce_ledstripbed
      service: switch.turn_on
    alias: Kamer - Bedknop - A
    condition: []
    trigger:
    - payload: 'ON'
      platform: mqtt
      topic: mqtt/kamer/bedknop

##################################################
      
  - action:
    - data:
        entity_id: group.kamer
      service: homeassistant.turn_off
    alias: Kamer - Bedknop - B
    condition: []
    trigger:
    - payload: 'OFF'
      platform: mqtt
      topic: mqtt/kamer/bedknop

Are your MQTT messages set to retain=true? If so you can set them to false.

1 Like

I do not know how I would be able to turn that off in a automation, but here is my MQTT setup in the Configuration.yaml file

mqtt:
  broker: 192.168.1.10
  port: 8129
  client_id: HASSIO
  username: !secret api_mqttusername
  password: !secret api_mqttpassword

whenever you post a message to a topic, you need to check the retain status.
If you post within HA, here is an example for an MQTT Switch:

How do you set the status of your mqtt/kamer/bedknop ?

1 Like

I publish the MQTT Messages with this arduino code on a NodeMCU with the ESP8266 Build in

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define wifi_ssid "######"
#define wifi_password "#######"

#define mqtt_server "192.168.1.10"
#define mqtt_user "###"
#define mqtt_port: "8129"
#define mqtt_password "####"

int button1 = A0;
int button2 = D1;
int buttonstate1;
int buttonstate2;

WiFiClient espClient;
PubSubClient client(espClient);

///////////////////////////////// Void Setup //////////////////////////////////////////////////////////

void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(2,OUTPUT);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  start();
  
}

//////////////////////////////// Wifi Setup ////////////
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

bool checkBound(float newValue, float prevValue, float maxDiff) {
  return !isnan(newValue) &&
         (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;

void loop() {
    if (!client.connected()) {
    reconnect();
  }
  client.loop();
  
  buttonstate1 = analogRead(button1);
  buttonstate2 = digitalRead(button2);

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;
  }
  if (buttonstate1 > 1000 && buttonstate2 == HIGH){
    client.publish("mqtt/kamer/bedknop", "ONALL", true);
    ledblink();
    delay(2000);
  }
  else if (buttonstate1 > 1000){
    client.publish("mqtt/kamer/bedknop", "ON", true);
    ledblink();
    }
  else if (buttonstate2 == HIGH){
    client.publish("mqtt/kamer/bedknop", "OFF", true);
    ledblink();
    }
  delay(100);
}

void ledblink(){
  digitalWrite(2,LOW);
  delay(500);
  digitalWrite(2,HIGH);
}

void start(){
  digitalWrite(2,LOW);
  delay(10000);
  digitalWrite(2,HIGH);
}

Can you try and set all these client.publish to have false at the end?
I believe this sets the retain flag to true when value = true…
Note that it will means your know may not have a status when you restart HA.
Alternative way would be to have above automations with an initial_state: false and enable them about 5 sec after HA starts…

1 Like

“Alternative way would be to have above automations with an initial_state: false and enable them about 5 sec after HA starts…” I tried that before but did not work, I will remove the true in the client.publish line and see what happens

Changed IT, It still does not work the automations still trigger

ok, do you have an automation that runs when HA starts? You could use this as a condition in your above automations. The idea is the automation doesn’t trigger if HA start automation triggered < 5 sec ago:

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.ha_startup_automation_name.attributes.last_triggered | default(0)) | int > 5)}}'
1 Like

Not sure that’ll stop it from running when you reload the automations within HA though. Would actually be nice to know :slight_smile:

1 Like

Nope does not work for me :frowning:

Something doesn’t make sense…
code above sous have worked… works for me…
Il check again tomorrow. Going to bed now :blush: