How to use the http api to post a sensor's attributes containing a list of objects

Hi all,

I’m trying to write a powershell script to send an API POST request to home assistant to update a custom sensor I’ve created to store my meeting events for that day, however I’m having a hard time figuring out how to post an entities attributes if they contain a list of objects.

Basically I am trying to achieve something like this this:

However when I create a test object in PS I get the following result in HA:

Here is my testing PS script:

$headers = @{"Authorization"="Bearer $HAToken";}

Write-Host ("Setting Microsoft Teams meetings test")
$params = @{
 "state"="Valid";
 "attributes"= @{
   "friendly_name"="$entityMeetingsName";
   "icon"="mdi:microsoft-teams";
   "meetings" = (
     @{
        "name"="test1";
        "start_time" = "9:01";
     },
     @{   
        "name"="test2";
        "start_time"="11:00";
     })
  }
}

$params.attributes.meetings = $params.attributes.meetings | ConvertTo-Json
	 
$params = $params | ConvertTo-Json
Invoke-RestMethod -Uri "$HAUrl/api/states/$entityMeetings" -Method POST -Headers $headers -Body ([System.Text.Encoding]::UTF8.GetBytes($params)) -ContentType "application/json"

This is my first time attempting to use both PS and home assistant APIs so please be kind

Found my own answer.

For anyone having a similar issue, this had to do with the powershell script, the ConvertTo-Json requires an additional argument to convert the object beyond 2 levels, so I replaced this:

$params.attributes.meetings = $params.attributes.meetings | ConvertTo-Json
	 
$params = $params | ConvertTo-Json

with this:
$params = $params | ConvertTo-Json -Depth 10

problem solved!

1 Like