Needing help with an Automation and its variables

Hopefully someone can point out what I’m doing wrong in the following automation

I have a file sensor which is updated by a shell script

this file sensor needs split into two seperate variables with an ! as the field delimiter

In the data template testing editor I have go what I think is the correct format see picture below

the automation is as follows

- id: '1682845133159'
  alias: GetPrinterInfo
#
# sensor.testpagecountfile is a file sensor
# and is formatted as XXX!YYY
#
# where XXX is the current pagecount
#       YYY is the total pagecount
#
  description: ''
  trigger:
  - platform: event
    event_type: ''
  - type: connected
    platform: device
    device_id: 40e2585b071d9cbc2439dbe1d94a755e
    entity_id: binary_sensor.samsung_sl_m3820nd
    domain: binary_sensor
  condition: []
  action:
  - service: shell_command.get_page_count
    data: {}
  - service: counter.configure
    data:
      minimum: 0
      maximum: 100
      value: '{{ states(''sensor.samsung_sl_m3820nd_toner_black'') }}'
    target:
      entity_id: counter.tonerlevel
  - service: counter.configure
    data:
      value_template: set pages = states('sensor.testpagecountfile').split("!")
      minimum: 0
      maximum: 100000
      value: "{{ pages[0] }}"
    target:
      entity_id: counter.pagecount
  - service: counter.configure
    data:
      value_template: set pages = states('sensor.testpagecountfile').split("!")
      minimum: 0
      maximum: 100000
      value: "{{ pages[1] }}"
    target:
      entity_id: counter.totalpagecount
  mode: single

but the pages variable is reported as undefined

Logger: homeassistant.components.automation.getprinterinfo
Source: components/automation/__init__.py:254
Integration: Automation (documentation, issues)
First occurred: 01:29:11 (1 occurrences)
Last logged: 01:29:11
Error while executing automation automation.getprinterinfo: Error rendering data template: UndefinedError: 'pages' is undefined

I have tried various positions and formats for the value_template lines but with out success

I clearly dont understand how templates and variables are used in automations and several hours of reading and editing have not made any difference !!

I can easily split the variable using two differnt file sensors the following works perfectly

  - platform: file
    name: pagecountfile
    file_path: /config/PageCount.txt
    value_template: '{{ value.split("!")[0] }}'

  - platform: file
    name: totalpagecountfile
    file_path: /config/PageCount.txt
    value_template: '{{ value.split("!")[1] }}'

but I just assumed I would be able to do it within the automation

If any one could explain to me what I’m doing wrong I would be very gratetfull

Try this:

  action:
  - service: shell_command.get_page_count
    data: {}
  - service: counter.configure
    data:
      minimum: 0
      maximum: 100
      value: '{{ states(''sensor.samsung_sl_m3820nd_toner_black'') }}'
    target:
      entity_id: counter.tonerlevel
  - variables:
      pages: "{{ states('sensor.testpagecountfile').split("!") }}"
  - service: counter.configure
    data:
      minimum: 0
      maximum: 100000
      value: "{{ pages[0] }}"
    target:
      entity_id: counter.pagecount
  - service: counter.configure
    data:
      value_template: set pages = states('sensor.testpagecountfile').split("!")
      minimum: 0
      maximum: 100000
      value: "{{ pages[1] }}"
    target:
      entity_id: counter.totalpagecount
  mode: single

Thank you for that it worked almost first time

I had to change the following line

pages: "{{ states('sensor.testpagecountfile').split("!") }}"

to

pages: "{{ states('sensor.testpagecountfile').split('!') }}"

Single qoutes around the ! rather than double quotes

Thats another little bit learned for me

thanks again

1 Like

I also forgot to delete the second value_template.

Oh yes just noticed that I just copied the little variables: block into my script and removed the value template

Out of interest do you know if there is any other way to do this

I couldnt work out any other method to get the data back into home assistant and the Synchru integartion doesnt provide the page counts for some reason as far as I can tell.

No sorry. I know nothing about Synchru.