Automation not triggered automatically

I have this automation which is not triggered automatically, but manually yes.
Can someone help me to find why this behaviour?

- alias: Qualcuno a Casa Accendi Radio
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d000201b557
    to: 'on'
  condition:
    - condition: time
      after: '10:30:00'
      before: '14:00:00'
    - condition: state
      entity_id: input_boolean.mauhome
      state: 'on'
    - condition: template
      value_template: '{{ (as_timestamp(now())-as_timestamp(states.input_boolean.mauhome.last_updated)) < 300 }}'
  action: 
    - service: script.turn_on
      entity_id: script.set_ampli_onkyo
    - service: script.turn_on
      entity_id: script.set_onkyo_status
    - delay: 00:00:01
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.salotto_audio
        volume_level: 0.60
    - service: media_player.play_media
      data_template:
        entity_id: media_player.salotto_audio
        media_content_id: "http://edge.radiomontecarlo.net/RMC.mp3"
        media_content_type: 'audio/mp4'

I would double-check the entity_ids, as well as if the state of the binary sensor is actually reported as “on/off”. If it’s a window/door sensor, it may come back as “open/closed” or something else.

The fact that it runs when activated manually means the action is working, but when you run an automation manually it bypasses trigger and condition values. I don’t see anything wrong with your trigger/conditions, so only culprit would be entity_id and the actual reported state of the sensor.

I have another similar automation:

- alias: Qualcuno a Casa Accendi Luci
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d000201b557
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: "00:05:00"
      - condition: state
        entity_id: input_boolean.mauhome
        state: 'on'
      - condition: template
        value_template: '{{ (as_timestamp(now())-as_timestamp(states.input_boolean.mauhome.last_updated)) < 300 }}'
  action: 
    - service: light.turn_on
      entity_id:  
        - light.yeelight_1
        - light.yeelight_2

This works good, so i think the problem is not related to the sensor… Maybe about conditions?
But also if i use the same conditions it is not working.

As far as I’m aware, when you have multiple conditions and qualifier, it’s the same as having “AND”, unless something has hanged recently.

You can try and make the condition section the same as this one using “AND” and see if that works.

I already tried, but unsuccesfully…

Don’t use and in the top level of conditions. It breaks more templates than it helps. Can you verify that your time & timezone is set properly in your operating system as well as your configuration?

EDIT: Your automation looks fine btw…

Yes, it’s all ok… All other time related automations are working. As i said previously i got this automation from the second i posted (that works ok). I removed only the AND condition and all the other was similar… Really don’t know why this is not triggered…

I disabled the value_template:

   - condition: template
     value_template: '{{ (as_timestamp(now())-as_timestamp(states.input_boolean.mauhome.last_updated)) < 300 }}'

and the automation works, but still do not understand why in the other configuration this template is working… Now, how to use that template in this automation?

I mean, it the automation would only work if you arrived home within the last 5 minutes or restarted home assistant in the last 5 minutes (unless you have restore state on your input boolean).

Yes, i know and it was just what i wanted with the second automation… but the question is always: why in this it doesn’t work and in the other yes?

Not sure to be honest. It should work. I’d start paying attention to the last_updated attribute and verify that it’s updating when it should be.

So you could flip the input_boolean and check that condition in the template editor and see the result as well.

And just for you own knowledge. Input booleans only ‘turn_on’ when off. If you supply the boolean with a ‘turn_on’ and it’s already on, no update will occur.

What you mean?

copy this:

{{ (as_timestamp(now())-as_timestamp(states.input_boolean.mauhome.last_updated)) < 300 }}

into the template editor and see weather it’s true or false after you flip the input_boolean.mauhome from off to on.

Ok clear, i’ll test it… but in alternative what to use in place of input_boolean?

A switch template would work, it behaves like a switch and turn_on’s will update the last_updated even if the switch is on or off.

hmmm… can you please give me an example? I still am not so good in templates… :joy:

How about you post your automation that flips the input_boolean. Cause a switch template may not be possible.

It returns “true” when i switch off and then on the input boolean. So it’s working as it must…

This are the automations that flip the input boolean:

- alias: 'Set non a Casa'
  condition:
    condition: state
    entity_id: input_boolean.mauhome
    state: 'on'
  trigger:
    platform: state
    entity_id:
      - device_tracker.fha_fha
      - device_tracker.s8_s8
      - device_tracker.galaxy_s8
#      - device_tracker.galaxy_note_3
    to: 'not_home' 
    for:
      minutes: 2
  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.mauhome

- alias: 'Set a Casa'
  condition:
    condition: state
    entity_id: input_boolean.mauhome
    state: 'off'
  trigger:
    platform: state
    entity_id:
      - device_tracker.fha_fha
      - device_tracker.s8_s8
      - device_tracker.galaxy_s8
#      - device_tracker.galaxy_note_3
    to: 'home'  
  action:
    - service: homeassistant.turn_on
      entity_id: input_boolean.mauhome