Help with home assistant api call

I have hassio on a rasp pi 3b.

I have a open/close sensor and i want to change the state and attributes of an entity (climate.panasonic_aircon), when the sensor changes state. I am ok writing the automation.yaml for this I just get stuck on this part

action:
  # magic happens and the state / attributes of the entity changes

Ok. But I see you want to change the state of a device. To do that something has to trigger that state change. This is usually from some “external” source or from an automation or manual flip of a switch inside hassio.

So maybe I’m confused or lost. What are you actually trying to do?

Short answer

- alias: set climate state to off when aircon turns off
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
      from: 'on'
      to: 'off'
  action:
    # change the state of climate.panasonic_aircon to off

Long answer:

I have a dumb air conditioner.

I use it with the broadlink climate custom component:

If the air conditioner is turned on / off using the original IR remote then home assistant and the climate control doesn’t know that the aircon has be turned on.

I added a zigbee door / window sensor to the air conditioner to detect if it is turned on / off

I thought I might be able to use this to update home assistant to tell it what the air conditioner is doing.

edit: i wrote in my post that the state was reverting back, but it’s not the state is sticking. As I test i changed the climate.panasonic_aircon state to “tomatoes” and it is staying as tomatoes, so that is good.

tomatoes…I like that… ok I’m tired and I need some more nicotine. But here’s a shot.

Take a look at the climate component “Generic Thermostat” for some ideas and here’s a snip from my climate automations

action:
  - service: climate.set_temperature
    data:
      entity_id: climate_front_room_thermostat
      ac_mode: False
      temperature: 70

There’s a lot of options, but you would pass them to the entity_id through the data tag. Match the options with your climate component you are using. That’s not a exact example as I’m using the vera temperature component. But gives you an idea.

1 Like

Thanks. Unfortunately that’s not what I am after I already know how to call the climate service.

I want to change (overwrite) the state without calling the service. I can do it using the web interface, but I don’t know how to put that into a script.

The curl example I gave before should work with the “script component” just modify it to work. Does the same as the web interface.

However. With using hass.io it is limited to what is provided within hass.io. I don’t run hass.io anymore as it was too limiting as I have a bi-directional setup with a VeraPlus unit as my device controller. So I like having command line access and doing http calls between the two systems.

I fired up ssh into my hassio and i typed the curl command, it is available.

I still don’t know how to make a script with curl command as there is no documentation or examples.

like do i type that example into a text file and save it with the .py extension, where do i save it etc… i have done a lot of googling but I can’t find any tutorial or existing example that i can use to figure out how to fire off that curl command from inside an automation in hassio.

actually i think i found it?

actually when i check in HA i should be using the rest_command i think

I think it’s supposed to be this:

Configuration.yaml

rest_command:
  set_climate_state_idle:
    method: POST
    url: 'https://xxxxxx.duckdns.org:8123/api/states/climate.panasonic_aircon'
    password: 'xxxxxx'
    content_type: application/json
    payload: '{
    "state": "idle",
    "attributes": {
        "fan_mode": "auto",
        "operation_mode": "idle"
        }
    }'

Automation.yaml

- alias: set climate state to idle when aircon turns off
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0001f38b1f
      from: 'on'
      to: 'off'
  condition:
    - condition: template
      value_template: "{{ not is_state('climate.panasonic_aircon', 'idle') }}"     
  action:
    - service: rest_command.set_climate_state_idle

but i get an error in configuration.yaml and I’m not sure what the issue is.

Try putting the payload all on one row

Im been strugeling for 15 hours straight now trying to accomplish the same thing as you.
Im using xeoma and has moved from Switchking to HA recently.

Erlier i sent http request like h_ttp://switchking_ip/devies/“device number”/turn_on when motion was detected.

As i understand you have made some binary sensor to listen to request from xeoma.

Trued to used command_line platform, restful-command and i have no clue what im doing wrong.
Even tried to send http request as h_ttp://ip-to my ha server/api/services/switch.turn_on/switch.my_entity
And also h_ttp://ip-to my ha server/api//switch.turn_on/switch.my_entity
But no luck.
when running h_ttp://ip-to my ha server/api/services/switch.turn_on/switch.my_entity i get 405 not allowed and h_ttp://ip-to my ha server/api//switch.turn_on/switch.my_entity (404 not found).

Can you please guide me in the right direction before i go nuts?

Best Regards
/Anders

Are you passing the x-auth in the header? It sounds like you have authentication enabled. In the API docs there’s an example of passing the auth credentials as well.

I tried with auth disable as well.

Tried to send it as a cURL script from my xeoma server instead and it works fine.

But dont understand how to send header with the http request from xeoma.

Still have configuration error if one line.

As mentioned on some forum i uset the start a program in xeoma insted. Start program cURL with the header parameters. Not as good att plain http request i xemoma but good enought.

Here’s the exact code I use on my Xeoma server.

#!/bin/bash

curl -X POST \\
    -H "Content-Type: application/json" \\
    -d '{"state": "on", "attributes": {"device_class": "motion", "friendly_name": "Driveway Cam 1 Motion"}}' \
    http://192.168.10.100:8123/api/states/binary_sensor.driveway_cam1_motion

I don’t use HTTP Auth on my system because it’s not Internet facing at all so I don’t have a need for Auth.

Scripts:
driveway_cam1_on.sh
driveway_cam1_off.sh

The only difference in the on/off is the state that I send in the curl command change the “state”: “on” section to either on or off.

I then use the Xeoma run script function or “Application Runner”. There’s 2 instances of Application runner. 1 for “on” and 1 for “off”.

I got it working at the command prompt

curl -X POST -H "x-ha-access: xxxxxxxxxxxxx" -H "Content-Type: application/json" -d '{"state": "on", "attributes": {"attribute_name": "attribute_value"}}' https://xxxxxx.duckdns.org:8123/api/states/climate.panasonic_aircon

and now I have it working using the rest_command too:

Configuration.yaml

rest_command:
  set_climate_to_on:
    method: POST
    url: 'https://xxxxxxxxxxxxx.duckdns.org:8123/api/states/climate.panasonic_aircon'
    headers:
      content-type: application/json
      password: xxxxxxxxxxxx
    payload: '{"state": "on", "attributes": {"current_temperature": {{ states.sensor.temperature_158d0001f52edb.state }}, "min_temp": 16, "max_temp": 30, "temperature": {{ states.sensor.climate_temperature.state }}, "target_temp_step": 1, "fan_mode": "{{ states.sensor.climate_temperature.state }}", "fan_list": ["low", "mid", "high", "auto"], "operation_mode": "off", "operation_list": ["off", "cool", "heat", "on"], "unit_of_measurement": "°C", "friendly_name": "Heat Pump", "supported_features": 193}}'

sensor:
  - platform: template
    sensors:
      climate_temperature:
        value_template: "{{ states.climate.panasonic_aircon.attributes['temperature'] }}"
      climate_fan_mode:
        value_template: "{{ states.climate.panasonic_aircon.attributes['fan_mode'] }}"

Automation.yaml

- alias: "Set AC to ON"
  trigger:
  action:
    - service: rest_command.set_climate_to_on
2 Likes