tom_l
May 3, 2024, 1:11am
2
All states are strings. Only attributes can have other types.
Please share your sensor configuration. You can probably use something like today_at(value_json)
along with supplying the correct device class.
Alternatively where are you trying to use this state that is causing issues?
1 Like
I’m retrieving event times from a source. These change daily and I want to not only store these but also use them in automation to send a reminder at the specific time retrieved in the API call.
petro
(Petro)
May 3, 2024, 12:22pm
4
Post the json and your configuration.
I haven’t done the full thing yet. I’ve only started with “mon”
{"city":"london","date":"2024-05-03","mon":"03:30","tue":"12:00","wed":"17:00","thur":"21:00","fri":"21:30"}
- resource_template: >-
{% set key = "" %}
https://www. site.com/api/times/?format=json&key={{ key }}&24hours=true/
headers:
Content-Type: "application/json"
scan_interval: 3600
sensor:
- name: "az_event_mon"
value_template: >-
"{{ value_json.mon }}"
I get:
az_event_mon → “03:30” with the quotes actually included in the value
petro
(Petro)
May 3, 2024, 2:55pm
6
The sensor itself will not show the quotes, it will be treated as a string without quotes when you look at it in the UI.
That’s what I find confusing. The UI is showing the quotes?!
petro
(Petro)
May 3, 2024, 3:14pm
8
because you are using multiline notation and single line notation for yaml.
value_template: >-
"{{ value_json.mon }}"
Use one or the other, not both.
mutliline…
value_template: >-
{{ value_json.mon }}
or single line
value_template: "{{ value_json.mon }}"
See
Templates are indicated by lines that start and end with {% %} or {{ }}. Everything else is yaml. The language contained inside {% %} and {{ }} is Jinja, it is not yaml.
Examples of a template:
{% set x = 4 %}
{{ x }}
Example of a template inside yaml
some_field: >
{% set x = 4 %}
{{ x }}
explanation:
some_field: > # THIS IS YAML
{% set x = 4 %} #THIS IS JINJA
{{ x }} # THIS IS JINJA
The character > in the some_field line is yaml. This mean the next set of indented lines are p…
1 Like
Thank you all very much indeed for your help. It’s working fine now.
I noticed that it’s making 5 API calls one for each sensor. Shouldn’t it just be one and the values taken and assigned to each “sensor”?
{"city":"london","date":"2024-05-03","mon":"03:30","tue":"12:00","wed":"17:00","thur":"21:00","fri":"21:30"}
- resource_template: >-
{% set key = "" %}
https://www. site.com/api/times/?format=json&key={{ key }}&24hours=true/
headers:
Content-Type: "application/json"
scan_interval: 3600
sensor:
- name: "az_event_mon"
value_template: "{{ value_json.mon }}"
sensor:
- name: "az_event_tue"
value_template: "{{ value_json.tue }}"
sensor:
- name: "az_event_wed"
value_template: "{{ value_json.wed }}"
petro
(Petro)
May 3, 2024, 6:36pm
10
- resource_template: >-
{% set key = "" %}
https://www. site.com/api/times/?format=json&key={{ key }}&24hours=true/
headers:
Content-Type: "application/json"
scan_interval: 3600
sensor:
- name: "az_event_mon"
value_template: "{{ value_json.mon }}"
- name: "az_event_tue"
value_template: "{{ value_json.tue }}"
- name: "az_event_wed"
value_template: "{{ value_json.wed }}"
Sorry. That’s what I did. I copied the wrong bit.
When I had the word sensor, it only created the az_event_wed
Even with this:
- resource_template: >-
{% set key = "" %}
https://www. site.com/api/times/?format=json&key={{ key }}&24hours=true/
headers:
Content-Type: "application/json"
scan_interval: 3600
sensor:
- name: "az_event_mon"
value_template: "{{ value_json.mon }}"
- name: "az_event_tue"
value_template: "{{ value_json.tue }}"
- name: "az_event_wed"
value_template: "{{ value_json.wed }}"
I’m still seeing 5 API calls in the logs
petro
(Petro)
May 3, 2024, 7:32pm
12
that will only make 1 call, how are you certain it’s making 5?
You are right. There is only one entry for:
DEBUG (MainThread) [homeassistant.components.rest]
and 5 for
DEBUG (MainThread) [homeassistant.components.rest.data]
I completely missed the .data bit in the log
Thank you once again.
1 Like
@petro Is there a way to manually trigger the API call and updating the sensors using an automation?
tom_l
May 4, 2024, 1:46am
15
See: How to set a custom scan_interval
In your case because the integration is not set up via the UI just set a really long scan interval (years) in the YAML config.
Then do the automation bit.
Note: It will still poll once when you restart home assistant.
Tam481
May 4, 2024, 12:01pm
16
Many thanks @tom_l , I can’t see the RESTful integration under Devices. If I try to add it through the UI I get a message that this cannot be done through the UI.
Yes but you linked to another post on how to use automation to trigger manual updates and the first thing in the post talked about enabling polling for updates for the integration which I thought was required in order to allow the automation to trigger the update. Is that not the case?
tom_l
May 4, 2024, 10:12pm
20
I’ve told you this three times now.
In your case because the integration is not set up via the UI ignore the first bit about polling in the UI.
Instead just set a really long scan interval (years) in the YAML config.
Then do the automation bit in that post.
And take note of the exception about restarts I mentioned.
Tam481
May 5, 2024, 10:27am
21
Got it. Many thanks. It’s working perfectly now.
Thank you both