Simple sensor

I am trying to set up a simple sensor to only announce items during the day time, but this does not seem to update sometimes, any idea what I am doing wrong? For example it was true all night and announced something at 6:45 am.

- platform: template
  sensors:
    voiceaction:
      value_template: > 
          {% if (now().hour >= 8 and now().hour <= 21) %}
            true
          {% else %}
            false
          {% endif %}

This is likely due to now.hour() not actually being a state that is recorded into the State Machine. As a result, your template sensor does not see any states that would trigger this template to update.

Maybe try the following:

Use the time_date platform to create a ‘time’ sensor that will be recorded to the State Machine.

sensor:
    - platform: time_date
      display_options:
          - 'time'

Then you can update your existing code by adding an ‘entity_id’ that your template will watch. When this entity_id is updated in the StateMachine, your template will re-run and update accordingly:

 - platform: template
   sensors:
      voiceaction:
      entity_id: sensor.time
      value_template: > 
          {% if (now().hour >= 8 and now().hour <= 21) %}
            true
          {% else %}
            false
          {% endif %}