Adjust polling frequency of Random Sensor

Hi guys,

I’ve built a custom keypad mounted outside my shed to control all manner of things (using a Shelly Plus i4 as an input device). Working nicely…

There is a timeout on the automation that reads the keypad so if a valid code is not input within 10 seconds it terminates the input and plays a TTS message on the Symfonisk speaker just inside the shed. There are ten messages from which one is chosen at random by the current state of the Random sensor.

Default behavior of the Random sensor is to update its state every 30 seconds - so a persistent fiddler at the keypad could get the same message two or even three times in a row before the sensor changes to select a different message. All I really want is to be able to set the Random sensor to refresh every 10 seconds rather than every 30.

configuration.yaml contains

 - platform: random
    minimum: 1
    maximum: 10

There does not appear to be a configuration variable to change the refresh rate.

Is this doable.

The only posts I’ve found where folk were asking to do something like this were answered not with how to do it, but with “why would you want to?”

Thanks in anticipation.

Not easily, but I think you could make your own using a time pattern triggered template sensor.

I’m definitely in the “why would you want to?” camp. Why not just randomize the messages in the automation…? Post your message automation so we can see what is possible.

Simple answer with a random sensor is no you can’t.

However, instead of using a random sensor, use the following in your automation to generate a random number for your message selection.

{{ range(1, 10) | random }}

The code from the relevant bit of the automation is like this…

choose:
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "1"
    sequence:
      - service: input_text.set_value
        data:
          value: Please stop playing with my buttons.
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "2"
    sequence:
      - service: input_text.set_value
        data:
          value: The novelty will wear off eventually.
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "3"
    sequence:
      - service: input_text.set_value
        data:
          value: Are you done fiddling yet?
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "4"
    sequence:
      - service: input_text.set_value
        data:
          value: Are you getting tired of this yet?
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "5"
    sequence:
      - service: input_text.set_value
        data:
          value: Keep this up and you will have to buy me a drink.
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "6"
    sequence:
      - service: input_text.set_value
        data:
          value: I am getting bored. Are you not bored yet?
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "7"
    sequence:
      - service: input_text.set_value
        data:
          value: I have an appointment shortly. Will you be much longer?
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "8"
    sequence:
      - service: input_text.set_value
        data:
          value: Should I call someone to help you with this?
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "9"
    sequence:
      - service: input_text.set_value
        data:
          value: Did you know, there is a camera just over there?
        target:
          entity_id: input_text.text_to_say
  - conditions:
      - condition: state
        entity_id: sensor.random_sensor
        state: "10"
    sequence:
      - service: input_text.set_value
        data:
          value: I just logged a call with the Police.
        target:
          entity_id: input_text.text_to_say

Yaml is one of those things I can just about understand when I’m reading it, but have real trouble getting it “just right” trying to write it. Thank goodness for the GUI or I’d be bugging you all a lot more.

Many thanks

....
action:
  - variables:
      messages:
        - Please stop playing with my buttons.
        - The novelty will wear off eventually.
        - Are you done fiddling yet?
        - Are you getting tired of this yet?
        - I am getting bored. Are you not bored yet?
        - I have an appointment shortly. Will you be much longer?
        - Should I call someone to help you with this?
        - Did you know, there is a camera just over there?
        - I just logged a call with the Police.
  - service: input_text.set_value
    data:
      value: "{{ messages | random }}" 
    target:
      entity_id: input_text.text_to_say

Thanks Drew,

System won’t let me save that…

 Message malformed: Unable to determine action @ data['action'][1] 

You don’t need to add action: if you are putting it into an existing action block of an automation… that was just to show where it goes generally. If your still having issues post your complete automation so we can see where the problem is.

Thanks Drew. All working well :slight_smile:

I was trying to enter the code into a separate block in the GUI, which didn’t want to play ball, but looking at the whole automation in yaml it made more sense how it fitted in.

Dear @Didgeridrew and Guys, I really do appreciate your work :heart:

However, such a solution is rather a kind of workaround.
I would suggest simply adding a new variable polling_rate to this Random Sensor to be able to set the refresh frequency value [in msec] freely. Sounds more professional?