How to create template sensor for random quotes?

I am trying to create a sensor that display quotes randomly using template sensor. This is what I did…

- platform: template
  sensors:
    random_motivational_quotes:
      friendly_name: 'Random Motivational Quotes'
      value_template: >-
          {{ [
          "Quote 1",
          "Quote 2",
          "Quote 3"
          ] | random }}

Unfortunately, my HA will not load each time I put up this sensor.

I am wondering why?

It works for me on Win 10 HA 62.0 (direct install) except that it changes every few milisecs when displayed in a group.

Edit: Changed it to use a time function to change quotes a bit more slowly…

- platform: template
  sensors:
    random_motivational_quotes:
      friendly_name: 'Random Motivational Quotes'
      value_template: >-
        {% set time_now = states('sensor.time').replace(':', '.')|float %}
        {% if time_now < 7 %}
          {{ [
              "Quote 1",
              "Quote 2",
              "Quote 3"
             ] | random }}
        {% elif time_now < 13 %}
          {{ [
              "Quote 4",
              "Quote 5",
              "Quote 6"
             ] | random }}
        {% elif time_now < 15 %}
          {{ [
              "Quote 7",
              "Quote 8",
              "Quote 9"
             ] | random }}
        {% elif time_now < 18 %}
          {{ [
              "Quote 10",
              "Quote 11",
              "Quote 12"
             ] | random }}
        {% else %}
          {{ [
              "Quote 13",
              "Quote 14",
              "Quote 15"
             ] | random }}
        {% endif %}
1 Like

thanks. i modified your sample a little bit to this…

- platform: template
  sensors:
    random_motivational_quotes:
      friendly_name: 'Random Motivational Quotes'
      value_template: >-
          {% set time_now = states('sensor.time').replace(':', '.')|float %}
          {% set quotes = [
          "Quote 1",
          "Quote 2",
          "Quote 3"
          ] %}

          {% if time_now < 1 %}
          {{quotes|random}}
          {% elif time_now < 2 %}
          {{quotes|random}}
          {% elif time_now < 3 %}
          {{quotes|random}}
          {% elif time_now < 4 %}
          {{quotes|random}}
          {% elif time_now < 5 %}
          {{quotes|random}}
          {% elif time_now < 6 %}
          {{quotes|random}}
          {% elif time_now < 7 %}
          {{quotes|random}}
          {% elif time_now < 8 %}
          {{quotes|random}}
          {% elif time_now < 9 %}
          {{quotes|random}}
          {% elif time_now < 10 %}
          {{quotes|random}}
          {% elif time_now < 11 %}
          {{quotes|random}}
          {% elif time_now < 12 %}
          {{quotes|random}}
          {% elif time_now < 13 %}
          {{quotes|random}}
          {% elif time_now < 14 %}
          {{quotes|random}}
          {% elif time_now < 15 %}
          {{quotes|random}}
          {% elif time_now < 16 %}
          {{quotes|random}}
          {% elif time_now < 17 %}
          {{quotes|random}}
          {% elif time_now < 18 %}
          {{quotes|random}}
          {% elif time_now < 19 %}
          {{quotes|random}}
          {% elif time_now < 20 %}
          {{quotes|random}}
          {% elif time_now < 21 %}
          {{quotes|random}}
          {% elif time_now < 22 %}
          {{quotes|random}}
          {% elif time_now < 23 %}
          {{quotes|random}}
          {% elif time_now < 24 %}
          {{quotes|random}}
          {% endif %}

it loads fine now. but the sensor value keep changing every 1 minute. How to make it 1 hour?

I think you can set the update of template sensor to match a state change of entity_id: in the configuration. If you make a hour time sensor that shows the hour of the day, then i think you can use

- platform: template
  sensors:
    random_motivational_quotes:
      entity_id: sensor.hour_of_the_day
      friendly_name: 'Random Motivational Quotes'
      value_template: >-
          {{ [
          "Quote 1",
          "Quote 2",
          "Quote 3"
          ] | random }}
2 Likes

thanks. it works!