Open scstraus opened 5 years ago
Here's another one, I tried to take out the body but it still failed:
2019-05-15 01:52:08 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=variable, service=set_variable, service_data=variable=dpd_variable, value_template=delivery, attributes_template={
"from": "REDACTED",
"date": "Wed, 15 May 2019 01:51:24 +0200",
"subject": "Fwd: DPD Kurýr: Dnes Vám doručíme balíček",
}>
2019-05-15 01:52:08 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/variable/__init__.py", line 179, in async_set_variable
attributes = json.loads(attributes_template.async_render({ 'variable': current_state }))
File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 1 (char 137)
2019-05-15 01:52:08 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.dpd_state_change, old_state=<state automation.dpd_state_change=on; last_triggered=None, id=dpd_state_change, friendly_name=DPD state change, hidden=True @ 2019-05-15T01:50:17.586808+02:00>, new_state=<state automation.dpd_state_change=on; last_triggered=2019-05-15T01:52:08.949595+02:00, id=dpd_state_change, friendly_name=DPD state change, hidden=True @ 2019-05-15T01:50:17.586808+02:00>>
FYI here's my config (after I took out the body attribute setting)
- id: posta_state_change
alias: 'Posta state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.cp_packages_coming_today
condition:
condition: state
entity_id: 'sensor.cp_packages_coming_today'
state: 'delivery'
action:
service: variable.set_variable
data_template:
variable: posta_variable
value_template: "{{ states('sensor.cp_packages_coming_today')}}"
attributes_template: >
{
"from": "{{ state_attr('sensor.cp_packages_coming_today', 'from') }}",
"date": "{{ state_attr('sensor.cp_packages_coming_today', 'date') }}",
"subject": "{{ state_attr('sensor.cp_packages_coming_today', 'subject') }}",
}
# "body": "{{ state_attr('sensor.cp_packages_coming_today', 'body') }}"
- id: dpd_state_change
alias: 'DPD state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.dpd_packages_coming_today
condition:
condition: state
entity_id: 'sensor.dpd_packages_coming_today'
state: 'delivery'
action:
service: variable.set_variable
data_template:
variable: dpd_variable
value_template: "{{ states('sensor.dpd_packages_coming_today')}}"
attributes_template: >
{
"from": "{{ state_attr('sensor.dpd_packages_coming_today', 'from') }}",
"date": "{{ state_attr('sensor.dpd_packages_coming_today', 'date') }}",
"subject": "{{ state_attr('sensor.dpd_packages_coming_today', 'subject') }}",
}
# "body": "{{ state_attr('sensor.dpd_packages_coming_today', 'body') }}"
- id: dpd_variable_reset
alias: 'DPD Variable Reset'
hide_entity: true
initial_state: 'true'
trigger:
platform: time
at: '00:00:01'
action:
service: variable.set_variable
data:
variable: dpd_variable
value: no_delivery
attributes:
from: ''
date: ''
subject: ''
body: ''
icon: mdi:delivery
- id: dhl_state_change
alias: 'DHL state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.dhl_packages_coming_today
condition:
condition: state
entity_id: 'sensor.dhl_packages_coming_today'
state: 'delivery'
action:
service: variable.set_variable
data_template:
variable: dhl_variable
value_template: "{{ states('sensor.dhl_packages_coming_today')}}"
attributes_template: >
{
"from": "{{ state_attr('sensor.dhl_packages_coming_today', 'from') }}",
"date": "{{ state_attr('sensor.dhl_packages_coming_today', 'date') }}",
"subject": "{{ state_attr('sensor.dhl_packages_coming_today', 'subject') }}",
}
So, I've changed the configuration a bit which worked to make the template for the main value of the sensor work when I take out the attributes like this:
- id: posta_state_change
alias: 'Posta state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.cp_packages_coming_today
condition:
condition: state
entity_id: 'sensor.cp_packages_coming_today'
state: 'delivery'
action:
service: variable.set_variable
data:
variable: posta_variable
data_template:
value: >
{{ states('sensor.cp_packages_coming_today')}}
But when I put the attributes back in like this:
- id: posta_state_change
alias: 'Posta state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.cp_packages_coming_today
condition:
condition: state
entity_id: 'sensor.cp_packages_coming_today'
state: 'delivery'
action:
service: variable.set_variable
data:
variable: posta_variable
attributes_template: >
{
"from": "{{ state_attr('sensor.cp_packages_coming_today', 'from') }}",
"date": "{{ state_attr('sensor.cp_packages_coming_today', 'date') }}",
"subject": "{{ state_attr('sensor.cp_packages_coming_today', 'subject') }}",
"body": "{{ state_attr('sensor.cp_packages_coming_today', 'body') }}"
}
data_template:
value: >
{{ states('sensor.cp_packages_coming_today')}}
I get this again:
2019-05-15 23:47:16 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File "/config/custom_components/variable/__init__.py", line 179, in async_set_variable
attributes = json.loads(attributes_template.async_render({ 'variable': current_state }))
File "/usr/local/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid control character at: line 5 column 50 (char 188)
Okay, so the issue is with the size of the email body.. I was able to make it work with the below syntax, but as soon as I add the body on, it crashes just like above.
- id: posta_state_change
alias: 'Posta state change'
hide_entity: true
initial_state: 'true'
trigger:
platform: state
entity_id: sensor.cp_packages_coming_today
condition:
condition: state
entity_id: 'sensor.cp_packages_coming_today'
state: 'Delivery'
action:
service: variable.set_variable
data:
variable: posta_variable
attributes_template: >
{
"from": "{{ state_attr('sensor.cp_packages_coming_today', 'from') }}",
"date": "{{ state_attr('sensor.cp_packages_coming_today', 'date') }}",
"subject": "{{ state_attr('sensor.cp_packages_coming_today', 'subject') }}"
}
data_template:
value: >
{{ states('sensor.cp_packages_coming_today')}}
Also I found a related SQL error.. sensor.dpd_packages_coming_today is the exact same sensor, just with different email content for a different shipping provider.
Error executing query: (MySQLdb._exceptions.DataError) (1406, "Data too long for column 'event_data' at row 1") [SQL: 'INSERT INTO events (event_type, event_data, origin, time_fired, created, context_id, context_user_id) VALUES (%s, %s, %s, %s, %s, %s, %s)'] [parameters: ('state_changed', '{"entity_id": "sensor.dpd_packages_coming_today", "old_state": {"entity_id": "sensor.dpd_packages_coming_today", "state": "Delivery", "attributes": { ... (72419 characters truncated) ... :00", "last_updated": "2019-05-31T06:45:12.660235+00:00", "context": {"id": "3daf7b0e44124eb59eb2f765fa277ae7", "parent_id": null, "user_id": null}}}', 'LOCAL', datetime.datetime(2019, 5, 31, 6, 45, 12, 660283, tzinfo=
Hi, when I try to set one of the variable attributes to the value of the body of an email from IMAP email sensor, I get the following (traceback at the bottom)