Automation stops working when some Entity IDs is added

I have an automation that saves the sensor values to a file.
I have 5 Xiaomi aqara temperature sensors.
For 2 of the sensors I can save temperature and battery level without any problems.
But for the remaining 3 sensors the automation will stop working if I add the battery level Entity ID.

I will give an example.

This work:
Sensor 1:
Temperature Entity ID: sensor.aqara_1_3
Battery Level Entity ID: sensor.aqara_1_battery_level
I’m using the following code in my automation.
{{ states.sensor.aqara_1_3.state }};{{ states.sensor.aqara_1_battery_level.state }}

This will stop the automation script from working:
Sensor 3:
Temperature Entity ID: sensor.temperature_8
Battery Level Entity ID: sensor.3_kok_battery_level
In this case I’m using:
{{ states.sensor.temperature_8.state }};{{ states.sensor.3_kok_battery_level.state }}
If I remove the last part: ;{{ states.sensor.3_kok_battery_level.state }} the automation works.

In Lovelace UI it’s no problem to add the battery level for Sensor 3 (sensor.3_kok_battery_level) but if I use it in an automation like above it won’t work.
Can someone explain why I have this problem and how to solve it?

Can you post the working and non-working automations? Otherwise it’s hard to tell.

Take a look here.

I might be stupid or dumb…
I tried to change to: {{ states.sensor.['3_kok_battery_level'].state }}
as described(?) in the tip from @VDRainer, but it still fails…

@Tediore example of working automation script:

 - id: '1572899635640'
  alias: Save sensor data to file
  description: ''
  trigger:
  - hours: '*'
    minutes: /6
    platform: time_pattern
    seconds: '00'
  condition: []
  action:
  - data:
      message: ';Battery level: {{ states.sensor.aqara_1_battery_level.state  }}'
    service: notify.filenotify

not working script:

- id: '1572899635640'
  alias: Save sensor data to file
  description: ''
  trigger:
  - hours: '*'
    minutes: /6
    platform: time_pattern
    seconds: '00'
  condition: []
  action:
  - data:
      message: ';Battery level: {{ states.sensor.3_kok_battery_level.state }}'
    service: notify.filenotify

I have tried to change the message to:
message: ';Battery level: {{ states.sensor.['3_kok_battery_level'].state }}'
But it still fails.

Edit, Format yaml code.

1 Like

Please post code properly formatted.

Point 11

EDIT: And read the link that i posted once again. :slightly_smiling_face:

I must be illiterate, still can’t understand my error…

Remove the point after sensor.

{{ states.sensor['3_kok_battery_level'].state }}

should do it.

1 Like

Thank you @VDRainer for your patience, next time I promise to put on my glasses when you giving advice.

1 Like