Splitting a attribute to use for creating an automation stop command

Hi everyone and thanks for any advice.

I am trying to take the attribute date stored under the term Next Start

Next start April 5, 2025 at 23:30:00
but only use the data after “at”

This will be generated into a sensor so that I can then use it as a When time is equal to (in this case 23:30) then stop automation.
Having read the forums I tried this:

  - sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('t')[1] }}  # Split by letter t, get the second part
      unit_of_measurement: "timestamp"
      device_class: "timestamp"
      state_class: "Measurement" 

but being met with a lot of errors.

Can any one help.

split('at')

Your indentation is off too. unit_of_measurement: and everything under it should line up with state:

Thanks but got

Invalid config for 'template' at configuration.yaml, line 70: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at')[1] }} # Split by letter t, get the second part\n"
Invalid config for 'template' at configuration.yaml, line 72: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at')[1] }} \n"

It has also caused carnage with other sensors

Please share the config as it is now.

Thanks @tom_l

Tried a few different ways but currently at:

  - sensor:
      - name: "Octopus dispatching next"
        #unit_of_measurement: "timestamp"
        #device_class: "timestamp"
        #state_class: "Measurement"
        state: >
          {% set dispatch_next = states('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching)
          {{ state_attr('dispatch_next', 'Next start') | split('at')[1] }}  
        # Split by letter t, get the second part

Is it because it is expecting integers etc and it is text? I really don’t know.

which gave

Invalid config for 'template' at configuration.yaml, line 72: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at')[1] }} \n

There’s so much wrong.

  1. You are missing the %} at the end of the first line of your template
  2. dispatch_next will not contain an entity id. it will be on or off so your template resolves to this:
{{ state_attr('on', 'Next start') | split('at')[1] }} 

or

{{ state_attr('off', 'Next start') | split('at')[1] }} 
  1. use {# #} to put comments in a template, not #

So try this

  - sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at')[1] }}  
        device_class: "timestamp"
        state_class: "Measurement"

Thanks, as I say, I have got myself in a mess trying to problem solve.

So with your’s we are back to:

Invalid config for 'template' at configuration.yaml, line 69: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at')[1] }} \n"
- sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start').split('at ')[1] }}  
        device_class: "timestamp"
        state_class: "Measurement"
Invalid config for 'template' at configuration.yaml, line 69: invalid template (TemplateSyntaxError: expected token 'end of print statement', got '[') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at ')[1] }} \n"
  - sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at ')[1] }}  
        device_class: "timestamp"
        state_class: "Measurement"

That is not what I wrote.

You:

) | split('at ')[1] 

Me:

).split('at ')[1] 

[quote=“newbuilder22, post:10, topic:874148”]

- sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') | split('at ')[1] }}  
        device_class: "timestamp"
        state_class: "Measurement"

Invalid config for 'template' at configuration.yaml, line 69: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value 'sensor->0->state', got "{{ state_attr('binary_sensor.octopus_energy_a_a9264586_intelligent_dispatching', 'Next start'.split('at ')[1] }} \n"

Mate I can’t help you if you can’t copy and paste this exactly:

- sensor:
      - name: "Octopus dispatch next"
        state: >
         {{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start').split('at ')[1] }}  
        device_class: "timestamp"
        state_class: "Measurement"

Thanks for your patience and apologies. I need to enter the redacted details and have obviously mucked up as I have done it.

I am now getting the sensor- Thanks, but it is showing state as unavailable even though the sensor shows

image

Template variable error: 'None' has no attribute 'split' when rendering '{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start').split('at ')[1] }}'

What sensor is that?

What is it’s entity_id?

You have the wrong sensor in your template.

The sensor that has the attributes is:
binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching

does the sensor update based on a change or only capture live data?

This looks like the state of a sensor not an attribute of a binary sensor.
image

What does this return in the template editor:

{{ state_attr('binary_sensor.octopus_energy_a_REDACTED_intelligent_dispatching', 'Next start') }}

Moved forward:

So it is next_start

And outputs

state: "2025-04-06 23:30:00+01:00”

There’s no “at” in that. And where did “state:” come from?

What do you mean by “moved forward”?

Honestly it is very difficult to help you if you don’t follow instructions and don’t provide detail of what you have done. Good luck I’m off to bed.

Sorry, but I can only answer what I am able to. I am asking for help because I do not totally understand.

The movement forward has been I have been able to get the sensor to work by changing the next start to next_start

State is the state of the actions you asked me to perform or otherwise the output from them.

I.e the output states that……

You are correct there is no at in the raw data output but there is in The attributes shown in the user interface (I assume that is what it is called).

So now in the raw data it shows as a date code space and then time. So I just need to work out how to only get the time without the +etc

Thanks for getting me this far… just a shame that you have not understood my replies and that equally i have not seen all your changes as you have not coached me in what you have done but instead thrown useful coding back.

I am very pleased with what you have done, just a pity you are not with my replies. Maybe I can learn from that.