Binary State after restart prevents run

Hi,

I created an automation that is triggered by the sunset. A condition checks if I has been the last 24h not at home. This is done by a binary sensor. But restarting HA changes the state of that binary sensor to false for a short time. Is there any idea how to solve that?

Greets

Very likely yes. Please share your automation yaml correctly formatted for the forum.

description: ""
triggers:
  - trigger: sun
    event: sunrise
    offset: "-01:00:00"
conditions:
  - condition: state
    entity_id: binary_sensor.nobody_at_home
    state: "on"
    for:
      hours: 24
      minutes: 0
      seconds: 0
actions:
  - delay:
      minutes: "{{ range(0,120)|random }}"
  - parallel:
      - sequence:
          - delay:
              minutes: "{{ range(0,5)|random }}"
          - action: cover.open_cover
            metadata: {}
            data: {}
            target:
              area_id:
                - sz
mode: single

Not according to this:

Sry. I have two automations. One for sunrise, one for sunset. The structure is the the same. The condition is the same. The actions are opening and closing covers.
To reduce the complexity of my question I reduced to one case.

Can you share the config for this?

input_boolean:
  A_home:
    name: A is Home
  B_home:
    name: B is Home
  C_home:
    name: C is Home

template:
  - sensor:
    - name: 'A_home'
      state: >
        {% if is_state('input_boolean.A_home', 'on') %}
          Home
        {% else %}
          not home
        {% endif %}
    - name: 'B_home'
      state: >
        {% if is_state('input_boolean.B_home', 'on') %}
          Home
        {% else %}
          not home
        {% endif %}
    - name: 'C_home'
      icon: mdi:account
      state: >
        {% if is_state('input_boolean.C_home', 'on') %}
          Home
        {% else %}
          not home
        {% endif %}

  - binary_sensor:
    - name: "all at home"
      icon: mdi:person
      state: >-
        {{ is_state('input_boolean.A_home', 'on') and
        is_state('input_boolean.B_home', 'on') and
        is_state('input_boolean.C_home', 'on') }}

    - name: "somebody at home"
      icon: mdi:person
      state: >-
        {{ states('input_boolean.A_home') or
        states('input_boolean.B_home') or
        states('input_boolean.C_home') }}

    - name: "nobody at home"
      icon: mdi:person
      state: >-
        {{ is_state('input_boolean.A_home', 'off') and
        is_state('input_boolean.B_home', 'off') and
        is_state('input_boolean.C_home', 'off') }}

What to do to fix that problem? Is there a workaround?

Whether it is off or unavailable, reloads will probably always say the sensor was not in the same state for 24 hours. So setting a timestamp when the last person leaves is probably a better way to go than for statements that are this long.

It seems like you are using input booleans for what device trackers/person/zone states usually do. Is there a specific reason you are not using that functionality?

Yes. The input boolean is set by homekit. In my case it was the only option which was working properly.
What do you mean with setting a timestamp? How, where to store and how to use?
Can you please give me some more hints?

There’s for instance the date and time input helper in the helpers section

You can clear it when you come home, and set it when the last person leaves home:

action: input_datetime.set_datetime
target:
  entity_id: input_datetime.last_person_left
data:
  datetime: "{{ now() }}"

Then you can check for it in other places.

{{ (states('input_datetime.last_person_left') | as_datetime(now()) + timedelta(days=1)) < now()