Split sensor data into two using comma as delimiter

So I’ve had to create a bash script to curl a website and trim the output

it runs and outputs the following: Friday 6th May, black

Id like to only query the website once - so trying to avoid running a similar request twice

so far I’ve tried the below, now I am new and all of the below is in the configuration.yaml - not sure if that’s where I should put the “Template” part?

  - platform: command_line
    name: BinsChecker
    command: /config/BinScript.sh
    scan_interval: 60
    command_timeout: 30


  - platform: command_line
    name: BinsChecker2
    command: /config/BinScript.sh
    scan_interval: 61
    command_timeout: 30
    value_template: "{{ value.split(',')[0] }}, {{ value.split(',')[1] }}"

# Template Bin Data
template:
  - sensor:
      - name: "Bin Day"
        state: "{{ states.sensor.BinsChecker.state.split(',')[0] }}"
      - name: "Bin Colour"
        state: "{{ states.sensor.BinsChecker.state.split(',')[1] }}"

any guidence on what I’m missing would be great or if its just not possible?

Thanks

Edders

Remove the BinsChecker2 config: you don’t need that. The rest of what you have is close, but should use lower-case and ideally this syntax:

template:
  - sensor:
      - name: "Bin Day"
        state: "{{ states('sensor.binschecker').split(',')[0] }}"
      - name: "Bin Colour"
        state: "{{ states('sensor.binschecker').split(',')[1] }}"

If it’s still not working after a restart, check if sensor.binschecker exists and contains what you want.

You probably don’t need to query the website every 60s though…

Even better though would be to use a rest sensor instead and you can do it in one go. What’s in BinScript.sh?

2 Likes

That worked perfectly, Many thanks

For completeness

for testing I outputted the Curl request to temp file so that I could test this with out querying the website too often. and set it to 60 seconds so I could make changes without restarting services

#!/usr/bin/env bash
#curl -s -X POST "https://app.ipswich.gov.uk/bin-collection/" --data-urlencode "txt_street=kings way” --data-urlencode "but_submit=Find now" | grep -E "This week,|Next week,*" | sed -E 's/<[^>]*>//g; s/ bin[s]* will be emptied\.//; s/ your //g; s/^.*on //; s/[0-9]+, */,/g' | sed 's/ ,/,/g' | jq -nR '[inputs | split(",") | {"date": .[0], "type": .[1]}]'

#Note I struggled with the output as Jason so switched to flat output and also used a file htmltest to do testing of the Grep / Sed Commands.

cat htmltest | grep -E "This week,|Next week,*" | sed -E 's/<[^>]*>//g; s/ bin[s]* will be emptied\.//; s/ your //g; s/^.*on //; s/[0-9]+, */,/g' | sed 's/ ,/,/g'

Originally I wanted to display a tile with the Day of the week and the colour for this weeks collection and the day and could for next weeks. but it got complicated so resorted to just this weeks data.

Once again many thanks @Troon

1 Like