Time Trigger to activate a xiaomi power switch (automation or api)

Hello,

My first post here.
I ve been using home assistant and I ve been able to set some nice lovelace dashboards using various stuff : Daiking AC, Philips Hue and also setup a Deconz link for my other Zigbee peripherals, even integrated a webcam managed by motion on a raspberry pi so, pretty cool stuff so far, love the interface.

I recently ordered a Xiaomi Mi Smart Plug which I can activate from lovelace dashboard just fine. I m also able to get the power consumption.

Since everything is now in motion, I wanted to start playing with simple automation but so far, no luck.

My goal :

  • Activate my Xiaomi power plug at 9PM everyday
  • Deactivate my XIaomi power plug at 1AM every day

No condition, simple stuff (at least I thought that would be an easy thing to do) much like two daily crontabs.

I tried playing with automation.yaml : Unsuccessfull
I also tried to play with Rest API calls : Meh…

Let me start with the API Calls as we can easily query metadata of the device.

 curl -s -X GET   -H "Authorization: Bearer $Token"   -H "Content-Type: application/json"   http://localhost:8123/api/states/switch.smart_plug_3 | jq .
{
  "entity_id": "switch.smart_plug_3",
  "state": "on",
  "attributes": {},
  "last_changed": "2021-03-25T11:06:19.072706+00:00",
  "last_updated": "2021-03-25T11:29:18.653351+00:00",
  "context": {
    "id": "c823a3d382ce70ceaf3a80d297c34462",
    "parent_id": null,
    "user_id": "1ef411b991514a569622d31f192a3b2a"
  }
}

So far so good, my power plug is on and powering my devices alright.

Now if I try to change the state to off :

# Sending a payload with state = off
 curl -s -X POST   -H "Authorization: Bearer $Token"   -H "Content-Type: application/json"   -d '{"state": "off"}'   http://localhost:8123/api/states/switch.smart_plug_3 | jq .
{
  "entity_id": "switch.smart_plug_3",
  "state": "off",
  "attributes": {},
  "last_changed": "2021-03-25T11:34:44.919181+00:00",
  "last_updated": "2021-03-25T11:34:44.919181+00:00",
  "context": {
    "id": "00196f8b49bd8b9e3df7184dffaea4df",
    "parent_id": null,
    "user_id": "1ef411b991514a569622d31f192a3b2a"
  }
}

# Checking if the change took effect
 curl -s -X GET   -H "Authorization: Bearer $Token"   -H "Content-Type: application/json"   http://localhost:8123/api/states/switch.smart_plug_3 | jq .                  {
  "entity_id": "switch.smart_plug_3",
  "state": "off",
  "attributes": {},
  "last_changed": "2021-03-25T11:34:44.919181+00:00",
  "last_updated": "2021-03-25T11:34:44.919181+00:00",
  "context": {
    "id": "00196f8b49bd8b9e3df7184dffaea4df",
    "parent_id": null,
    "user_id": "1ef411b991514a569622d31f192a3b2a"
  }
}

The state does indeed switch to off but the power plug is still providing power.
I expected the changed state to trigger and actualy turn off my power plug.
NOTE : I checked the syntax when I turn the power plug to off using lovelace dashboard and If I query the sate of the device, the syntax is indeed “off” and not “Off or OFF”

I browsed official documentation and various posts but so many ways to do things, none worked for me after trying over and over for a day. So I figured I’d come beg for help here :stuck_out_tongue:

Also, if it could work using automation.yaml that would be great.

One of the things I tried… :

- id: Test Light Activation
    alias: Lights On
    trigger:
      platform: time
      at: '21:00:00'
    action:
      - service: switch.turn_on
        entity_id: switch.smart_plug_3
        data:
          state: on
- id: Test Light Activation
    alias: Lights Off
    trigger:
      platform: time
      at: '01:00:00'
    action:
      - service: switch.turn_off
        entity_id: switch.smart_plug_3
        data:
          state: on

Idealy I wanted a blue print to be able to configure this from the dashboard by providing a “Time to power on” and “Time to power off” but at this point, I would just be happy with a configuration file or an api call through crontab… :stuck_out_tongue:

Thanks

Hi! Welcome to Home Assistant.

Looks like you’re overthinking things a bit and making life hard for yourself. The easiest way to do what you want is to use the Automation Editor to create the automation. You don’t have to learn YAML, etc.

First, if you haven’t already, create two time helpers. Go to Configuration -> Helpers. Click “+ ADD HELPER” in the bottom-right corner. Select “Date and/or time”. Give it a name (e.g., On Time), select the Time option, and click CREATE. Do it again to create the Off Time helper.

Next, go to the Overview page and change the values for the two time helpers. These are in 24 hour format, so enter “01:00” for Off Time and “21:00” for On Time.

Lastly, go to Configuration -> Automations. Click on “+ ADD AUTOMATION” in the bottom-right corner. Click “START WITH AN EMPTY AUTOMATION”. Under Triggers, select Time for “Trigger type”. Select the “Value of a date/time helper” option. Then click on the drop-down and select the On Time helper. Then under Actions select either Device or “Call service” and enter the appropriate values to turn the plug on. When you have everything the way you like, click SAVE. Then go back and do it again to create the automation to turn the plug off.

1 Like

Huzza!

Thanks for this quick and efficient answer.
I just tested this following your guide and it’s working fine thank you very much kind sir.

I ll play around with HA and automation through the UI a bit more I guess before messing with the config files :stuck_out_tongue:

Regards

Quick update if anyone finds interest to this post.
In order to interract with the power plug through the API use the “services” call, not the “states” one mentioned in the OP.
The “services” call is only good to query, create/update metadata. It doesn’t interact with the actual device.

To actually interact with the device using the "states"call :

# Turn On
curl -s -X POST \
  -H "Authorization: Bearer $Token" \
  -H "Content-Type: application/json" \
  -d '{"state": "on"}' \
  http://localhost:8123/api/states/switch.smart_plug_3

# Turn Off
curl -s -X POST \
  -H "Authorization: Bearer $Token" \
  -H "Content-Type: application/json" \
  -d '{"state": "off"}' \
  http://localhost:8123/api/states/switch.smart_plug_3

HA has what is called its State Machine. It is where the states of all the entities are recorded. When a device updates itself, it writes its new state to the State Machine. The State Machine is also where “things” look to see the current state of all the entities. And by “things”, I mean the UI, automations, etc. They don’t get the state directly from the devices themselves.

If you go to the STATES tab of the Developer Tools page, you see what’s in the State Machine for all the entities. If you click on an entity, it will populate in the fields at the top. If you change anything there and then click SET STATE, it will only write the new values to the State Machine. It won’t change the device itself. This is why it says:

Set the representation of a device within Home Assistant.
This will not communicate with the actual device.

The only way to change a device is to call an appropriate service. You can do that from the SERVICES tab of the Developer Tools page, or in the action of an automation, script, etc.

If you use HA’s REST API (as you show), you read/write the State Machine (just like on the STATES page) if you use .../api/states/... Again, this only changes the representation in the State Machine; it doesn’t change the actual device. If you want to change a device using the REST API, you have to use .../api/services/<domain>/<service>, which calls a service.