chaos13
(Shane)
February 3, 2024, 2:41am
1
Hi, Im trying to get a sensor value and POST it to a simple API ive created. I have a template and a rest_command.
If I test in developer tools my api is receiving the below and not the actual value of sensor.power_up_time;
{“status_text”:“sensor.power_up_time”}
How do I get the actual value of my sensor to the API.
Any help is gratefully received!
tom_l
February 3, 2024, 3:18am
2
{"status_text": states('sensor.power_up_time') }
chaos13
(Shane)
February 3, 2024, 8:05am
3
Thanks for the reply!
service: rest_command.post_poweruptimes
data: {"status" : states('sensor.power_up_time')}
My API receives null as the data in the above case.
Here’s my Template / rest_command from my configuration.yaml
template:
- trigger:
- platform: event
event_type: "imap_content"
id: "custom_event"
event_data:
sender: "[email protected] "
sensor:
- name: "Power Up Time"
state: '{{ trigger.event.data["subject"].split("Power-ups: Opt in to Power up for free at ")[1] }}'
rest_command:
post_poweruptimes:
url: http://www.xxxxx.com/powerups/
method: POST
payload: '{"status_text": "{{ status }}"}'
content_type: 'application/json; charset=utf-8'
The sensor displays perfectly on my dashboard and works as expected.
The state is just a string of text like this “10:00 AM - 1:00 PM,
Sunday 28/01/24”
I have also tried other sensors the result is the same. If remove states() the JSON is received by my API just fine.
tom_l
February 3, 2024, 8:31am
4
service: rest_command.post_poweruptimes
data:
status: "{{ states('sensor.power_up_time') }}"
chaos13
(Shane)
February 3, 2024, 2:42pm
5
That worked thank you! For anyone integrating with imap watch out for hidden \r or \n in the sensors when pushed to JSON it breaks the format. I had to strip these first before POSTing.