Randomly working automation with input_text and template

Hi,

In Stockholm the local radio tweets traffic information. I use the quite standard scrape sensor to get it in to HA.

This is the sensor (that works fine)

  - platform: scrape
    name: Trafik P4 Stockholm
    resource: https://twitter.com/TrafikP4Sthlm
    select: ".js-tweet-text-container .tweet-text"

After using this sensor, I realized that it some days the latest tweet is only valid for minutes and I only see the latest. Therefore I build a stack of FIFO for the tweets using input_text.

To keep the interest, I start with the automation the randomly works then I will give you the full automation. Please note that the day and time part works.

As said, this works, some times!!!

  - data_template:
      entity_id: input_text.trafik_7
      value: "{{ states.sensor.trafik_p4_stockholm.state.split ('http') [0] }}"
    service: input_text.set_value

(tried without the “split” it is still working randomly)

Here is the full automation.

- id: '1580301033027'
  alias: test trafik
  description: ''
  trigger:
  - event_data:
      entity_id: sensor.trafik_p4_stockholm
    event_type: state_changed
    platform: event
  condition: []
  action:
#   - data_template:
#       entity_id: input_text.trafik_6
#       value: '{{ states.input_text.trafik_5.state }}'
#     service: input_text.set_value
#   - data_template:
#       entity_id: input_text.trafik_time_6
#       value: '{{ states.input_text.trafik_time_5.state }}'
#     service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_5
      value: '{{ states.input_text.trafik_4.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_5
      value: '{{ states.input_text.trafik_time_4.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_4
      value: '{{ states.input_text.trafik_3.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_4
      value: '{{ states.input_text.trafik_time_3.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_3
      value: '{{ states.input_text.trafik_2.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_3
      value: '{{ states.input_text.trafik_time_2.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_2
      value: '{{ states.input_text.trafik_1.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_2
      value: '{{ states.input_text.trafik_time_1.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_1
      value: '{{ states.input_text.trafik_7.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_1
      value: '{{ states.input_text.trafik_time_7.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_7
      value: "{{ states.sensor.trafik_p4_stockholm.state.split ('http') [0] }}"
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_7
      value: '{{ as_timestamp(states.sensor.trafik_p4_stockholm.last_changed)| timestamp_custom("%A %X") }}'
    service: input_text.set_value

As said, it is a stack first in first out, and it works, now and then:-(
The " time and date thing works fine.

If you are wondering here is the configuration.yaml.

input_text:
  trafik_1:
    name: Trafik 1
    initial: ' '
  trafik_2:
    name: Trafik 2
    initial: ' '
  trafik_3:
    name: Trafik 3
    initial: ' '
  trafik_4:
    name: Trafik 4
    initial: ' '
  trafik_5:
    name: Trafik 5
    initial: ' '
  trafik_6:
    name: Trafik 6
    initial: ' '
  trafik_7:
    name: Trafik 7
    initial: ' '

  trafik_time_1:
    name: Trafik time 1
    initial: ' '
  trafik_time_2:
    name: Trafik time 2
    initial: ' '
  trafik_time_3:
    name: Trafik time 3
    initial: ' '
  trafik_time_4:
    name: Trafik time 4
    initial: ' '
  trafik_time_5:
    name: Trafik time 5
    initial: ' '
  trafik_time_6:
    name: Trafik time 6
    initial: ' '
  trafik_time_7:
    name: Trafik time 7
    initial: ' '

Hi,

I solved most of the issue. The problem was in definition of input_text in config.yaml.


input_text:
trafik_1:
name: Trafik 1
initial: ’ ’

By default this is 100 characters long, a tweet can be 280 characters. You might think that the first 100 characters would be copied, but no nothing gets copied.

Now I changed the config.yaml to as follows.

  trafik_1:
    name: Trafik 1
    initial: ' '
    max: 255

There is still posibilities for glitches, a tweet can as said be 280 charaters and input_text can only be 255, but mostly it works. This is how it looks.

I found another issue, when the tweets somes to close to each other (less than a minute), it might fail as well, I’m trying with a delay in the automation. This is my complete configuration.

config.yaml


input_text:
  trafik_1:
    name: Trafik 1
    initial: ' '
    max: 255
  trafik_2:
    name: Trafik 2
    initial: ' '
    max: 255
  trafik_3:
    name: Trafik 3
    initial: ' '
    max: 255
  trafik_4:
    name: Trafik 4
    initial: ' '
    max: 255
  trafik_5:
    name: Trafik 5
    initial: ' '
    max: 255
  trafik_6:
    name: Trafik 6
    initial: ' '
    max: 255
  trafik_7:
    name: Trafik 7
    initial: ' '
    max: 255

  trafik_time_1:
    name: Trafik time 1
    initial: ' '
  trafik_time_2:
    name: Trafik time 2
    initial: ' '
  trafik_time_3:
    name: Trafik time 3
    initial: ' '
  trafik_time_4:
    name: Trafik time 4
    initial: ' '
  trafik_time_5:
    name: Trafik time 5
    initial: ' '
  trafik_time_6:
    name: Trafik time 6
    initial: ' '
  trafik_time_7:
    name: Trafik time 7
    initial: ' '

Automation.yaml


- id: '1580301033027'
  alias: Övrigt_trafik_tweet
  description: ''
  trigger:
  - event_data:
      entity_id: sensor.trafik_p4_stockholm
    event_type: state_changed
    platform: event
  condition: []
  action:
  - data_template:
      entity_id: input_text.trafik_6
      value: '{{ states.input_text.trafik_5.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_6
      value: '{{ states.input_text.trafik_time_5.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_5
      value: '{{ states.input_text.trafik_4.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_5
      value: '{{ states.input_text.trafik_time_4.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_4
      value: '{{ states.input_text.trafik_3.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_4
      value: '{{ states.input_text.trafik_time_3.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_3
      value: '{{ states.input_text.trafik_2.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_3
      value: '{{ states.input_text.trafik_time_2.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_2
      value: '{{ states.input_text.trafik_1.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_2
      value: '{{ states.input_text.trafik_time_1.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_1
      value: '{{ states.input_text.trafik_7.state }}'
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_1
      value: '{{ states.input_text.trafik_time_7.state }}'
    service: input_text.set_value
  - delay: 00:02:00
  - data_template:
      entity_id: input_text.trafik_7
      value: "{{ states.sensor.trafik_p4_stockholm.state.split ('http') [0] }}"
    service: input_text.set_value
  - data_template:
      entity_id: input_text.trafik_time_7
      value: '{{ as_timestamp(states.sensor.trafik_p4_stockholm.last_changed)| timestamp_custom("%A
        %X") }}'
    service: input_text.set_value

lovelacexxx.yaml


  - type: markdown
    content: >
      ## #Kollektivtrafiken Stockholm
      
      {{as_timestamp(states.sensor.kolletivtrafiken.last_changed) | timestamp_custom("%A %X") }} 
      
      {{ states.sensor.kolletivtrafiken.state.split('#koll')[0] }}
      

      {{ states.input_text.buss_time_1.state }}

      {{ states.input_text.buss_1.state }}
      
 
      {{ states.input_text.buss_time_2.state }}

      {{ states.input_text.buss_2.state }}
      
      
      {{ states.input_text.buss_time_3.state }}

      {{ states.input_text.buss_3.state }}
      
      
      {{ states.input_text.buss_time_4.state }}

      {{ states.input_text.buss_4.state }}


      {{ states.input_text.buss_time_5.state }}

      {{ states.input_text.buss_5.state }}
      
      
      {{ states.input_text.buss_time_6.state }}

      {{ states.input_text.buss_6.state }}

      
      https://twitter.com/kollektivkollen

////Tobe