Send Template Text Sensor state to HA only at change

Many thanks @Mahko_Mahko
I just realized indeed this morning that we shall use the quoted lambda in a different filter node and not within the main lambda.

I already tested this, but you were quicker. The below works perfectly! Very nice, thank you again!

text_sensor:

- platform: template
  id: test_sensor
  name: Test Sensor
  update_interval: 1s
  icon: mdi:progress-clock
  filters:
    - lambda: |-
        static std::string last;
        if (x == last)
          return {};
        last = x;
        return x;
  lambda: !lambda |-
    auto ctrl = id(sprinkler_ctrl);
    auto active = ctrl->active_valve();
    if (!active.has_value()) {
      return std::string("--");
    }
    auto time_remaining = ctrl->time_remaining_active_valve();
    if (!time_remaining.has_value()) {
      return std::string("00");
    }
    int seconds = round(id(sprinkler_ctrl).time_remaining_active_valve().value_or(0));
    int days = seconds / (24 * 3600);
    seconds = seconds % (24 * 3600);
    int hours = seconds / 3600;
    seconds = seconds % 3600;
    int minutes = seconds /  60;
    seconds = seconds % 60;
      return {
        ((days ? String(days) + "d " : "") +
        (hours ? String(hours) + "h " : "") +
        (minutes ? String(minutes) + "m " : "") +
        (String(seconds) + "s")
        ).c_str()};
2 Likes