TRIGGERcmd
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Run one of your TRIGGERcmd commands from a python script via Home Assistant

    Home Assistant
    1
    1
    66
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • RussR
      Russ
      last edited by Russ

      Get your HA "Long-lived access tokens" from the Security page under your user profile. Click your HA username in the lower left, to open your user profile.

      You should see this at the top of the page:
      f9d78abf-2992-4714-86d1-df45a2813cf5-image.png

      import requests
      
      # Home Assistant settings
      BASE_URL = "http://(your HA server IP):8123/api"
      TOKEN = "(your long-lived HA access token)"
      
      # Headers for authentication
      HEADERS = {
          "Authorization": f"Bearer {TOKEN}",
          "Content-Type": "application/json"
      }
      
      def toggle_light(entity_id):
          """Toggle a light entity in Home Assistant."""
          url = f"{BASE_URL}/services/light/toggle"
          data = {"entity_id": entity_id}
          
          response = requests.post(url, json=data, headers=HEADERS)
          
          if response.status_code == 200:
              print(f"Successfully toggled {entity_id}.")
          else:
              print(f"Failed to toggle {entity_id}. Status code: {response.status_code}, Response: {response.text}")
      
      # Example usage
      if __name__ == "__main__":
          light_entity_id = "switch.laptop_calculator"  # Replace with your entity ID
          toggle_light(light_entity_id)
      

      The above script shows how to flip a switch, including the virtual switches you get from the official TRIGGERcmd integration.

      Russell VanderMey

      1 Reply Last reply Reply Quote 0
      • First post
        Last post